Learn How We Can Help You Keep Teleworkers Protected During the COVID-19 Crisis

Network Change & Configuration Management

Reply
Highlighted

Access REST API via Perl (Perl2Rest NetMRI)

Employee
Employee
Posts: 15
2070     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: 353
2071     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.

 

 

Highlighted

Re: Access REST API via Perl (Perl2Rest NetMRI)

Authority
Posts: 24
2071     0
How to update EA to the using rest in Perl ?
Highlighted

Re: Access REST API via Perl (Perl2Rest NetMRI)

Adviser
Posts: 422
2071     0

Can you please start a new thread instead of replying to a very old one.

 

You can add a link to it Smiley Happy

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh
Showing results for 
Search instead for 
Do you mean 

Recommended for You