Reply
Highlighted

Access REST API via Perl (Perl2Rest)

BAndersen Employee
Employee
Posts: 15
2170     0

This is a skeleton Perl script to help you access the REST API via Perl and an LWP User Agent.  The benefit of Perl over CURL is that you can take the output of the REST query and then use the data and manipulate it.  This uses the get() method [Read], but there are also functions for put() [Update] , post() [Create], delete() [Delete] so that you can actually manipulate data on the system.

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

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

$USER="USER";
$PASS="PASS";
$SERVER="Server";
$VERSION="1.4"; #WAPI version

#Get authentication realm from print $response->header("WWW-Authenticate");
$REALM="InfoBlox ONE Platform";

#Create a Perl CLI browser
my $browser=LWP::UserAgent->new();

#Ignore SSL Cert & set credentials
$browser->ssl_opts(verify_hostname => 0);
$browser->credentials("${SERVER}:443","$REALM","$USER","$PASS");


=pod
Various objects that can be fetched

fixedaddress
ipv4address
ipv6address
ipv6fixedaddress
ipv6network
ipv6networkcontainer
ipv6range
lease
macfilteraddress
member
namedacl
network
networkcontainer
networkview
range
record:a
record:aaaa
record:host
record:host_ipv4addr
record:host_ipv6addr
record:mx
recordSmiley Tonguetr
record:srv
record:txt
restartservicestatus
scheduledtask
search
view
zone_auth
zone_delegated
zone_forward
zone_stub
=cut

#Search for ip addresses >=10.0.0.0
$url = URI->new("https://$SERVER/wapi/v$VERSION/ipv4address");
#Add fields to query
$url->query_form(
    '_max_results' => '10',
    'ip_address>' => '10.0.0.0'
#    '_return_fields' => 'discovered_data'
);

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

die "Error: ", $response->status_line, "\n", Dumper($response->headers), "\n\n\n",
    "Is the REALM correct: ",$response->header("WWW-Authenticate"), "== ", $REALM
 unless $response->is_success;

#Dump out the Headers
#print Dumper($response->headers);

$ref=from_json($response->content);

#Dump out the Perl Object
#print Dumper($ref),"\n\n";

foreach $item (@$ref){
  print "Network = ", $item->{'network'},"\n",
      "Names = ", join(",",@{$item->{'names'}}),"\n",
      "IP = ", $item->{'ip_address'},"\n\n";
}

exit;

Showing results for 
Search instead for 
Do you mean 

Recommended for You