我正在尝试将单个 OID 的值放入变量中。但是,我只能使用 pysnmp 来遍历它。
我想获取 OID 1.3.6.1.4.1.2.3.51.2.2.7.1.0
的值,如果我使用 SNMP 工具对其进行测试,它会返回值 255。
使用此代码我没有得到任何输出:
def getsnmp(host, oid):
for (errorIndication,
errorStatus,
errorIndex,
varBinds) in nextCmd(SnmpEngine(),
CommunityData('public', mpModel=0),
UdpTransportTarget((host, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)),
lookupMib=False,
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:
for varBind in varBinds:
print(' = '.join([x.prettyPrint() for x in varBind]))
getsnmp('10.100.11.30', '1.3.6.1.4.1.2.3.51.2.2.7.1.0')
但是,如果我删除最后一个 .0,则会得到结果:
1.3.6.1.4.1.2.3.51.2.2.7.1.0 = 255
如何无需步行即可直接访问特定的 OID?
谢谢!
最佳答案
nextCmd
返回相对于给定 OID 的下一个 OID。它永远不会返回给定 OID 的值。
如果您需要查询特定的OID,您应该使用getCmd
函数而不是nextCmd
。
关于python - 使用 pysnmp 获取单个 SNMP 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51965316/