Generating a Report of Interface Information


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


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.

# -- a script to build a detailed IP interface 
# listing directly from a list of routers. 
#Set behavour
$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 () {
 @ifIndex=Q$snmpwalk $rtr .;
 @ipAddress=Q$snmpwalk $rtr .;
 @ipMask=Q$snmpwalk $rtr .;
 print CSV "
 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


The 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 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 script

Interface IP-Address Mask MTU Speed Admin Oper
Serial0/0 1,500 768 Kb/s up up
Loopback0 1,514 0 Kb/s up up
FastEthernet1/0 1,500 100 Mb/s up up
Ethernet0/0 1500 10Mb/s down down
BRI0 1500 64 Kb/s down down
Ethernet0 1500 10 Mb/s up up
Loopback0 1514 0 Mb/s up up
Interface IP-Address Mask MTU Speed Admin Oper
Serial0.1 0 28 Kb/s up up
Ethernet0 1500 10 Mb/s up up
Loopback0 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 © 2008-2017.
If you may any questions please contact us: