Generating a Report of Interface Information

Problem

You want to build a spreadsheet of active IP subnets for your network.

Solution

Keeping track of assigned IP subnets on a network is a vitally important, but often tedious, task. In large organizations, it can be extremely difficult to maintain accurate and up-to-date addressing information. The Perl script in Example 2-1 uses SNMP to automatically gather current IP subnet information directly from the routers themselves. The script creates an output file in CSV format so you can easily import the information into a spreadsheet.

Example 2-1. netstat.pl

#!/usr/local/bin/perl 
#
# netstat.pl -- a script to build a detailed IP interface 
# listing directly from a list of routers. 
#
#Set behavour
$workingdir="/home/cisco/net";
$snmpro="ORARO"; 
#
$rtrlist="$workingdir/RTR_LIST";
$snmpwalk="/usr/local/bin/snmpwalk -v 1 -c $snmpro";
$snmpget="/usr/local/bin/snmpget -v 1 -c $snmpro";
open (RTR, "$rtrlist") || die "Can't open $rtrlist file";
open (CSV, ">$workingdir/RESULT.csv") || die "Can't open RESULT.csv file";
while () {
 chomp($rtr="$_");
 @ifIndex=Q$snmpwalk $rtr .1.3.6.1.2.1.4.20.1.2Q;
 @ipAddress=Q$snmpwalk $rtr .1.3.6.1.2.1.4.20.1.1Q;
 @ipMask=Q$snmpwalk $rtr .1.3.6.1.2.1.4.20.1.3Q;
 $arraynum=0;
 print CSV "
$rtr
";
 print CSV "Interface, IP-Address, Mask, MTU, Speed, Admin, Operational
";
 for $ifnumber (@ifIndex) {
 chomp(($foo, $ifnum) = split(/= /, $ifnumber));
 $ifDescription=Q$snmpget $rtr ifDescr.$ifnumQ;
 $ifMTU=Q$snmpget $rtr ifMtu.$ifnumQ;
 $ifSpeed=Q$snmpget $rtr ifSpeed.$ifnumQ;
 $ifAdminstatus=Q$snmpget $rtr ifAdminStatus.$ifnumQ;
 $ifOperstatus=Q$snmpget $rtr ifOperStatus.$ifnumQ;
 chomp(($foo, $ipaddr) = split(/: /, $ipAddress[$arraynum]));
 chomp(($foo, $mask) = split(/: /, $ipMask[$arraynum]));
 chomp(($foo, $ifdes, $foo) = split(/"/, $ifDescription));
 chomp(($foo, $mtu) = split (/= /, $ifMTU));
 chomp(($foo, $speed) = split (/: /, $ifSpeed));
 chomp(($foo, $admin) = split (/= /, $ifAdminstatus));
 chomp(($foo, $oper) = split (/= /, $ifOperstatus));
 if ( $speed > 3194967295 ) { $speed = 0 };
 $admin =~ s/(.*)//;
 $oper =~ s/(.*)//;
 if ( $oper eq "dormant" ) { $oper = "up(spoofing)"};
 $speed = $speed/1000;
 if ( $speed > 1000) {
 $speed = $speed/1000;
 $speed =~ s/$/ Mb/s/;
 }
 else {
 $speed =~ s/$/ Kb/s/;
 }
 print CSV "$ifdes,$ipaddr,$mask,$mtu,$speed,$admin,$oper
";
 $arraynum++;
 }
} 
close(RTR);
close(CSV);

Discussion

The netstat.pl script uses SNMP to gather IP subnet information from a list of routers. This ensures that the information is accurate and current. The script gathers all of the pertinent information about each router's IP interfaces and outputs this information as a CSV file.

The netstat.pl script requires Perl and NET-SNMP and expects to find both in the /usr/local/bin directory. For more information on Perl or NET-SNMP, please see Appendix A. If you keep these programs in a different location, you will need to modify the script appropriately.

Before using the script, you must define two variables, $workingdir and $snmpro. The $workingdir variable must contain the name of directory where you will store the script and its input and output files. The variable, $snmpro must contain the SNMP read-only community string for your routers. The script assumes that the same community string is valid on all devices.

The script systematically queries each router in a list, one after another. It expects to find this list in a file called RTR_LIST, located in the working directory. This router list should contain a single router name or IP address on each line with no comments or other information included. The results of the script will be stored in a file called RESULT.csv, also located in the working directory.

You can then import the RESULT.csv file into a spreadsheet. The results will look similar to the example report shown in Table 2-3.

Table 2-3. An example output of the netstat.pl script

Detroit            
Interface IP-Address Mask MTU Speed Admin Oper
Serial0/0 10.1.1.1 255.255.255.252 1,500 768 Kb/s up up
Loopback0 10.2.2.2 255.255.255.252 1,514 0 Kb/s up up
FastEthernet1/0 172.22.1.4 255.255.255.0 1,500 100 Mb/s up up
Ethernet0/0 172.25.1.8 255.255.255.0 1500 10Mb/s down down
BRI0 10.1.99.55 255.255.255.0 1500 64 Kb/s down down
Ethernet0 172.25.1.7 255.255.255.0 1500 10 Mb/s up up
Loopback0 172.25.25.6 255.255.255.255 1514 0 Mb/s up up
Boston            
Interface IP-Address Mask MTU Speed Admin Oper
Serial0.1 172.20.1.2 255.255.255.252 0 28 Kb/s up up
Ethernet0 172.20.10.1 255.255.255.0 1500 10 Mb/s up up
Loopback0 172.20.100.1 255.255.255.255 1514 0 Mb/s up up

The script captures information about every interface that has been configured with an IP address. This includes interfaces that are administratively or operationally down, loopback interfaces, HSRP addresses, and IP unnumbered interfaces. The script will not display any interfaces or sub interfaces that are not configured for IP.

As a side benefit, this script uses only open standard SNMP MIBs. So you can use it to extract IP interface information from almost any SNMP-enabled device, including nonCisco equipment.

See Also

Appendix A





Cisco IOS Cookbook
Cisco IOS Cookbook (Cookbooks (OReilly))
ISBN: 0596527225
EAN: 2147483647
Year: 2004
Pages: 505
Simiral book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net