mcalvi

Import Asset Groups

Discussion created by mcalvi on Jun 19, 2012
Latest reply on Jul 30, 2012 by Parag Baxi

Inline is some code we used to import large amounts of asset groups.  It is a bit of a kludge but it works for us so we thought we would share.

 

It is run by typing the following.

 

/usr/bin/perl ./importAssetGroups.pl userid password inputFile outputFile

 

 

First off the import ifle needs to be in the format of the following

 

AssetGroupName,Domain,IP-Range,Scanner,BusinessUnit

 

For example:

 

NA-Tx-Dallas,contoso.com,10.54.112.0-10.54.112.255,NA-Scanner,NA-Finance

NA-Tx-SanAntonio,contoso.com,10.54.113.0-10.54.113.255,NA-Scanner,NA-Sales

 

 

Next you need to have perl and the apporpriate modules installed.

 

 

#!/usr/bin/perl -w

########################################
#
# library imports
#
########################################
use strict;
use LWP::UserAgent;
use Config::Simple;
use HTTP::Request;
require XML::Twig;

 


########################################
#
#  define variables
#
########################################
my $user = $ARGV[0];
my $pass = $ARGV[1];
my $request;            # HTTP request handle
my $result;             # HTTP response handle
my $server = "qualysapi.qualys.com";            # QualysGuard server's FQDN hostname
my $url;                # API access URL
my $xml;                # Twig object handle
my $inFile = $ARGV[2];
my $outFile = $ARGV[3];
my $target_ip_file="";
my $tracking_method="";
my $owner="";
my $location="";
my $function="";
my $asset_tag="";
my $comment="";
my $query="";


########################################
#
# Error message
#
########################################
sub error {
        my ($xml, $element) = @_;

        my $number = $element->att('number');
        my $message;

        # Some APIs return embedded "<SUMMARY>error summary text</SUMMARY>"
        # elements, so detect and handle accordingly. NOTE: <SUMMARY>
        # elements are usually included for reporting multiple errors with
        # one error element.

        if (!($message = $element->first_child_trimmed_text('SUMMARY'))) {
                $message = $element->trimmed_text;
        }

        if ($number) {
                printf STDERR "Request Status: FAILED\nError Number: %1d\nReason: %s\n", $number, $message;
        } else {
                printf STDERR "Request Status: FAILED\nReason: %s\n", $message;
        }

        exit 255;
}


#######################################
#
# Generic return message
#
########################################
sub generic_return {
        my ($xml, $element) = @_;

        my ($return, $status, $number, $message);

        # This is a GENERIC_RETURN element. So, display the RETURN element,
        # which gives the detailed status.

        if ($return = $element->first_child('RETURN')) {
                $status  = $return->att('status');
                $number  = $return->att('number');
                $message = $return->trimmed_text;

                if ($number) {
                        printf STDERR "Query: %s\nRequest Status: %s\nError Number: %1d\nReason: %s\n", $query, $status, $number, $message;

                        if ($number == 1999)
                        {
                                sleep (120);
                        }
                } else {
                        printf STDERR "Request Status: %s\nReason: %s\n", $status, $message;
                }
        } else {
                # An XML recognition error; display the XML for the offending
                # element.

                printf STDERR "Unrecognized XML Element:\n%s\n", $element->print;
        }

        #exit ($status eq "SUCCESS" ? 0 : 255);
}

close STDERR;
my $count = 0;
open IN, "$inFile" || die "Cannot open file\n";
open STDERR, ">$outFile" || warn "Cannot open error $outFile\n";

while (my $line = <IN>)
{
        chomp ($line);

        my ($grpName, $domain, $range, $scanner, $div) = split /,/,$line;

        $query = "&title=$grpName";
        $query .= "&host_ips=$range";
        $query .= "&domains=$domain";
        $query .= "&scanner_appliances=$scanner";
        $query .= "&default_scanner_appliance=$scanner";
        $query .= "&division=$div";

        # XML::Twig is a handy way to process an XML document. We use it to attach
         # various handlers, which are triggered whenever related tags are found
         # in the XML document. We also attach an error() handler, which is
         # triggered whenever Twig finds any errors.  The generic_return()
         #handler covers the case where a
         # <GENERIC_RETURN> element is encountered.

        $url  = "https://$server/msp/asset_group.php?action=add$query";
        print "URL = \n$url\n";

        $xml = new XML::Twig(
                TwigHandlers => {
                        ERROR             => \&error,
                        GENERIC_RETURN    => \&generic_return,
                },
        );

        #  counter end after 200 hits
        if ($count > 200)
        {
                sleep (86400);
                $count = 0;
        }
        else
        {
                $count++;
                sleep (60);
        }

 

 


        # Create an instance of the authentication user agent
        my $ua  = LWP::UserAgent->new();

        $request = new HTTP::Request GET => $url;
        $request->authorization_basic($user, $pass);
        $result = $ua->request($request);

 

        if ($result->is_success) {
                # Parse the XML

                $xml->parse($result->content);

        } else {
                # An HTTP related error

                printf STDERR "HTTP Error: %s\n", $result->status_line;
                exit 1;
        }

        # print "press enter to continue\n";
        # <STDIN>;

}
close IN;
close STDERR;

Outcomes