使用 PySNMP 库接收 SNMP 响应后崩溃

问题描述 投票:0回答:1

使用 PySNMP 库接收 SNMP 响应后崩溃

收到 get-response 后脚本崩溃了

使用: Windows 10 pysnmp 4.4.12 Python 解释器:3.10

这是我的代码:

from pysnmp.hlapi import *

iterator = getCmd(SnmpEngine(),
                  CommunityData('public', mpModel=1),
                  UdpTransportTarget(('172.19.6.41', 161)),
                  ContextData(),
                  ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'))
                  )
print('start')

errorIndication, errorStatus, errorIndex, varBinds = next(iterator)

if errorIndication:  # SNMP engine errors
    print(errorIndication)
else:
    if errorStatus:  # SNMP agent errors
        print('%s at %s' % (errorStatus.prettyPrint(),
                            varBinds[int(errorIndex) - 1] if errorIndex else '?'))
    else:
        for varBind in varBinds:  # SNMP response contents
            print(' = '.join([x.prettyPrint() for x in varBind]))
print('end')

################################################## ######################################## 我可以在wireshark 中看到SNMP 请求的发送和响应。 在终端中我看到以下崩溃:

标签:

C:\Users\user\AppData\Local\Programs\Python\Python310\python.exe C:\Projects\pythonProject\OOO\SNMP_examples\PySNMP\04_get_snmp.py 
start
Traceback (most recent call last):
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\carrier\asyncore\dispatch.py", line 45, in runDispatcher
loop(timeout or self.getTimerResolution(),
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 214, in loop
poll_fun(timeout, map)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 157, in poll
read(obj)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 94, in read
obj.handle_error()
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 90, in read
obj.handle_read_event()
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 427, in handle_read_event
self.handle_read()
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\carrier\asyncore\dgram\base.py", line 170, in handle_read
self._cbFun(self, transportAddress, incomingMessage)
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\carrier\base.py", line 84, in _cbFun
self.__recvCallables[recvId](
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\entity\engine.py", line 151, in __receiveMessageCbFun
self.msgAndPduDsp.receiveMessage(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\proto\rfc3412.py", line 291, in receiveMessage
msgVersion = verdec.decodeMessageVersion(wholeMsg)
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\proto\api\verdec.py", line 15, in decodeMessageVersion
seq, wholeMsg = decoder.decode(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pyasn1\codec\ber\decoder.py", line 2003, in __call__
for asn1Object in streamingDecoder:
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pyasn1\codec\ber\decoder.py", line 1918, in __iter__
for asn1Object in self._singleItemDecoder(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pyasn1\codec\ber\decoder.py", line 1778, in __call__
for value in concreteDecoder.valueDecoder(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pyasn1\codec\ber\decoder.py", line 654, in valueDecoder
for chunk in substrateFun(asn1Object, substrate, length, options):
TypeError: decodeMessageVersion.<locals>.<lambda>() takes 3 positional arguments but 4 were given

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Projects\pythonProject\OOO\SNMP_examples\PySNMP\04_get_snmp.py", line 19, in <module>
errorIndication, errorStatus, errorIndex, varBinds = next(iterator)
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\hlapi\asyncore\sync\cmdgen.py", line 113, in getCmd
snmpEngine.transportDispatcher.runDispatcher()
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\carrier\asyncore\dispatch.py", line 50, in runDispatcher
raise PySnmpError('poll error: %s' % ';'.join(format_exception(*exc_info())))
pysnmp.error.PySnmpError: poll error: Traceback (most recent call last):
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\carrier\asyncore\dispatch.py", line 45, in runDispatcher
loop(timeout or self.getTimerResolution(),
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 214, in loop
poll_fun(timeout, map)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 157, in poll
read(obj)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 94, in read
obj.handle_error()
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 90, in read
obj.handle_read_event()
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncore.py", line 427, in handle_read_event
self.handle_read()
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\carrier\asyncore\dgram\base.py", line 170, in handle_read
self._cbFun(self, transportAddress, incomingMessage)
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\carrier\base.py", line 84, in _cbFun
self.__recvCallables[recvId](
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\entity\engine.py", line 151, in __receiveMessageCbFun
self.msgAndPduDsp.receiveMessage(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\proto\rfc3412.py", line 291, in receiveMessage
msgVersion = verdec.decodeMessageVersion(wholeMsg)
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pysnmp\proto\api\verdec.py", line 15, in decodeMessageVersion
seq, wholeMsg = decoder.decode(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pyasn1\codec\ber\decoder.py", line 2003, in __call__
for asn1Object in streamingDecoder:
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pyasn1\codec\ber\decoder.py", line 1918, in __iter__
for asn1Object in self._singleItemDecoder(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pyasn1\codec\ber\decoder.py", line 1778, in __call__
for value in concreteDecoder.valueDecoder(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\pyasn1\codec\ber\decoder.py", line 654, in valueDecoder
for chunk in substrateFun(asn1Object, substrate, length, options): ;TypeError: decodeMessageVersion.<locals>.<lambda>() takes 3 positional arguments but 4 were given
caused by <class 'TypeError'>: decodeMessageVersion.<locals>.<lambda>() takes 3 positional arguments but 4 were given

Process finished with exit code 1

################################################## ################################## 如果我发表评论

#ContextData(),

请求未发送,但在终端中我可以看到打印的开始和结束。

我尝试在终端中发送 SNMP 请求并接收响应。

我发送了回复并希望在终端中得到回复。

python snmp pysnmp
1个回答
0
投票

扩展了我上面的评论。

您在 Windows 上的初次尝试以某种方式将依赖项

pyasn1
解决为不兼容的版本 0.5.0。这是崩溃的典型原因,调用堆栈清楚地表明了这一点。

您在 Linux 上的最后一次尝试成功了,只是

pyasn1
版本 0.5.1 已发布,并且不兼容性消失了。

但是一旦切换到 Python 3.12,你应该会遇到更多问题,因为

asyncore
已经消失了。

您可能想阅读此公告,了解有关 PySNMP 的其他信息。

© www.soinside.com 2019 - 2024. All rights reserved.