Clean up the SNMP_HELPER.PY function for Python 3.x
There are some differences in Python 2 vs. Python 3. One of those differences is that the print command now requires you to actually have parans () around the content that you wish to print. This was about the only thing that I had to do to get Kirk’s code working in Python 3. If you try to run the code in the python IDLE software it will come up with this error right away. I could also have run the py2to3 scripts, but since this was a small file, it was easy to just search for the 4 or so print statements and edit it manually as I was reading through the code to try and understand what Kirk was doing.
So Kirk takes you through the normal PIP installation. I’m performing this on OS X Mavericks with Python 3. So for those not familiar with the differences yet. Python 2.x is natively installed on OSX. If you do a pip install … command, this will result in you downloading and making that specific library available to the python 2.x version on your OS. Since I’m using python 3.x, I instead need to use the pip3 install command which will, instead, make the library you’re downloading available to python 3.x on your system
$pip3 install pysnmp
Note: Kirk has a couple of other ways to install the pysnmp library over on his blog, so I won’t repeat them here.
Testing Out SNMP
So it’s a good idea to ensure that SNMP is running and you have the right community strings on the machine you’re going to access. For this, I’m going to use an
SNMP MIB browser that I have installed on my MBA to test this out. You could also use the net-snap utilities as shown on Kirk’s blog if you’d like to do this from the CLI. I highly recommend getting a MIB Browser installed on your system. http://www.ireasoning.com has a nice free one available.
So now that we’ve confirmed this all works. on to the code.
Setting the Stage
So I’m assuming that you’re able to run the SNMP_Helper.py file in IDLE. If you look at the code, one of the first things it does is import the cmdgen method from the pysnmp library
“from pysnmp.entity.rfc3413.oneliner import cmdgen”
One of the ways that has really helped me learn is to go through other people’s code and try and understand exactly what they are doing. I don’t think I could have written SNMP_Helper.py on my own yet, but I can understand what it’s doing, and I can DEFINITELY use it. 🙂
Now we set up a few variables, using the exact same names that Kirk used over in his blog here
>>> COMMUNITY_STRING = ‘public’
>>> SNMP_PORT = 161
>>> a_device = (‘10.101.0.221’, COMMUNITY_STRING, SNMP_PORT)
Running the Code
Now we’ll run the exact same SNMP query against the sysDescr OID that Kirk used. And Amazingly enough, get a very similar output.
>> snmp_data = snmp_get_oid(a_device, oid=’.188.8.131.52.184.108.40.206.0′, display_errors=True)
It’s nice to see that we have gotten that same nasty output. SNMP is a standard after all and we should expect to see the same response from Cisco, HP, and other vendors devices when using standard SNMP functions, such as the MIBII sysDescr OIDs.
So now, let’s use Kirk’s cleanup function to be able to see what the data actually looks like. Again, remember Python3 needs those parens for the print statement to work properly.
>>> output = snmp_extract(snmp_data)
>>> print (output)
HP Comware Platform Software, Software Version 5.20.99 Release 2221P05
HP A5500-24G-PoE+ EI Switch with 2 Interface Slots
Copyright (c) 2010-2014 Hewlett-Packard Development Company, L.P.
Just for giggles, I also used this code against my Synology Diskstation
Linux DiskStation 220.127.116.11 #4482 Fri Apr 18 02:12:31 CST 2014 armv5tel
Then against my Server Technologies intelligent PDU
Sentry Switched CDU
Then against my DIGI console server.
>>> snmp_data = snmp_get_oid(a_device, oid=’.18.104.22.168.22.214.171.124.0′, display_errors=True)
error_message No SNMP response received before timeout
The last one was working exactly as expected as I have ACL’s in place to only allow SNMP access from certain devices in my network. 🙂
It’s nice to see that standards like SNMP and widely available libraries like pysnmp can be used to access the devices regardless of the vendor they come from.
SNMP gets a bad wrap in general as there are new cooler technologies out there like NETCONF, OpenFlow, OVSDB, NetFlow, sFlow, and I’m sure a dozen others that I’m missing that can do a better job of the functions that SNMP was originally designed to go after.
But sometimes, SNMP is what we have, and the reason that it’s still around after all these years is that it’s “good enough”
Questions or comments? please post below!