Extracting Hardware Inventory Information

Problem

You need an up-to-date list of the hardware configurations and IOS levels of all of your routers.

Solution

The Bourne Shell script in Example 1-3 uses SNMP to extract useful version information from a list of routers. By default, the script will store this data in CSV format so you can easily import it into a spreadsheet for analysis. No arguments are required or expected.

Example 1-3. inventory.sh

#!/bin/sh
#
#    inventory.sh -- a script to extract valuable information
#                    from a list of routers. (Name, Type, IOS version)       
#
#
# Set behaviour
public="ORARO"
workingdir="/home/cisco"
#
LOG=$workingdir/RESULT.csv
infile=$workingdir/RTR_LIST
snmp="/usr/local/bin/snmpget -v1 -c $public"
#
while read device
do
  $snmp $device sysName.0 > /dev/null
  if [ "$?" = "0" ] ; then
    rtr=\Q$snmp $device .1.3.6.1.4.1.9.2.1.3.0 | cut -f2 -d\" \Q
    type2=\Q$snmp $device .1.3.6.1.4.1.9.9.25.1.1.1.2.3 | cut -f2 -d$ \Q
    ios=\Q$snmp $device .1.3.6.1.4.1.9.9.25.1.1.1.2.5 | cut -f2 -d$ \Q
    prot=\Q$snmp $device .1.3.6.1.4.1.9.9.25.1.1.1.2.4 | cut -f2 -d$ \Q
    echo "$device, $rtr, $type2, $ios, $prot" >> $LOG                 
  fi
done < $infile

Discussion

The inventory.sh script extracts hardware and IOS version information directly from the routers using SNMP. This ensures that the data is up to date. You can even automate this script so it runs periodically to make sure that your inventory information is always accurate. In a large network, this is much easier than keeping track of this information manually.

By default, the script captures the device name, router type, IOS version, and IOS feature set from each router. It stores this information gathered in a CSV format file called RESULT.csv.

This script requires NET-SNMP to gather the information via SNMP. You could use a different SNMP package if you prefer, but then you will need to modify the syntax appropriately. The script expects to find the executable snmpget in the directory / usr/local/bin. Again, if you keep this file in a different location, you will need to define the correct location in the variable snmp. For more information on NET-SNMP, see Chapter 17 or Appendix A.

Before running this script in your network, you will need to modify two variables. The first is the variable called public. This value must contain your read-only SNMP community string. The script assumes that you have the same community string on all of the routers in the list. The second variable that you will need to set is workingdir, which must contain the name of the directory that you wish to run the script from.

Finally, you will need to build a file called RTR_LIST that contains the names of all of your routers, with one name on each line. The script expects to find this file in the working directory.

The output of the script is a CSV file, which you can import into a spreadsheet to analyze and sort the results as required. Table 1-4 shows an example of the script’s output as it might look in a spreadsheet.

Table 1-4. Output results from the inventory.sh script

RouterRouter nameTypeIOS versionIOS feature set
TorontotorontoC250012.2(1d)ENTERPRISE|FIREWALL PLUS IPSEC 56
BostonbostonC260012.4(10)IP|VOICE
NewyorknewyorkC260012.2(31)ENTERPRISE PLUS
TampatampaC260012.4(10)IP|VOICE
SanfransanfranC250012.3(16)IP PLUS

Get Cisco IOS Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.