Access REST API via Perl (Perl2Rest NetMRI)

BAndersen Employee
Posts: 15
1607     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.



use Data:Smiley Very Happyumper;
use LWP;
use URI;
use JSON;


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)
$browser->ssl_opts(verify_hostname => 0);

#Build our authentication request
$url = URI->new("https://$SERVER/api/$VERSION/authenticate.json");
    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");
    sort => 'DeviceIPDotted'

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

#Create a Perl data object reference from JSON

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

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

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



You can use this sort of

Posts: 357
1607     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