To play with SNMP using python install easysnmp python module.
pip3 install easysnmp
Collecting easysnmp
Downloading easysnmp-0.2.5.tar.gz (67 kB)
|████████████████████████████████| 67 kB 148 kB/s
Installing collected packages: easysnmp
Running setup.py install for easysnmp ... done
Successfully installed easysnmp-0.2.5
SNMP v2
Let us take a sample device
Device IP: 192.168.0.25
Public Community: testPub
sysDescr OID: 1.3.6.1.2.1.1.1
Poll device using snmpwalk to get sysDescr value
snmpwalk -v 2c -c testPub 192.168.0.25 1.3.6.1.2.1.1.1
SNMPv2-MIB::sysDescr.0 = STRING: Cisco IOS XR
Python code to get same
#!/usr/local/bin/python3
from easysnmp import Session
hostname = "192.168.0.25"
community = "testPub"
session = Session(hostname=hostname, community=community, version=2)
print(session.get('.1.3.6.1.2.1.1.1.0'))
Save above code as pysnmpv2.py and execute
./pysnmpv2.py
<SNMPVariable value='Cisco IOS XR Software
SNMP v3
Lets take a sample device and its SNMP v3 parameters
DeviceIP: 192.168.0.25
username : testuser
SHA : shaToken
AES128 : aesToken
sysDescr OID: 1.3.6.1.2.1.1.1
First using command line you can verify retrieval of sysDescr value using snmpwalk
snmpwalk -v3 -u testuser -l authPriv -a SHA -A shaToken -x AES -X aesToken 192.168.0.25 1.3.6.1.2.1.1.1
SNMPv2-MIB::sysDescr.0 = STRING: Cisco IOS XR Software...
Now, using easysnmp module you can poll device to get its sysdescr (1.3.6.1.2.1.1.1) OID value
#!/usr/local/bin/python3
from easysnmp import Session
hostname = "192.168.0.25"
security_username = "testuser"
auth_password = "shaToken"
privacy_password = "aesToken"
session3 = Session(hostname=hostname, version=3, security_level="auth_with_privacy", security_username=security_username, auth_protocol="SHA", auth_password=auth_password, privacy_protocol="AES", privacy_password=privacy_password)
print(session3.walk("1.3.6.1.2.1.1.1"))
Save above code as pysnmp.py and execute….
./pysnmp.py
[<SNMPVariable value='Cisco IOS XR Software...