Reply

How to access netmri::API from an internal perl script ?

Authority
Posts: 40
3512     0

Hi,

I am trying to find a list of all neighbors (and its IP) connected to a device. I don't seem to be able to access the netmri::API from an internal perl script. Can you please help?

Script snippet,

my $netmri = new NetMRI_Easy;

my $broker = $netmri->broker->neighbor;
my @devices = $broker->index();

However, it seems that $netmri->broker->neighbor didn't return anything. I tried. $netmri->broker, $netmri->get_broker('Neighbor') available from netmri_easy library.

The output of $broker is shown below...which is not correct.

 Broker:
$VAR1 = bless( {
                 'client' => 0,
                 '_legacy' => 0
               }, 'NetMRI::API::Broker::V2_10_0::Neighbor' );
$VAR2 = '
';

 

Thanks,

Rahul Shenoy

 

This is a sample API Script

MGriffin
Techie
Posts: 37
3512     0

Rahul,

Look at the sample API script "neighbors.pl"

In the expanded API archive, falls in directory "NetMRI/API/samples"

 


# cat neighbors.pl
#!/usr/bin/perl
use strict;
use NetMRI::API::Client;
 
usage() if !@ARGV;
 
my $netmri = new NetMRI::API::Client();
 
 
my $broker = $netmri->get_broker("Device");
my @devs = $broker->find(@ARGV);
 
die "No matching devices found.\n" if !@devs;
 
foreach my $d (@devs) {
print "\n\nCurrent neighbors of $d->{DeviceName} $d->{DeviceIPDotted} ($d->{DeviceID}):\n";
my @ns = sort { $a->{ifIndex} <=> $b->{ifIndex} || $a->{NeighborDeviceID} <=> $b->{NeighborDeviceID} || $a->{NeighborIfIndex} <=> $b->{NeighborIfIndex} } $d->get_neighbors();
 
foreach my $n (@ns) {
        my $nd;
        eval {
                $nd = $broker->find_by_id($n->{NeighborDeviceID});
        };
        if ($@ =~ /^H404/) {
                print "Could not find device $n->{NeighborDeviceID}\n";
                next;
        }
 
        printf "\nOn %7s %6d %15s %16s \%s\n", ($n->{ifIndex} ? ("if" . $n->{ifIndex}) : "unknown"), $nd->{DeviceID}, $nd->{DeviceName}, $nd->{DeviceIPDotted}, ($n->{NeighborIfIndex} ? ("if" . $n->{NeighborIfIndex}) : "unknown");
        if ($n->{CDPInd}) {
                my $fmt = "              CDP: %20s %20s %16s\n";
                my @dets = $n->get_neighbor_details("CDP");
                foreach my $det (@dets) {
                        printf $fmt, $det->{CDPNeighborIPDotted}, $det->{CDPNeighborPortName}, $det->{CDPNeighborMAC};
                        $fmt = "                   %20s %20s %16s\n";
                }
        }
        if ($n->{SwitchFwdInd}) {
                my $fmt = "        SwitchFwd: %20s %20s %16s\n";
                my @dets = $n->get_neighbor_details("SwitchFwd");
                foreach my $det (@dets) {
                        printf $fmt, "", "", $det->{SwitchFwdNeighborMAC};
                        $fmt = "                   %20s %20s %16s\n";
                }
        }
        if ($n->{SerialInd}) {
                my $fmt = "           Serial: %20s %10s %16s\n";
                my @dets = $n->get_neighbor_details("Serial");
                foreach my $det (@dets) {
                        printf $fmt, $det->{SerialNeighborIPDotted}, "", "";
                        $fmt = "                      %20s %10s %16s\n";
                }
        }
        if ($n->{DirectEthernetInd}) {
                print "   DirectEthernet: (no additional detail available)\n";
        }
        if ($n->{RevSwitchFwdInd}) {
                my $fmt = "     RevSwitchFwd: %20s %20s %16s\n";
                my @dets = $n->get_neighbor_details("RevSwitchFwd");
                foreach my $det (@dets) {
                        my $nb = $netmri->get_broker("Neighbor");
                        my $sf = $nb->find($det->{SwitchFwdNeighborID});
                        my @more = $sf->get_neighbor_details("SwitchFwd");
                        foreach my $md (@more) {
                                printf $fmt, "(SwitchFwd $md->{NeighborID})", "", $md->{SwitchFwdNeighborMAC};
                                $fmt = "                   %20s %20s %16s\n";
                        }
                }
        }
        if ($n->{IPRoutedInd}) {
                my $fmt = "         IPRouted: %20s    via %16s\n";
                my @dets = $n->get_neighbor_details("IPRouted");
                foreach my $det (@dets) {
                        my $r = $det->get_device_route();
                        printf $fmt, $r->{RouteCIDR}, $r->{RouteNextHopIPDotted}, $r->{RouteIfIndex};
                        $fmt = "                   %20s    via %16s\n";
                }
        }
 
}
}
 
sub usage
{
        print "\nDescription\n-----------\n\n";
        print "Searches for devices and prints out neighbor relationships.\n";
        print "\nUsage\n-----\n\n";
        print "   $0 {  |   [   ...] }\n\n";
        print "                - a device id\n";
        print "      - field/value pairs (multiple may be specified: DeviceVendor Cisco DeviceModel 3630)\n";
        print "\n";
 
        exit 0;
}
Highlighted

I think his question was from

Adviser
Posts: 357
3512     0

I think his question was from the NetMRI_Easy object. That should be the same broker it's returning to you, so I would expect it to work.

 

Can navigate to:

https://yournetmri/api/2.10/neighbors

?

That should be the same as the results you get from the call in your script, except in XML since the browser includes an Accepts: header for XML. If you wanted to see the JSON that the API uses underneath, append ".json" to that.

If you get no results there then you have no neighbor records. Is this a standalone or distributed (OC) system, and what version?

 

 

Hi Mike and John,

Authority
Posts: 40
3513     0

Hi Mike and John,

Thanks a ton for your help. You actually saved me lots of hours today. You are right John. I am able to access the results through netMRI_easy. I was confused about the broker objects returned. The broker->neighbor->index  returns very large outputs...I thought it was garbage or irrelevant output. Most of the entries I saw were type "unknown" but relevant results were hidden deep inside. I didn't expect it to throw all the neighbor relationships in the system and then select a desired subset. Now my understanding of the NetMRI::API datastructures is much better!!! Thanks a lot guys and thanks a ton Mike for your immediate reply.

 

Thanks,

Rahul Shenoy

Showing results for 
Search instead for 
Do you mean 

Recommended for You