Generating a Report of Routing Table Information

Problem

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

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 standard output (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=\Q$snmpwalk $rtr ifDescr\Q; for $ifnum (@iftable) { chomp (($intno, $intname) = split (/ = /, $ifnum)); $intno=~s/.*ifDescr\.//; $intname=~s/"//gi; $int{$intno}=$intname; } @ipRouteDest=\Q$snmpwalk $rtr ipRouteDest\Q; @ipRouteMask=\Q$snmpwalk $rtr ipRouteMask\Q; @ipRouteNextHop=\Q$snmpwalk $rtr ipRouteNextHop\Q; @ipRouteProto=\Q$snmpwalk $rtr ipRouteProto\Q; @ipRouteIfIndex=\Q$snmpwalk $rtr ipRouteIfIndex\Q; #@ipRouteMetric1=\Q$snmpwalk $rtr ipRouteMetric1\Q; 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, ...

Get Cisco IOS Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.