In short,
I am utilizing pysnmp to get multiple values to create a .CSV containing routing table information.
When I do an SNMPWALK I on OID: .1.3.6.1.2.1.4.20.1.3 I get:
IP-MIB::ipAdEntNetMask.10.30.0.0 = IpAddress: 255.255.255.254
IP-MIB::ipAdEntNetMask.10.30.0.2 = IpAddress: 255.255.255.254
IP-MIB::ipAdEntNetMask.10.30.0.14 = IpAddress: 255.255.255.254
IP-MIB::ipAdEntNetMask.10.30.0.18 = IpAddress: 255.255.255.254
IP-MIB::ipAdEntNetMask.10.30.0.22 = IpAddress: 255.255.255.254
IP-MIB::ipAdEntNetMask.10.30.0.26 = IpAddress: 255.255.255.254
IP-MIB::ipAdEntNetMask.10.30.0.30 = IpAddress: 255.255.255.254
IP-MIB::ipAdEntNetMask.10.30.0.32 = IpAddress: 255.255.255.254
IP-MIB::ipAdEntNetMask.10.30.0.65 = IpAddress: 255.255.255.248
IP-MIB::ipAdEntNetMask.10.30.0.97 = IpAddress: 255.255.255.248
IP-MIB::ipAdEntNetMask.10.30.0.128 = IpAddress: 255.255.255.255
IP-MIB::ipAdEntNetMask.127.0.0.50 = IpAddress: 255.0.0.0
While utilizing the same method in my pysnmp script, I get the following output:
-> snmpNetMAsk = (snmpDictClean(snmpNetMAsk, 10))
(Pdb) pprint.pprint(snmpNetMAsk)
[[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.0)),
IpAddress(hexValue='fffffffe'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.2)),
IpAddress(hexValue='fffffffe'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.14)),
IpAddress(hexValue='fffffffe'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.18)),
IpAddress(hexValue='fffffffe'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.22)),
IpAddress(hexValue='fffffffe'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.26)),
IpAddress(hexValue='fffffffe'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.30)),
IpAddress(hexValue='fffffffe'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.32)),
IpAddress(hexValue='fffffffe'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.65)),
IpAddress(hexValue='fffffff8'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.97)),
IpAddress(hexValue='fffffff8'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.10.30.0.128)),
IpAddress(hexValue='ffffffff'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.3.127.0.0.50)),
IpAddress(hexValue='ff000000'))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.0)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.2)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.14)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.18)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.22)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.26)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.30)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.32)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.65)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.97)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.10.30.0.128)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.4.127.0.0.50)), Integer(1))],
[(MibVariable(ObjectName(1.3.6.1.2.1.4.20.1.5.10.30.0.0)), Integer(18024))]]
As you can clearly see, all of my values are in HEX, and then copied to some Interger. I do not fully understand what is going on here. It seems easier to just run an snmpwalk in the terminal and capture it to a file, then edit the file with a python script. If there is a way to maintain the values in a dictionary using pysnnmp, I would much rather do that. I only ask this after days of searching on my own. Thanks for any help!
EDIT: Showing a full version of the script I am using.
#!/usr/bin/python
import pprint
import pysnmp
from snmp_helper import *
def snmpDictClean(input_list, input_num):
output_dict = dict()
for varBindTableRow in input_list:
for name, val in varBindTableRow:
name = str(name)
val = str(val)
splitKey = name.split('.')
del splitKey[0:input_num]
splitKey = '.'.join(splitKey)
output_dict[splitKey] = val
return output_dict
def main():
ipCidrRouteIfIndex = '.1.3.6.1.2.1.4.24.4.1.5'
ifDescr = '.1.3.6.1.2.1.2.2.1.2'
ipAdEntIfIndex = '.1.3.6.1.2.1.4.20.1.2'
ipAdEntNetMask = '.1.3.6.1.2.1.4.20.1.3'
# SNMPv3 Connection Parameters
a_user = 'USER'
auth_key = 'AUTH_KEY'
encrypt_key = 'ENCRYPT_KEY'
snmp_user = (a_user, auth_key, encrypt_key)
ipAddr = [
'host1',
'host2',
]
for hlist in ipAddr:
snmpDevice = (hlist, 161)
snmpRoute = snmp_bulk_oid_v3(snmpDevice, snmp_user, oid=ipCidrRouteIfIndex)
snmpRoute = (snmpDictClean(snmpRoute, 11))
snmpIfDescr = snmp_bulk_oid_v3(snmpDevice, snmp_user, oid=ifDescr)
snmpIfDescr = (snmpDictClean(snmpIfDescr, 10))
snmpIpIndex = snmp_bulk_oid_v3(snmpDevice, snmp_user, oid=ipAdEntIfIndex)
snmpIpIndex = (snmpDictClean(snmpIpIndex, 10))
snmpNetMAsk = snmp_bulk_oid_v3(snmpDevice, snmp_user, oid=ipAdEntNetMask)
snmpNetMAsk = (snmpDictClean(snmpNetMAsk, 10))
import pdb; pdb.set_trace()
if __name__ == '__main__':
main()
I am using snmp_helper from @ktbyers: https://github.com/ktbyers/pynet/tree/master/snmp. I modified it to use snmpbulk instead of get.
# Python
from __future__ import print_function
from pysnmp.entity.rfc3413.oneliner import cmdgen
import pprint
def snmp_bulk_oid_v3(snmp_device, snmp_user, oid='', auth_proto='sha',
encrypt_proto='des', display_errors=True):
# unpack snmp_user
a_user, auth_key, encrypt_key = snmp_user
auth_proto_map = {
'sha': cmdgen.usmHMACSHAAuthProtocol,
'md5': cmdgen.usmHMACMD5AuthProtocol,
'none': cmdgen.usmNoAuthProtocol
}
if auth_proto in auth_proto_map.keys():
auth_protocol = auth_proto_map[auth_proto]
else:
raise ValueError("Invalid authentication protocol specified: %s" % auth_proto)
encrypt_proto_map = {
'des': cmdgen.usmDESPrivProtocol,
'3des': cmdgen.usm3DESEDEPrivProtocol,
'aes128': cmdgen.usmAesCfb128Protocol,
'aes192': cmdgen.usmAesCfb192Protocol,
'aes256': cmdgen.usmAesCfb256Protocol,
'none': cmdgen.usmNoPrivProtocol,
}
if encrypt_proto in encrypt_proto_map.keys():
encrypt_protocol = encrypt_proto_map[encrypt_proto]
else:
raise ValueError("Invalid encryption protocol specified: %s" % encrypt_proto)
# Create a PYSNMP cmdgen object
cmdGen = cmdgen.CommandGenerator()
(errorIndication, errorStatus, errorIndex, varBindTable) = cmdGen.bulkCmd(
cmdgen.UsmUserData(a_user, auth_key, encrypt_key,
authProtocol=auth_protocol,
privProtocol=encrypt_protocol, ),
cmdgen.UdpTransportTarget(snmp_device),
0,
25,
oid,
lookupNames=True, lookupValues=True
)
if errorIndication:
print(errorIndication)
else:
if errorStatus:
print('%s at %s' % (
errorStatus.prettyPrint(),
errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
)
)
'''
else:
for varBindTableRow in varBindTable:
for name, val in varBindTableRow:
snmp_data = print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
'''
return varBindTable
What you see is vanilla OID-value pairs as received from SNMP Agent. To get prettier output with pysnmp you need to get it passing received OID-value pairs through MIB resolver. Use this example as a prototype but additionally load IP-MIB like this:
...
errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
cmdgen.CommunityData('public'),
cmdgen.UdpTransportTarget(('demo.pysnmp.com', 161)),
MibVariable('1.3.6.1.2.1.4.20.1.3').loadMibs('IP-MIB'),
lookupNames=True, lookupValues=True
)
...
Make sure to use .prettyPrint() for printing out OIDs/values.
Keep in mind that you need conventional ASN.1 IP-MIB converted into pysnmp format to make it usable by pysnmp. You could do that with (yet experimental) pysmi MIB compiler or with build-pysnmp-mib script shipped with pysnmp or just install a pre-built MIB collection from PyPI (pip pysnmp-mibs).
If you get your own IP-MIB.py, rather than using pysnmp-mibs package, you could put your IP-MIB.py into a directory and point pysnmp to that custom location through MibVariable.addMibSource() method. See this example for more information. All MibVariable methods could be chained like this:
MibVariable('1.3.6.1.2.1.4.20.1.3').addMibSource('/etc/pymibs').loadMibs('IP-MIB')
or you could skip .loadMibs() by specifying MIB name to load in initialiser:
MibVariable('IP-MIB', 'ipRoute').addMibSource('/etc/pymibs')
The MibVariable class is polymorphic to ObjectIdentifier, so those two could be used interchangeably. But MibVariable could gather more information related to the OID it was initialised with from a MIB file. For instance MibVariable may contain MIB module name and MIB symbol name associated with the OID in question. It can also hold a SNMP data type of a value associated with that OID.
You can think of MibVariable as an implementation of SNMPv2-SMI's OBJECT-TYPE macro.