Previous section   Next section

Recipe 2.14 Generating a Report of Routing Table Information

2.14.1 Problem

You need to extract the IP routing table from one of your routers.

2.14.2 Solution

The script in Example 2-2, rt.pl, uses SNMP to extract the routing table from a specified router, and displays this information to STDOUT. The script expects to find a hostname or IP address of a router on the command line.

Example 2-2. rt.pl
#!/usr/bin/perl
#
#           rt.pl -- a script to extract the routing table
#                    from a router.
#
#Set behavior
$snmpro="ORARO";
#
$x=0;
$snmpwalk="/usr/local/bin/snmpwalk -v 1 -c $snmpro";
$snmpget="/usr/local/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`;
for $ifnum (@iftable) {
    chomp (($intno, $intname) = split (/ = /, $ifnum));
    $intno=~s/.*ifDescr\.//;
    $intname=~s/"//gi;
    $int{$intno}=$intname;
}
@ipRouteDest=`$snmpwalk $rtr ipRouteDest`;
@ipRouteMask=`$snmpwalk $rtr ipRouteMask`;
@ipRouteNextHop=`$snmpwalk $rtr ipRouteNextHop`;
@ipRouteProto=`$snmpwalk $rtr ipRouteProto`;
@ipRouteIfIndex=`$snmpwalk $rtr ipRouteIfIndex`;
#@ipRouteMetric1=`$snmpwalk $rtr ipRouteMetric1`;
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++
}

2.14.3 Discussion

The rt.pl script is written in Perl and uses NET-SNMP to extract the routing table information via SNMP. Perl and NET-SNMP must be in the /usr/local/bin directory. For more information on Perl or NET-SNMP, see Appendix A.

You must define the variable $snmpro to contain the SNMP read-only community string for the router before using this script:

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%

The output from the script is relatively straightforward, except for static routes and directly connected routes, which require a little explanation.

For static routes, the output shows a value of "local" in the protocol field and an interface name of "Local." Directly connected routes also appear as "local" protocol information, but the interface name is the real interface associated with this route.

For example, the route 172.20.0.0 255.255.0.0 is a static route:

172.20.0.0      255.255.0.0     172.25.1.5        local Local

172.25.1.0 255.255.255.0 is a directly connected route:

172.25.1.0      255.255.255.0   172.25.1.7        local Ethernet0

Since this script queries only open standard SNMP MIB value, you can use it to extract IP route information from most any SNMP-enabled device, including non-Cisco equipment.

2.14.4 See Also

Appendix A


  Previous section   Next section
Top