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 that you can easily import the information into a spreadsheet.
#!/usr/local/bin/perl # # netstat.pl -- a script to build a detailed IP interface # listing directly from a list of routers. # #Set behavior $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 (<RTR>) { chomp($rtr="$_"); @ifIndex=`$snmpwalk $rtr .1.3.6.1.2.1.4.20.1.2`; @ipAddress=`$snmpwalk $rtr .1.3.6.1.2.1.4.20.1.1`; @ipMask=`$snmpwalk $rtr .1.3.6.1.2.1.4.20.1.3`; $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=`$snmpget $rtr ifDescr.$ifnum`; $ifMTU=`$snmpget $rtr ifMtu.$ifnum`; $ifSpeed=`$snmpget $rtr ifSpeed.$ifnum`; $ifAdminstatus=`$snmpget $rtr ifAdminStatus.$ifnum`; $ifOperstatus=`$snmpget $rtr ifOperStatus.$ifnum`; 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);
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 to be in the /usr/local/bin directory. For more information on Perl or NET-SNMP, 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 are 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.
Detroit | ||||||
---|---|---|---|---|---|---|
Interface |
IP Address |
Mask |
MTU |
Speed |
Admin |
Oper |
Serial0/0 |
10.1.1.1 |
255.255.255.252 |
1500 |
768Kb/s |
up |
up |
Loopback0 |
10.2.2.2 |
255.255.255.252 |
1514 |
0Kb/s |
up |
up |
FastEthernet1/0 |
172.22.1.4 |
255.255.255.0 |
1500 |
100Mb/s |
up |
up |
Ethernet0/0 |
172.25.1.8 |
255.255.255.0 |
1500 |
10Mb/s |
down |
down |
Toronto | ||||||
Interface |
IP Address |
Mask |
MTU |
Speed |
Admin |
Oper |
BRI0 |
10.1.99.55 |
255.255.255.0 |
1500 |
64Kb/s |
down |
down |
Ethernet0 |
172.25.1.7 |
255.255.255.0 |
1500 |
10Mb/s |
up |
up |
Loopback0 |
172.25.25.6 |
255.255.255.255 |
1514 |
0Kb/s |
up |
up |
Boston | ||||||
Interface |
IP Address |
Mask |
MTU |
Speed |
Admin |
Oper |
Serial0.1 |
172.20.1.2 |
255.255.255.252 |
0 |
28Kb/s |
up |
up |
Ethernet0 |
172.20.10.1 |
255.255.255.0 |
1500 |
10Mb/s |
up |
up |
Loopback0 |
172.20.100.1 |
255.255.255.255 |
1514 |
0Kb/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 non-IP interfaces or subinterfaces.
Because this script uses only open standard SNMP MIBs, you can use it to extract IP interface information from almost any SNMP-enabled device, including non-Cisco equipment.
Top |