Background: I plan to design and implement a controller-based QoS-Solution with distributed control-plane using SNMP and RMON.
How to send SNMP-Requests in Python?
Why Python? It’s an arbitrary decision, every programming language might be sufficient. For the moment, I prefer Python for new projects.
I decided to try Easy-SNMP, since good performance and a nice python-programming-interface seem to be killer-features for me.
EasySNMP homepage
EasySNMP documentation
A development environment
- clone an ubuntu-server VM
https://allones.de/2017/11/17/linux-quick-and-âŠab-vm-deployment/
- install net-snmp
sudo apt-get install libsnmp-dev snmp-mibs-downloader
- install gcc, python
sudo apt-get install gcc python-dev python-pip
- install EasySNMP
pip install easysnmp
Enable SNMP on a Router
Never ever enable SNMP-Access for everybody, don’t even think about it.
Use an ACL permitting only the SNMP-Manager.
ip access-list standard ACL_SNMP
permit host 192.168.2.89
snmp-server community READ ro ACL_SNMP
! just as an example System-Variable
snmp-server location allones.de
Access the Router via SNMP
The router’s LAN-IP is 192.168.2.72.
I like the „Session“-Interface:
user@snmp-server:~$ python
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from easysnmp import Session
>>>
>>> session = Session(hostname='192.168.2.72', community='READ', version=2)
Be aware to poll the Object-Instance, not the Object-Tree-Position.
Wrong
>>> location = session.get('sysLocation')
>>> print location
<SNMPVariable value='NOSUCHINSTANCE' (oid='sysLocation', oid_index='', snmp_type='NOSUCHINSTANCE')>
Correct: Specify an Instance-ID
>>>
>>> location = session.get('sysLocation.0')
>>> print location
<SNMPVariable value='allones.de' (oid='sysLocation', oid_index='0', snmp_type='OCTETSTR')>
Works!
How to access the return-value?
How to verify the Datatype?
I’ve been using the Cisco-developed TCL-Interface for years, the „snmp_getone“-command there, where you had to parse the return-value (a string similar to the „location“-Variable above) applying regular expressions… Weird.
Just remembering the blog post, which motivated me to learn TCL… Ten years old, how time flies!
Read: „SNMP with TCL“ by Ivan Pepelnjak
Might this be easy with EasySNMP?
>>> print location.value
allones.de
>>> print location.oid
sysLocation
>>> print location.oid_index
0
>>> print location.snmp_type
OCTETSTR
Isn’t EasySNMP beautiful!? đ