####
#!/usr/bin/perl
use strict;
use warnings;
use 5.014;
use MIME::Base64;
use Data::Dumper;
use utf8;
use Text::Unidecode;
use XML::Twig;
use XML::LibXML;
use REST::Client;
use Data::Validate::Domain qw(is_hostname);
use Socket;
use Net::DNS;
use Net::MAC;
# Just in case we want to request JSON instead of XML from CMDB
#use JSON; Just in case we want to request JSON instead of XML from CMDB
my $scriptversion = "1.01 (Vance Turner, Staples Inc., 1/20/2016)";
our $header_count = 0;
our $fqdn_pos = 0;
our $ip_address_pos = 0;
our $dns_domain_pos = 0;
our $name_pos = 0;
our $mac_address_pos = 0;
# declare our command line options variables.
our $host = 'https://staplessb.service-now.com';
our $wanthelp = 0;
our $record_limit = 1;
our $class = '';
our $user = '';
our $pwd = '';
our $offset = 0;
our $outputfile = 'XMLDUMP.xml';
sub usage
{
my $err = shift and select STDERR;
print <] [-c ] [-u username ] [-p password] [-o ]
-t use for the CMDB connection to open. (https://staplessb.service-now.com)
-r use as the maximum nomber of CMDB records to retrieve, default = '100'
-c use as the CMDB class records you want to retrieve.
-u CMDB username to use
-p CMDB password to use
-o write output to file named , defaults
to stdout if no outputfile is specified
Examples:
perl $0 -u user -p password -r max_records -o outputfile.xml (creates outputfile.xml)
EOU
exit $err;
} # usage
sub Get_CMDB_CI
{
my $client = REST::Client->new(host => $host);
my $encoded_auth = encode_base64("$user:$pwd", '');
$client->GET("/api/now/table/cmdb_ci?sysparm_limit=$record_limit&sysparm_offset=$offset",
{'Authorization' => "Basic $encoded_auth",
'Accept' => 'application/xml'});
my $CI_RECS = $client->responseContent();
return ($CI_RECS);
}
use Getopt::Long qw(:config bundling nopermute passthrough);
GetOptions (
"help|h|?" => \$wanthelp ,
"t=s" => \$host,
"r=i" => \$record_limit,
"c=s" => \$class,
"u=s" => \$user,
"p=s" => \$pwd,
"o=s" => \$outputfile,
);
if ($wanthelp) {
usage(1);
}
if (-s $outputfile) {
print STDERR "File '$outputfile' already exists. Overwrite? [y/N] > N\b";
scalar =~ m/^[yj](es|a)?$/i or exit;
}
my $input_xml;
open my $fh, '>>:encoding(utf8)', $outputfile or die "Cannot open: $outputfile: $!";
do {
say $fh $input_xml;
$offset = $offset + $record_limit +50000 ;
print "Offset=$offset\n";
} while ($input_xml = Get_CMDB_CI());
close($fh) || warn "close failed: $!";