Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I just wanted to say THANKS for the forum. The knowledge I gain from your site is invaluable..."

Geography

Where in the world do Tek-Tips members come from?

SNMP Perl script to view routing table on Cisco

RFC1795 (IS/IT--Management)
24 Apr 12 12:34
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:
http://fengnet.com/book/cisco.ios.cookbook.2nd/I_0596527225_CHP_2_SECT_19.html

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


 
bnorton916 (Programmer)
24 Apr 12 12:48
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
RFC1795 (IS/IT--Management)
25 Apr 12 6:34
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

 
bnorton916 (Programmer)
26 Apr 12 9:51
Hmmm...

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

What model router is this?

Bill

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close