Crashing after receiving SNMP-response with PySNMP library
Script crashed after receiving get-response
Used: Windows 10 pysnmp 4.4.12 Python interpreter: 3.10
Here is my code:
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')
######################################################################################## I can see that in the wireshark SNMP-request sent and responded. In terminal I see following crash:
tag:
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
#################################################################################
In case I commenting #ContextData(),
Request isn't sent, but in terminal I can see start and end printed.
I tried to send SNMP request and receive response in the terminal.
I sent response and expected to get response in the terminal.
Your initial attempt on Windows somehow resolved the dependent
pyasn1to an incompatible version 0.5.0. That's the classic cause of the crash and the call stack indicated that clearly.Your last attempt on Linux worked, simply
pyasn1version 0.5.1 was shipped and the incompatibility is gone.But you should expect more issues once you switch to Python 3.12, as
asyncoreis gone then.You might want to read this announcement to see what else you should know about PySNMP.