Using SNMP to Extract Inventory Information from a List of Routers
Problem
You want to build a report of important router information for all of your managed routers.
Solution
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% ./inventory.pl
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
Freebsd%
The Perl code follows in Example 17-1.
Example 17-1. inventory.pl
#!/usr/bin/perl |
Discussion
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.