Generating a Report of Routing Table Information


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


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

Example 2-2.

# -- a script to extract the routing table
# from a router.
#Set behavior
$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
print "Destination	Mask		Nexthop";
print "		 Proto	Interface
@iftable=Q$snmpwalk $rtr ifDescrQ;
for $ifnum (@iftable) {
 chomp (($intno, $intname) = split (/ = /, $ifnum));
@ipRouteDest=Q$snmpwalk $rtr ipRouteDestQ;
@ipRouteMask=Q$snmpwalk $rtr ipRouteMaskQ;
@ipRouteNextHop=Q$snmpwalk $rtr ipRouteNextHopQ;
@ipRouteProto=Q$snmpwalk $rtr ipRouteProtoQ;
@ipRouteIfIndex=Q$snmpwalk $rtr ipRouteIfIndexQ;
#@ipRouteMetric1=Q$snmpwalk $rtr ipRouteMetric1Q;
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
",$dest, $mask, $nhop, $prot, $int1);


The script is written in Perl and uses NET-SNMP to extract the routing table information via SNMP. It expects to find both Perl and NET-SNMP in the /usr/local/bin directory. For more information on Perl or NET-SNMP, please see Appendix A.

Before using the script, you must define the variable $snmpro to contain the SNMP read-only community string for the router:

Freebsd% ./ toronto
Destination Mask Nexthop Proto Interface ospf Ethernet0 ospf Ethernet0 ospf Ethernet0 local Local ospf Ethernet0 ospf Ethernet0 ospf Ethernet0 (e)igrp Ethernet0 ospf Ethernet0 local Ethernet0 (e)igrp Ethernet0 (e)igrp Ethernet0 local Loopback0 (e)igrp Ethernet0 ospf Ethernet0

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 is a static route: local Local 

And is a directly connected route: local Ethernet0 

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

See Also

Appendix A

Router Configuration and File Management

Router Management

User Access and Privilege Levels


IP Routing





Frame Relay

Handling Queuing and Congestion

Tunnels and VPNs

Dial Backup

NTP and Time


Router Interfaces and Media

Simple Network Management Protocol





First Hop Redundancy Protocols

IP Multicast

IP Mobility




Appendix 1. External Software Packages

Appendix 2. IP Precedence, TOS, and DSCP Classifications


Cisco IOS Cookbook
Cisco IOS Cookbook (Cookbooks (OReilly))
ISBN: 0596527225
EAN: 2147483647
Year: 2004
Pages: 505 © 2008-2020.
If you may any questions please contact us: