Simple Cisco switch inventory with bash and snmp

Scripts, usually I write some because I don’t like repetitive tasks and I’m lazy, meaning I prefer automation over useless hard work.

Don’t know where I found this quote but I like it:

Don't spend your time doing work a well-trained monkey could do.

Today’s request was quite simple: get model and serial number from a bunch of Cisco switches.

I now NEDI, Observium and LibreNMS can do that but I preferred to write a quick script I could use as a one shot tool instead of a complete software solution.

Requirements

The script reads from a file that contains a list of IP addresses, all the devices must have a snmp community with read priviledge.

The Script

The script is quite readable: for each line of the input file it does some snmp read operations and print the result when finished.

Sed commands are used to clean the output of the snmp output.

Replace variable community with the correct value for your environment.

#!/bin/bash
community=MYCOMMUNITY
while IFS='' read -r line || [[ -n "$line" ]]; do
hostname=$(snmpwalk -v 2c -c $community -O v -L n $line 1.3.6.1.4.1.9.2.1.3 | sed 's/^.*://g' | sed 's/"//g')
model=$(snmpwalk -v 2c -c $community -O v -L n $line 1.3.6.1.2.1.47.1.1.1.1.13.1001 | sed 's/^.*://g' | sed 's/"//g')
serial=$(snmpwalk -v 2c -c $community -O v -L n $line 1.3.6.1.4.1.9.5.1.2.19 | sed 's/^.*://g' | sed 's/"//g')
echo -e $line'\t'$hostname'\t'$model"\t"$serial
done < "$1"

Launch with:

./snmpinventory.sh switchlist.txt

where file switchlist.txt is a simple list of IP addresses.

Final result

The final result is a list of values separated by a tab os it is easy to copy and paste in a spreadsheet.

hostname    model   serial

I added this script to my GitHub repository.

Enjoy!

 
comments powered by Disqus