pysnmp - ValueError: too many values to unpack (expected 4)
pysnmp - ValueError: too many values to unpack (expected 4)
如果我尝试这样的事情,我得到了
ValueError: too many values to unpack (expected 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 函数)return 是您应该迭代的单个 Python 生成器对象:
>>> from pysnmp.hlapi import *
>>> g = nextCmd(SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.snmplabs.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 可迭代对象一样使用此生成器,例如在循环中。或者,如果您只需要 运行 一个 request/response,您只需调用 next
即可。
在每次迭代中,pysnmp 都会发出一个请求(一个或多个,视情况而定),要求提供一个大于前一个 OID 的 OID。这样你就可以 "walk" SNMP 代理,直到你打破循环或耗尽代理端的 OID。
你在这里的错误是你期望 pysnmp 的 nextCmd
到 运行 SNMP 查询立即和 return 一个值。相反,pysnmp 为您提供了一个生成器协程,您可以重复使用它来执行多个查询(您也可以 .send()
它的 OIDs 进行查询)。
感谢您的回答。我重写了代码。现在它正在做我想做的事。它只是搜索“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
...
如果我尝试这样的事情,我得到了
ValueError: too many values to unpack (expected 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 函数)return 是您应该迭代的单个 Python 生成器对象:
>>> from pysnmp.hlapi import *
>>> g = nextCmd(SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.snmplabs.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 可迭代对象一样使用此生成器,例如在循环中。或者,如果您只需要 运行 一个 request/response,您只需调用 next
即可。
在每次迭代中,pysnmp 都会发出一个请求(一个或多个,视情况而定),要求提供一个大于前一个 OID 的 OID。这样你就可以 "walk" SNMP 代理,直到你打破循环或耗尽代理端的 OID。
你在这里的错误是你期望 pysnmp 的 nextCmd
到 运行 SNMP 查询立即和 return 一个值。相反,pysnmp 为您提供了一个生成器协程,您可以重复使用它来执行多个查询(您也可以 .send()
它的 OIDs 进行查询)。
感谢您的回答。我重写了代码。现在它正在做我想做的事。它只是搜索“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
...