我正在尝试将单个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?
谢谢!
答案 0 :(得分:2)
nextCmd
返回相对于给定的 next OID。它永远不会返回给定OID的值。
如果您需要查询特定的OID,则应使用getCmd
函数而不是nextCmd
。