import pysnmp from pysnmp.hlapi import * def get_snmp_value(oid, ip, community): errorIndication, errorStatus, errorIndex, varBinds = next( getCmd(SnmpEngine(), CommunityData(community), UdpTransportTarget((ip, 161)), ContextData(), ObjectType(ObjectIdentity(oid))) ) if errorIndication: print(errorIndication) raise elif errorStatus: print('%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) raise else: if isinstance(varBinds[0][1], pysnmp.proto.rfc1905.NoSuchObject): return None value = varBinds[0][1] if isinstance(value, pysnmp.proto.rfc1902.Integer): return int(value)