etingof / pysnmp

Python SNMP library
http://snmplabs.com/pysnmp/
BSD 2-Clause "Simplified" License
568 stars 188 forks source link

pysnmp : working with up to None octets of substrate #425

Closed akhileshjoshi123 closed 1 year ago

akhileshjoshi123 commented 1 year ago

I am trying a simple SNMP receiver script:

from pysnmp.entity import engine, config
from pysnmp.carrier.asyncore.dgram import udp
from pysnmp.entity.rfc3413 import ntfrcv
from pyasn1 import debug
debug.setLogger(debug.Debug('all'))

# Create SNMP engine with autogenernated engineID and pre-bound
# to socket transport dispatcher
snmpEngine = engine.SnmpEngine()

# Transport setup
# UDP over IPv4, first listening interface/port
config.addTransport(
    snmpEngine,
    udp.domainName + (1,),
    udp.UdpTransport().openServerMode(('0.0.0.0', 162))
)

# SNMPv1/2c setup
# SecurityName <-> CommunityName mapping
config.addV1System(snmpEngine, 'my-area', 'public')

# Callback function for receiving notifications
# noinspection PyUnusedLocal,PyUnusedLocal,PyUnusedLocal
def cbFun(snmpEngine, stateReference, contextEngineId, contextName,
          varBinds, cbCtx):
    print('Notification from ContextEngineId "%s", ContextName "%s"' % (contextEngineId.prettyPrint(),
                                                                        contextName.prettyPrint()))
    for name, val in varBinds:
        print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))

# Register SNMP Application at the SNMP engine
ntfrcv.NotificationReceiver(snmpEngine, cbFun)
snmpEngine.transportDispatcher.jobStarted(1)  # this job would never finish
snmpEngine.transportDispatcher.runDispatcher()

# Run I/O dispatcher which would receive queries and send confirmations
try:
    snmpEngine.transportDispatcher.runDispatcher()

finally:
    snmpEngine.transportDispatcher.closeDispatcher()

Output:

2022-08-22 14:18:44,463 pyasn1: decoder called at scope Sequence with state 0, working with up to None octets of substrate: <_io.BytesIO object at 0x7fbc1d815048>
2022-08-22 14:18:44,464 pyasn1: tag decoded into <TagSet object, tags 0:32:16>, decoding length
2022-08-22 14:18:44,464 pyasn1: value length decoded into 444
2022-08-22 14:18:44,464 pyasn1: candidate ASN.1 spec is Sequence
2022-08-22 14:18:44,464 pyasn1: value decoder chosen for an ambiguous type by type ID 11
2022-08-22 14:18:44,464 pyasn1: codec SequencePayloadDecoder chosen by ASN.1 spec, decoding value
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/pysnmp/carrier/asyncore/dispatch.py", line 46, in runDispatcher
    use_poll=True, map=self.__sockMap, count=1)
  File "/usr/lib64/python3.6/asyncore.py", line 207, in loop
    poll_fun(timeout, map)
  File "/usr/lib64/python3.6/asyncore.py", line 188, in poll2
    readwrite(obj, flags)
  File "/usr/lib64/python3.6/asyncore.py", line 123, in readwrite
    obj.handle_error()
  File "/usr/lib64/python3.6/asyncore.py", line 108, in readwrite
    obj.handle_read_event()
  File "/usr/lib64/python3.6/asyncore.py", line 423, in handle_read_event
    self.handle_read()
  File "/usr/local/lib/python3.6/site-packages/pysnmp/carrier/asyncore/dgram/base.py", line 170, in handle_read
    self._cbFun(self, transportAddress, incomingMessage)
  File "/usr/local/lib/python3.6/site-packages/pysnmp/carrier/base.py", line 85, in _cbFun
    self, transportDomain, transportAddress, incomingMessage
  File "/usr/local/lib/python3.6/site-packages/pysnmp/entity/engine.py", line 152, in __receiveMessageCbFun
    self, transportDomain, transportAddress, wholeMsg
  File "/usr/local/lib/python3.6/site-packages/pysnmp/proto/rfc3412.py", line 291, in receiveMessage
    msgVersion = verdec.decodeMessageVersion(wholeMsg)
  File "/usr/local/lib/python3.6/site-packages/pysnmp/proto/api/verdec.py", line 17, in decodeMessageVersion
    recursiveFlag=False, substrateFun=lambda a, b, c: (a, b[:c])
  File "/usr/lib/python3.6/site-packages/pyasn1-0.5.0rc1-py3.6.egg/pyasn1/codec/ber/decoder.py", line 1998, in __call__
    for asn1Object in streamingDecoder:
  File "/usr/lib/python3.6/site-packages/pyasn1-0.5.0rc1-py3.6.egg/pyasn1/codec/ber/decoder.py", line 1914, in __iter__
    self._substrate, self._asn1Spec, **self._options):
  File "/usr/lib/python3.6/site-packages/pyasn1-0.5.0rc1-py3.6.egg/pyasn1/codec/ber/decoder.py", line 1776, in __call__
    self, substrateFun, **options):
  File "/usr/lib/python3.6/site-packages/pyasn1-0.5.0rc1-py3.6.egg/pyasn1/codec/ber/decoder.py", line 653, in valueDecoder
    for chunk in substrateFun(asn1Object, substrate, length, options):
TypeError: <lambda>() takes 3 positional arguments but 4 were given

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/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 "/usr/local/lib/python3.6/site-packages/pysnmp/carrier/asyncore/dispatch.py", line 46, in runDispatcher
    use_poll=True, map=self.__sockMap, count=1)
;  File "/usr/lib64/python3.6/asyncore.py", line 207, in loop
    poll_fun(timeout, map)
;  File "/usr/lib64/python3.6/asyncore.py", line 188, in poll2
    readwrite(obj, flags)
;  File "/usr/lib64/python3.6/asyncore.py", line 123, in readwrite
    obj.handle_error()
;  File "/usr/lib64/python3.6/asyncore.py", line 108, in readwrite
    obj.handle_read_event()
;  File "/usr/lib64/python3.6/asyncore.py", line 423, in handle_read_event
    self.handle_read()
;  File "/usr/local/lib/python3.6/site-packages/pysnmp/carrier/asyncore/dgram/base.py", line 170, in handle_read
    self._cbFun(self, transportAddress, incomingMessage)
;  File "/usr/local/lib/python3.6/site-packages/pysnmp/carrier/base.py", line 85, in _cbFun
    self, transportDomain, transportAddress, incomingMessage
;  File "/usr/local/lib/python3.6/site-packages/pysnmp/entity/engine.py", line 152, in __receiveMessageCbFun
    self, transportDomain, transportAddress, wholeMsg
;  File "/usr/local/lib/python3.6/site-packages/pysnmp/proto/rfc3412.py", line 291, in receiveMessage
    msgVersion = verdec.decodeMessageVersion(wholeMsg)
;  File "/usr/local/lib/python3.6/site-packages/pysnmp/proto/api/verdec.py", line 17, in decodeMessageVersion
    recursiveFlag=False, substrateFun=lambda a, b, c: (a, b[:c])
;  File "/usr/lib/python3.6/site-packages/pyasn1-0.5.0rc1-py3.6.egg/pyasn1/codec/ber/decoder.py", line 1998, in __call__
    for asn1Object in streamingDecoder:
;  File "/usr/lib/python3.6/site-packages/pyasn1-0.5.0rc1-py3.6.egg/pyasn1/codec/ber/decoder.py", line 1914, in __iter__
    self._substrate, self._asn1Spec, **self._options):
;  File "/usr/lib/python3.6/site-packages/pyasn1-0.5.0rc1-py3.6.egg/pyasn1/codec/ber/decoder.py", line 1776, in __call__
    self, substrateFun, **options):
;  File "/usr/lib/python3.6/site-packages/pyasn1-0.5.0rc1-py3.6.egg/pyasn1/codec/ber/decoder.py", line 653, in valueDecoder
    for chunk in substrateFun(asn1Object, substrate, length, options):
;TypeError: <lambda>() takes 3 positional arguments but 4 were given
caused by <class 'TypeError'>: <lambda>() takes 3 positional arguments but 4 were given

I looked for references for this particular issue but I am unable to find any. Can someone please help ?

akhileshjoshi123 commented 1 year ago

Found the issue. The issue was with the pyasn1 package. I had pyasn1 0.5.0.rc1 version installed. Downgrading it to pyasn1 0.4.8 resolved the issue. For some reason decoder was erroring out due to the above error message that I posted. But glad that it is working now.