Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SNMP Perl script to view routing table on Cisco

Status
Not open for further replies.

RFC1795

IS-IT--Management
Feb 13, 2003
76
US
Hi all

I'm trying to understand why this snmp script is not working, not sure if its the actual script that is the issue as I'm not clued up in perl really (or snmp for that matter).
Figured as I'm trying to do this using SNMP this might be the best place to ask.

Original code is grabbed and adjusted from here with Feherke assistance:

Code:
#!/usr/bin/perl
#
#           rt.pl -- a script to extract the routing table
#                    from a router.
#
#Set behavior
$snmpro='private=string';
#
$x=0;
$snmpwalk="/usr/bin/snmpwalk -v 1 -c $snmpro";
#$snmpget="/usr/bin/snmpget -v 1 -c $snmpro";
chomp ($rtr=$ARGV[0]);
if ( $rtr eq "" ) {die "$0: Must specify a router\n"};
print "Destination\tMask\t\tNexthop";
print "\t\t Proto\tInterface\n";
@iftable=`$snmpwalk $rtr ifDescr`;
#@iftable=qq{$snmpwalk $rtr ifDescr};
for $ifnum (@iftable) {
    chomp (($intno, $intname) = split (/ = /, $ifnum));
    $intno=~s/.*ifDescr\.//;
    $intname=~s/"//gi;
    $int{$intno}=$intname;
}

@ipRouteDest=`$snmpwalk $rtr ipRouteDest`;
@ipRouteDest=`$snmpwalk $rtr ipRouteDest`;
@ipRouteMask=`$snmpwalk $rtr ipRouteMask`;
@ipRouteNextHop=`$snmpwalk $rtr ipRouteNextHop`;
@ipRouteProto=`$snmpwalk $rtr ipRouteProto`;
@ipRouteIfIndex=`$snmpwalk $rtr ipRouteIfIndex`;
for $intnum (@ipRouteIfIndex) {
    chomp (($foo, $int) = split (/= /, $intnum));
    chomp (($foo, $dest) = split (/: /, @ipRouteDest[$x]));
    chomp (($foo, $mask) = split (/: /, @ipRouteMask[$x]));
    chomp (($foo, $nhop) = split (/: /, @ipRouteNextHop[$x]));
    chomp (($foo, $prot) = split (/= /, @ipRouteProto[$x]));
    #chomp (($foo, $metr) = split (/= /, @ipRouteMetric1[$x]));
    $int1 = $int{$int};
    if ($int1 eq '') {$int1="Local"};
    $prot=~s/\(.*//; $prot=~s/ciscoIgrp/\(e\)igrp/;
    printf ("%-15s %-15s %-15s %7s %-25s\n",$dest, $mask, $nhop, $prot, $int1);
    $x++
}

Its supposed to output data like this:
Code:
Freebsd% ./rt.pl toronto
Destination     Mask            Nexthop          Proto  Interface
10.1.1.0        255.255.255.252 172.25.1.5         ospf Ethernet0                
10.2.2.2        255.255.255.255 172.25.1.5         ospf Ethernet0                
172.16.2.0      255.255.255.0   172.25.1.5         ospf Ethernet0                
172.20.0.0      255.255.0.0     172.25.1.5        local Local                    
172.20.1.0      255.255.255.252 172.25.1.5         ospf Ethernet0                
172.20.10.0     255.255.255.0   172.25.1.5         ospf Ethernet0                
172.20.100.1    255.255.255.255 172.25.1.5         ospf Ethernet0                
172.22.0.0      255.255.0.0     172.25.1.5      (e)igrp Ethernet0                
172.22.1.0      255.255.255.0   172.25.1.5         ospf Ethernet0                
172.25.1.0      255.255.255.0   172.25.1.7        local Ethernet0                
172.25.2.0      255.255.255.252 172.25.1.5      (e)igrp Ethernet0                
172.25.25.1     255.255.255.255 172.25.1.5      (e)igrp Ethernet0                
172.25.25.6     255.255.255.255 172.25.25.6       local Loopback0                
172.25.26.4     255.255.255.252 172.25.1.5      (e)igrp Ethernet0                
172.25.26.5     255.255.255.255 172.25.1.5         ospf Ethernet0
Freebsd%

But when I run it I just get the headers and nothing else. (Script completes and drops me to prompt once done.)

Is there a better way to do this perhaps? Or should I head beck to the Perl forum hehe
 
Well I would try the snmpwalk from the command line first.

Add an print "$snmpwalk $rtr ipRouteDest";

after @ipRouteDest=`$snmpwalk $rtr ipRouteDest`;

and then run the command from the command line.

You need to determine if you have an SNMP problem or perl problem.

Bill
 
Thanks bnorton916

The command was printed to the screen, a copy and paste to the command line did nothing, so I expect the ipRouteDest command is not supported by the Cisco device.

Code:
#/usr/bin/snmpwalk -v 1 -c private=string router1 ipRouteDest
#

If I run the command without the ipRouteDest I get loads back from the device. Got a feeling this is not going to work for me.

Changing it to snmpv2 I get this output:
Code:
RFC1213-MIB::ipRouteDest = No Such Object available on this agent at this OID

 
Hmmm...

ipRouteDest is part of MIB-II. I would have guessed that all routers would support this object.

What model router is this?

Bill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top