Generating a Report of Interface Information

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.2\Q;
@ipAddress=\Q$snmpwalk $rtr .1.3.6.1.2.1.4.20.1.1\Q;
@ipMask=\Q$snmpwalk $rtr .1.3.6.1.2.1.4.20.1.3\Q;
$arraynum=0;
print CSV "\n$rtr\n";
print CSV "Interface, IP-Address, Mask, MTU, Speed, Admin, Operational\n";
for $ifnumber (@ifIndex) {
chomp(($foo, $ifnum) = split(/= /, $ifnumber));
$ifDescription=\Q$snmpget $rtr ifDescr.$ifnum\Q;
$ifMTU=\Q$snmpget $rtr ifMtu.$ifnum\Q;
$ifSpeed=\Q$snmpget $rtr ifSpeed.$ifnum\Q;
$ifAdminstatus=\Q$snmpget $rtr ifAdminStatus.$ifnum\Q;
$ifOperstatus=\Q$snmpget $rtr ifOperStatus.$ifnum\Q;
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\n";
$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