如果我尝试这样的事情,我就得到了
ValueError:需要解包的值太多(预计为 4 个)
有人能解释一下为什么吗?
from pysnmp.hlapi import *
errorIndication, errorStatus, errorIndex, varBinds = nextCmd(
SnmpEngine(),
CommunityData('public', mpModel=1),
UdpTransportTarget(('giga-int-2', 161)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.2.1.31.1.1.1.1')),
lexicographicMode=False
)
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
for v in varBinds:
for name, val in v:
print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
nextCmd()
函数(以及其他 pysnmp 函数)返回一个您应该迭代的 Python 生成器对象:
>>> from pysnmp.hlapi import *
>>> g = nextCmd(SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 161)),
... ContextData(),
... ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr')))
>>> next(g)
(None, 0, 0, [ObjectType(ObjectIdentity(ObjectName('1.3.6.1.2.1.1.1.0')),
DisplayString('SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m'))])
您可以像任何 Python 迭代器一样使用此生成器,例如在循环中。或者,如果您只需要运行一个请求/响应,您可以简单地调用
next
。
在每次迭代中,pysnmp 都会发出一个请求(一个或多个,具体取决于具体情况),要求提供一个大于前一个 OID 的 OID。这样您就可以“行走”SNMP 代理,直到您打破循环或耗尽代理端的 OID。
您的错误是您期望 pysnmp 的
nextCmd
立即运行 SNMP 查询并返回一个值。相反,pysnmp 为您提供了一个生成器协程,您可以重复使用它来执行多个查询(您也可以 .send()
它的 OID 来查询)。
感谢您的回答。我重写了代码。现在它正在做我想做的事。它只是搜索“1.3.6.1.2.1.31.1.1.1.x”树。
from pysnmp.hlapi import *
for errorIndication, errorStatus, errorIndex, varBinds in nextCmd(
SnmpEngine(),
CommunityData('public', mpModel=1),
UdpTransportTarget(('giga-int-2', 161)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.2.1.31.1.1.1.1')),
lexicographicMode=False
):
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
for v in varBinds:
print(v.prettyPrint())
SNMPv2-SMI::mib-2.31.1.1.1.1.1 = sc0
SNMPv2-SMI::mib-2.31.1.1.1.1.2 = sl0
SNMPv2-SMI::mib-2.31.1.1.1.1.3 = sc1
SNMPv2-SMI::mib-2.31.1.1.1.1.5 = VLAN-1
SNMPv2-SMI::mib-2.31.1.1.1.1.6 = VLAN-1002
...