Reply

Access REST API via Perl (Perl2Rest NetMRI)

BAndersen Employee
Employee
Posts: 15
1504     0

This is a sekelton Perl script for access the NetMRI REST API.  This allows you to pull back data and use the strength of Perl to manipulate the data as you see fit.

 

------Code---------

#!/usr/bin/perl
use Data:Smiley Very Happyumper;
use LWP;
use URI;
use JSON;

$USER="USERNAME";
$PASS="PASSWORD";
$SERVER="SERVER";
$VERSION="2.9";

my $browser=LWP::UserAgent->new();


#Open up a cookie jar in a local file which only exists for the length of runtime
$browser->cookie_jar({ file => ".$$.cookies.txt" });

#Turn off SSL verification (Either method works)
#$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
$browser->ssl_opts(verify_hostname => 0);

#Build our authentication request
$url = URI->new("https://$SERVER/api/$VERSION/authenticate.json");
$url->query_form(
    username => "$USER",
    password => "$PASS"
);
#Authenticate, place cookies in the cookie jar automatically
my $response=$browser->get($url);

#BEGIN our scripting here

#In this example we will get all of the devices and then print out their IPs

#Build the WAPI URL by passing options to query_form
$url = URI->new("https://$SERVER/api/$VERSION/devices/index");
$url->query_form(
    sort => 'DeviceIPDotted'
);

#Execute the request
my $response=$browser->get($url);

#Create a Perl data object reference from JSON
$ref=from_json($response->content);

print "Total Devices: ",$ref->{'total'},"\n";

$devarray=$ref->{'devices'};
$i=1;
foreach $device (@$devarray){
  print "($i) ",$device->{'DeviceIPDotted'},",",$device->{'DeviceMAC'},"\n";
  $i++;
}

#print Dumper($ref),"\n\n";

exit;

Highlighted

You can use this sort of

Adviser
Posts: 357
1504     0

You can use this sort of script if you want to access the API via Perl but do not want to install the full Perl API package. The API package includes a complete framework binding the JSON object to Perl classes, however, and therefore is usually much more convenient.

 

 

Showing results for 
Search instead for 
Do you mean 

Recommended for You