Originally published October 7, 2019 @ 2:42 pm

I needed to check if SNMP was accessible on whatever live servers that existed in a particular subnet. Here’s a quick script to do this.

You will need to specify subnet information and the SNMP community string. The script will use nmap to find live hosts and will use snmpwalk running via xargs to query multiple hosts at once. The end result should look something like this:

192.168.122.1  192.168.122.1       no
192.168.122.2  192.168.122.2       no
192.168.122.3  ncc1701.jedi.local  yes

And here’s the script

subnet=192.168.122.0; subnetmask=24
commstr="vvrblfcctcbhrgbhincdggrjhb"; export commstr
threads=$(grep -c processor /proc/cpuinfo) && (( threads = threads * 10 )) || threads=12
snmpcheck() {
  echo -ne "\t$(host -4 -t A -W1 -s  2>/dev/null | awk '{print $NF}' | \
  sed -r "s/.*DOMAIN.*//g")\t$(snmpwalk -c ${commstr} -v 2c  2>/dev/null | head -1 | wc -l)\n" | \
  awk '$3!=1 {print $1,$2,"no";next}; $3=1{print $1,$2,"yes";next};{print $0}'
} && export -f snmpcheck
nmap -sn ${subnet}/${subnetmask} -oG - | awk '$4=="Status:" && $5=="Up" {print $2}' | \
xargs -n1 -P${threads} bash -c 'snmpcheck "$@"' _ | sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 | column -t