Created
February 19, 2019 18:02
-
-
Save grimpy/b63cf22e816c967e2f3240a522e0e74d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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