Bash Script to Gather All Your Cisco Serial Numbers via SNMP

Posted: by SamDobs in Labels:
0

Handy Script from Eric Flores @ Packetpushers.net.

Recently, I was doing a true up of our Cisco SmartNet contract, and had to get the serial numbers from all of the Cisco devices on the network. Rather than access each device manually, I wrote a bash script that would get the serial numbers via the SNMP ENTITY table.
To use the script, you will need:
  1. A Linux distro with net-snmp-utils installed.
  2. The ENTITY-MIB.
  3. The location of the ENTITY-MIB specified in the variable ENTITY on line 13 of the script.
The command syntax is (which you can also get from running “./getserial.sh -–help” ) –
./getserial.sh [community] [list] [report_file.csv]
Parameters are as follows-
community – RO community string for devices
list – text file with the list of DNS name or IP addresses for each device – one device per line
report_file.csv – the output CSV file for the report
The script -
#!/bin/bash
# Help prompt
if [ -n "$1" ] && [ $1 == "--help" ]; then
echo -e “\nCommand usage:”
echo -e “./getserial.sh [community] [list] [report_file.csv]\n”;
exit 1
fi
#Location of ENTITY-MIB
ENTITY=”/usr/share/snmp/mibs/ENTITY-MIB.txt”
# Set VARs
COMMUNITY=”$1″
LIST=`cat $2`
REPORT=”$3″
# Create CSV file and set header
echo -e “Device Description,Device Type,Serial Number,Model Number,Other\n” > $REPORT
# Loop through devices in list
for DEVICE in $LIST ; do {
# Get hostname from device
HOST=$(snmpget -v2c -c $COMMUNITY -Oqv $DEVICE SNMPv2-MIB::sysName.0)
# Add hostname and device name to csv
echo -e “$HOST,$DEVICE” >> $REPORT
# Echo status to stdout
echo -e “Querying device: $DEVICE – Hostname: $HOST”
# Querry ENTITY table on device cut only entPhysicalDescr, entPhysicalClass, entPhysicalSerialNum, entPhysicalModelName
# and entPhysicalAlias (entPhysicalAlias somtimes has serial number of chassis on router and model number on Nexus) colums
# and sed to remove top 3 lines of output. Input into a var TABLE
TABLE=$(snmptable -v2c -c $COMMUNITY -m +$ENTITY -Cf , $DEVICE 1.3.6.1.2.1.47.1.1.1 | cut -d “,” -f 1,4,10,12,13 | sed -n ’1,3d;p’)
# Get line numbers that only have an entry in entPhysicalSerialNum column and input into var LINES
LINES=$(echo -e “$TABLE” | cut -sd “,” -f 3 | grep -n . | cut -d : -f 1)
# Loop through line numbers in var LINES. Echo TABLE into sed and grab lines from var LINES.
# Append to csv file
for i in $LINES; do {
echo -e “$TABLE” | sed -n `echo -e “$i”`p >> $REPORT 2> /dev/null
}
done
# Add line between devices in csv
echo >> $REPORT
}
done
echo -e “\nSNMP Qeuries Completed.\n”
The output file will look like this (with serial numbers masked) -
Device Description,Device Type,Serial Number,Model Number,Other
firewall1.yourdomain.com
ASA 5520 Adaptive Security Appliance,chassis,JM#########,ASA5520,
ASA 5500 Series Security Services Module-20,module,JA########,ASA-SSM-20,
switch2.yourdomain.com
WS-C3750X-48,chassis,FD########,WS-C3750X-48T-S,
FRU Power Supply,powerSupply,LI########,C3KX-PWR-350WAC ,
WS-C3750X-48,chassis,FD#########,WS-C3750X-48T-S,
FRU Power Supply,powerSupply,LIT##########,C3KX-PWR-350WAC ,
router1.yourdomain.com
CISCO3925-CHASSIS,chassis, RELEASE SOFTWARE (fc1), RELEASE SOFTWARE (fc1),FTX########
Cisco Services Performance Engine 100 for Cisco 3900 ISR,module,FOC########,C3900-SPE100/K9,
8 Port GE Non-POE EHWIC Switch,module,FOC########,EHWIC-D-8ESG,
C3900 AC Power Supply 2,powerSupply,QC########,PWR-3900-AC,
1000BASE-LX SFP,module,FNS########,FTLF1318P2BCL-C3A,
C3900 AC Power Supply 1,powerSupply,QCS########,PWR-3900-AC,
nexusswitch1.yourdomain.com
Fabric [VPC domain:100],stack,FOX########, Inc.,N5K-C5596UP
48X10GE/Modular Supervisor in Fixed Module-1,module,JAF########, Inc.,N5K-C5596UP
16 port L3 GEM,module,JAF########, Inc.,N55-M160L3
Nexus5596 Chassis,chassis,FOX########, Inc.,N5K-C5596UP
PowerSupply-1 Fan-1,fan,POG########, Inc.,N55-PAC-1100W
PowerSupply-1 Fan-2,fan,POG########, Inc.,N55-PAC-1100W
PowerSupply-2 Fan-1,fan,POG########, Inc.,N55-PAC-1100W
PowerSupply-2 Fan-2,fan,POG########, Inc.,N55-PAC-1100W
You can then open the file up with any application that can parse a CSV, such as Microsoft Excel. For some reason, Cisco router chassis’, Nexus switches, and most SFPs don’t follow the MIB. Since they put serial and model numbers in the entPhysicalAlias column instead of entPhysicalSerialNum, you will find them in the “Other” field of the CSV.

0 comments: