Skip to content

Instantly share code, notes, and snippets.

@grimpy
Created February 19, 2019 18:02
Show Gist options
  • Save grimpy/b63cf22e816c967e2f3240a522e0e74d to your computer and use it in GitHub Desktop.
Save grimpy/b63cf22e816c967e2f3240a522e0e74d to your computer and use it in GitHub Desktop.
from pysnmp.hlapi import *
from collections import defaultdict
dataset = defaultdict(dict)
for (errorIndication,
errorStatus,
errorIndex,
varBinds) in nextCmd(SnmpEngine(),
CommunityData('public', mpModel=0),
UdpTransportTarget(('10.107.2.202', 161)),
ContextData(),
ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')),
ObjectType(ObjectIdentity('IF-MIB', 'ifType')),
ObjectType(ObjectIdentity('IF-MIB', 'ifMtu')),
ObjectType(ObjectIdentity('IF-MIB', 'ifSpeed')),
ObjectType(ObjectIdentity('IF-MIB', 'ifPhysAddress')),
ObjectType(ObjectIdentity('IF-MIB', 'ifType')),
lexicographicMode=False):
if errorIndication:
print(errorIndication)
break
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex)-1][0] or '?'))
break
else:
print('New cmd')
for (key, val) in varBinds:
keyname = key.prettyPrint()
value = val.prettyPrint()
field, index = keyname[8:].rsplit(".", 1)
dataset[index][field] = value
print(dataset)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment