Script: Multiple SNMP Community String

From OpenNMS
Jump to: navigation, search

It has been asked on the list if ONMS could support searching through multiple community strings. While this could be done it is probably better to (a) manage your community strings better and (b) to have another utility tool that can search through the community strings you want and generate the proper snmp-config.xml file for you so that ONMS can focus on other things.

So in that light here is a quick and dirty shell script that will do the job.

First create a file called snmp-communities.list with each community you want test on a single line:

NOTE: the values for the snmp-communities.list & ip.list files are just examples. Make sure you put the proper values for your network in these files.



Then either use the file that OpenNMS builds (or at least use to build) in the {OPENNMS_HOME}/etc directory called "include" or create your own called ip.list. Again a single IP per line. (NOTE: you can always run a query against the database and have it dump the found nodes to a text file for you also... Whatever is your pleasure.)


Finally, cut and paste the following script to a file named whatever you want and make it executable. On the machine you run this on you need (a) access to every IP you want to test and (b) snmpwalk.

NOTE: running this script can get you in trouble with your IT staff if you are not authorized to do this. In some environments you will automatically lose your network access. Run with permission!


# Long drawn out SNMP community finder...
# Mike Coakley
# Oct 2006
# v1.0
# Software ASIS... Use with caution


echo '<!-- Autogenerate snmp-config.xml file -->' > $SNMP_FILE
echo "<?xml version=\"1.0\"?>" >> $SNMP_FILE
echo "<snmp-config retry=\"4\" timeout=\"800\" version=\"v2c\">" >> $SNMP_FILE

for ip in $(cat $IP_FILE); do
    echo "Trying $ip..." 
    for com in $(cat $COMMUNITY_FILE); do
        echo "  with community $com..."
        NAME=$(snmpwalk -c $com -v1 $ip sysName 2> /dev/nul | awk -F ":" ' { print $4 } ')
        if [ "$NAME" != "" ]; then
            echo "  Found $NAME with $com against $ip using SNMP v1"
            echo "  ... trying v2c"
            NAME=$(snmpwalk -v2c -c $com $ip sysName 2> /dev/nul | awk -F ":" ' { print $4 } ')
            if [ "$NAME" != "" ]; then
                echo "  Found $NAME with $com against $ip using SNMP v2c"
                echo "  ** Going to create the definition using v2c (DEFAULT)"

                echo "  <definition read-community=\"$com\">" >> $SNMP_FILE
                echo "    <specific>$ip</specific>" >> $SNMP_FILE
                echo "  </definition>" >> $SNMP_FILE
                echo "  ** Going to create the definition using v1"
                echo "  <definition read-community=\"$com\" version=\"v1\">" >> $SNMP_FILE
                echo "    <specific>$ip</specific>" >> $SNMP_FILE
                echo "  </definition>" >> $SNMP_FILE
    if [ $FOUND = 0 ]; then
        echo "  No matching communities found for $ip"

echo "</snmp-config>" >> $SNMP_FILE

echo "Done searching for communities for ya! LATER!"