Using SNMP to Extract Inventory Information from a List of Routers


You want to build a report of important router information for all of your managed routers.


The following Perl script extracts important router informationsuch as router name, physical location, contact name, and serial number from a list of routersand creates a report of this information. The script is intended to be run manually, and no arguments are required or expected.

Here's some example output:

Freebsd% ./
 Router Location Contact Serial
Router 999 Queen St. W., Toronto, Ont Ian Brown 416-555-2943 JAX123456 
Boston 1273 Main Street, Boston, MA Bob Irwin 800-555-1221 JAX231567 
Denver 24 Sussex Drive, Denver, CO Helpdesk 800-555-2992 JAX928362 
Frame 999 Queen St. W., Toronto, Ont Ian Brown 416-555-2943 JAX212321 
Toronto 999 Queen St. W., Toronto, Ont Ian Brown 416-555-2943 JAX283291 
Boston2 1273 Main Street, Boston, MA Bob Irwin 800-555-1221 JAX292228 
Denver2 24 Sussex Drive, Denver, CO Helpdesk 800-555-2992 JAX219115 

The Perl code follows in Example 17-1.

Example 17-1.

# -- a script to extract valuable information
# from a Router. (Name, Location, Contact, S/N)
# Set behavior
open (RTR, "$rtrlist") || die "Can't open $rtrlist file";
open (LOG, ">$workingdir/RESULT") || die "Can't open $workingdir/RESULT file";
printf " Router		 Location			Contact		 Serial
printf LOG " Router		; Location			;Contact		 ;Serial
while () {
 $snmpget="/usr/local/bin/snmpget -v1 c $snmpro $rtr ";
 $rtr=Q$snmpget .;
 $loc=Q$snmpget .;
 $con=Q$snmpget .;
 $sin=Q$snmpget .;
 chomp(($foo, $RTR) = split (/"/, $rtr));
 chomp(($foo, $LOC) = split (/= /, $loc));
 chomp(($foo, $CON) = split (/= /, $con));
 chomp(($foo, $SIN) = split (/"/, $sin));
 printf ("%-12.12s %-30.30s %-25.25s %-12.12s
", $RTR, $LOC, $CON, $SIN);
 printf LOG ("%-12.12s; %-30.30s; %-25.25s; %-12.12s
", $RTR, $LOC, $CON, $SIN);


This script extracts important router information from a list of routers and presents that list in a semicolon-delimited file, as well as displaying it on the screen. It is a very simple script that just extracts MIB variables from a list of routers using snmpget, illustrating the value and versatility of SNMP.

This Perl script works by cycling through a list of routers. For each router, it uses SNMP get requests to extract the value of four variables: router name, location, contact, and serial number, as we did manually in Recipe 17.3. The core of this script is NET-SNMP's snmpget utility, which extracts SNMP data from the list of routers. NET-SNMP must be present on the server, and snmpget must reside in the /usr/local/bin directory before this script will function.

This script contains three important variables that you have to set correctly before it will run properly. First, the variable $workingdir contains the directory in which the script resides. Next is the read-only SNMP community string, which is stored in the script's $snmpro variable. Substitute your organization's SNMP read-only community string for the example value shown, ORARO. Note that this script assumes that you use the same SNMP read-only community string on all routers.

The third variable, $rtrlist, contains the location of the router list. The example script uses a file called RTR_LIST, located in the working directory. You will need to change this variable to point to a file containing a list of routers on your network. The script expects this file to have a single router name per line.

The script produces two types of output. It displays the results on your screen while the script is executing. And the script also logs all results to a file called RESULT that resides in the working directory. The script automatically creates this file the first time you run it, and it overwrites the contents on each subsequent execution.

See Also

Recipe 17.3

Router Configuration and File Management

Router Management

User Access and Privilege Levels


IP Routing





Frame Relay

Handling Queuing and Congestion

Tunnels and VPNs

Dial Backup

NTP and Time


Router Interfaces and Media

Simple Network Management Protocol





First Hop Redundancy Protocols

IP Multicast

IP Mobility




Appendix 1. External Software Packages

Appendix 2. IP Precedence, TOS, and DSCP Classifications


Cisco IOS Cookbook
Cisco IOS Cookbook (Cookbooks (OReilly))
ISBN: 0596527225
EAN: 2147483647
Year: 2004
Pages: 505 © 2008-2020.
If you may any questions please contact us: