FusionInventory-Agent

 view release on metacpan or  search on metacpan

bin/fusioninventory-remoteinventory  view on Meta::CPAN

use Getopt::Long;
use Digest::SHA;
use XML::TreePP;
use Time::HiRes qw(gettimeofday);
use File::Which;

our $VERSION = "1.0";

my $options = {
    useragent => "FusionInventory-RemoveInventory/$VERSION",
};

GetOptions(
    $options,
    'help|h',
    'useragent|u=s',
    'verbose|v',
    'debug',
    'port|p=i',
    'timeout|t=i',
    'baseurl|b=s',
    'token|T=s',
    'directory|d=s',
    'id=s',
    'ssl|s',
    'ca-cert-file=s',
    'no-ssl-check',
    'no-compression|C',
) or pod2usage(-verbose => 0);

pod2usage(-verbose => 0, -exitstatus => 0) if $options->{help};

pod2usage(
    -message => "\nGive a least one host to get inventory from as parameter\n",
    -verbose => 0,
    -exitstatus => 1
) unless @ARGV;

pod2usage(
    -message => "\nNo token as shared secret defined\n",
    -verbose => 0,
    -exitstatus => 1
) unless $options->{token};

pod2usage(
    -message => "\nWhen asking inventory to more than one host, you must use the --directory parameter\n",
    -verbose => 0,
    -exitstatus => 1
) if !$options->{directory} && @ARGV>1;

pod2usage(
    -message => "\nDirectory not found: $options->{directory}\n",
    -verbose => 0,
    -exitstatus => 1
) if ($options->{directory} && ! -d $options->{directory});

my $ua = LWP::UserAgent->new(
    agent                 => $options->{useragent},
    timeout               => $options->{timeout} || 180,
    parse_head            => 0, # No need to parse HTML
    keep_alive            => 1,
);

if ($options->{ssl}) {
    $ua->ssl_opts(SSL_ca_file => $options->{'ca-cert-file'})
        if $options->{'ca-cert-file'};
    $ua->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0)
        if $options->{ssl} && $options->{'no-ssl-check'};
}

$options->{verbose} = 1 if $options->{debug};

my $id = $options->{id} || id();

warn "Using $id as request id\n"
    if $options->{verbose};

foreach my $host (@ARGV) {
    my $url = ( $options->{ssl} ? "https://" : "http://" ). $host;
    $url .= $options->{port} ? ":".$options->{port} : ":62354";
    $url .= $options->{baseurl} ? $options->{baseurl} : "/inventory";

    warn "$host: Trying $url\n"
        if $options->{verbose};

    my $req = HTTP::Request->new(GET => $url.'/session');
    $req->header( 'X-Request-ID' => $id );
    $req->protocol( 'HTTP/1.1' );

    if ($options->{debug}) {
        warn "--->\n";
        warn "Request: ".$req->as_string();
    }

    my $session = $ua->request($req);

    if ($options->{debug}) {
        warn "<---\n";
        warn "Response: ".$session->as_string();
    }

    if (!$session->is_success()) {
        warn "$host: No session (".$session->status_line().")\n";
        next;
    }

    my $nonce = $session->header('X-Auth-Nonce')
        or die "No nonce\n";

    my $sha = Digest::SHA->new(256);
    $sha->add($nonce.'++'.$options->{token});

    my $payload = $sha->b64digest;

    # Update request to get inventory
    $req->uri($url.'/get');
    $req->header( 'X-Auth-Payload' => $payload );

    # Set Accept header
    my $accept = 'application/xml';
    if (!$options->{'no-compression'}) {



( run in 2.458 seconds using v1.01-cache-2.11-cpan-d8267643d1d )