- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
Access REST API via Perl (Perl2Rest NetMRI)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
04-04-2014 09:02 AM
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:umper;
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;
You can use this sort of
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
04-04-2014 09:11 AM
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.
Re: Access REST API via Perl (Perl2Rest NetMRI)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
05-19-2020 02:32 PM
Re: Access REST API via Perl (Perl2Rest NetMRI)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
05-20-2020 07:15 AM
Can you please start a new thread instead of replying to a very old one.
You can add a link to it
Twitter: https://twitter.com/sifbaksh
https://sifbaksh.com