Re: [pysnmp-users] pysnmp / pyasn1 decode problem with multi-line traps
Brought to you by:
elie
From: Tom <to...@we...> - 2015-03-11 06:02:56
|
Hi -ilya, thanks for the quick answer. Your solution sounds great but unfortunately I have some troubles with the implementation. I changed the script as follows: ... print('Var-binds:') result = {} for oid, val in varBinds: print('%s = %s' % (oid.prettyPrint(), val.prettyPrint())) result[oid] = val.asOctets() print('%s = %s' % (oid.prettyPrint(), result.prettyPrint())) ... When I run the script and send some test data via snmp to the server I got this error message. Traceback (most recent call last): File "snmp-transports.py", line 115, in <module> transportDispatcher.runDispatcher() File "/usr/lib/python2.7/dist-packages/pysnmp/carrier/asynsock/dispatch.py", line 41, in runDispatcher raise PySnmpError('poll error: %s' % ';'.join(format_exception(*exc_info()))) pysnmp.error.PySnmpError: poll error: Traceback (most recent call last): ; File "/usr/lib/python2.7/dist-packages/pysnmp/carrier/asynsock/dispatch.py", line 37, in runDispatcher use_poll=True, map=self.__sockMap, count=1) ; File "/usr/lib/python2.7/asyncore.py", line 220, in loop poll_fun(timeout, map) ; File "/usr/lib/python2.7/asyncore.py", line 201, in poll2 readwrite(obj, flags) ; File "/usr/lib/python2.7/asyncore.py", line 123, in readwrite obj.handle_error() ; File "/usr/lib/python2.7/asyncore.py", line 108, in readwrite obj.handle_read_event() ; File "/usr/lib/python2.7/asyncore.py", line 449, in handle_read_event self.handle_read() ; File "/usr/lib/python2.7/dist-packages/pysnmp/carrier/asynsock/dgram/base.py", line 83, in handle_read self._cbFun(self, transportAddress, incomingMessage) ; File "/usr/lib/python2.7/dist-packages/pysnmp/carrier/base.py", line 52, in _cbFun self, transportDomain, transportAddress, incomingMessage ; File "snmp-transports.py", line 59, in trapCallback result[oid] = val.asOctets() ;AttributeError: ObjectSyntax instance has no attribute 'asOctets' Where is my mistake? Does it only works if a hex value is send by snmp? How can I check if the value is hex or not? Sorry for the stupid questions but I'm really new to python and their modules. Kind regards, Tom > Am 10.03.2015 um 07:03 schrieb Ilya Etingof <il...@gl...>: > > Hi Tom, > > The OctetString type prints its contents in hex form if there’s a potentially non-printable characters there that may be lost when printing to the terminal. Use .asOctets() method to retrieve data in pure octet-string form: > > >>> s = OctetString(hexValue='4f70657261746f722058585858585858585858203a20585858585858585858582c2058585858585858202c204c6f63616c6c792061757468656e74696361746564202d207375636365737366756c6c79207369676e6564206f6e20746f20746865207465726d696e616c20275858582e5858582e582e584058272061742031343a3238207573696e672027585858585858585820585858205858585858585858272e0a4f70657261746f722050726f66696c65733a2058585858585858585858585858') > >>> s.prettyPrint() > '0x4f70657261746f722058585858585858585858203a20585858585858585858582c2058585858585858202c204c6f63616c6c792061757468656e74696361746564202d207375636365737366756c6c79207369676e6564206f6e20746f20746865207465726d696e616c20275858582e5858582e582e584058272061742031343a3238207573696e672027585858585858585820585858205858585858585858272e0a4f70657261746f722050726f66696c65733a2058585858585858585858585858' > >>> s.asOctets() > "Operator XXXXXXXXXX : XXXXXXXXXX, XXXXXXX , Locally authenticated - successfully signed on to the terminal 'XXX.XXX.X.X@X' at 14:28 using 'XXXXXXXX XXX XXXXXXXX'.\nOperator Profiles: XXXXXXXXXXXXX" > > -ilya > >> On 09 Mar 2015, at 22:23, Tom Kr. <to...@we...> wrote: >> >> >> Hi there, >> >> I'm new to this mailing list and I have a issue with the pysnmp / pyasn1 module. >> I use python with version 3.4 and pysnmp with version 4.2.5. >> The OS is Ubuntu server 14.04.2. >> >> # lsb_release -a >> No LSB modules are available. >> Distributor ID: Ubuntu >> Description: Ubuntu 14.04.2 LTS >> Release: 14.04 >> Codename: trusty >> >> ii python-pysnmp4 4.2.5-1 all Python SNMP library for agents and managers (Python 2 module) >> ii python-pyasn1 0.1.7-1ubuntu2 all ASN.1 library for Python (Python 2 module) >> ii python3.4 3.4.0-2ubuntu1 amd64 Interactive high-level object-oriented language (version 3.4) >> >> I try to use the following script to read snmp traps and print this traps on the screen. >> But unfortunately I have some troubles with multi-line traps. >> This traps are only shown as hex values. >> I think the problem is the decoder but I'm not very familiar with python and also not with pysnmp. >> >> I hope someone of you can help me or give me some ideas what I should do next. >> For the moment I have no idea. >> >> The script: >> ---------------------------------------------------------------------------------------------- >> import sys,os >> >> sys.path.append('/usr/share/pyshared/') >> >> from pysnmp.carrier.asynsock.dispatch import AsynsockDispatcher >> from pysnmp.carrier.asynsock.dgram import udp, udp6 >> from pyasn1.codec.ber import decoder >> from pysnmp.proto import api >> >> def trapCallback(transportDispatcher, transportDomain, transportAddress, wholeMsg): >> while wholeMsg: >> msgVer = int(api.decodeMessageVersion(wholeMsg)) >> if msgVer in api.protoModules: >> pMod = api.protoModules[msgVer] >> else: >> print('Unsupported SNMP version %s' % msgVer) >> return >> reqMsg, wholeMsg = decoder.decode( >> wholeMsg, asn1Spec=pMod.Message(), >> ) >> print('Notification message from %s:%s: ' % ( >> transportDomain, transportAddress >> ) >> ) >> reqPDU = pMod.apiMessage.getPDU(reqMsg) >> if reqPDU.isSameTypeWith(pMod.TrapPDU()): >> if msgVer == api.protoVersion1: >> print('Enterprise: %s' % ( >> pMod.apiTrapPDU.getEnterprise(reqPDU).prettyPrint() >> ) >> ) >> print('Agent Address: %s' % ( >> pMod.apiTrapPDU.getAgentAddr(reqPDU).prettyPrint() >> ) >> ) >> print('Generic Trap: %s' % ( >> pMod.apiTrapPDU.getGenericTrap(reqPDU).prettyPrint() >> ) >> ) >> print('Specific Trap: %s' % ( >> pMod.apiTrapPDU.getSpecificTrap(reqPDU).prettyPrint() >> ) >> ) >> print('Uptime: %s' % ( >> pMod.apiTrapPDU.getTimeStamp(reqPDU).prettyPrint() >> ) >> ) >> varBinds = pMod.apiTrapPDU.getVarBindList(reqPDU) >> else: >> varBinds = pMod.apiPDU.getVarBindList(reqPDU) >> print('Var-binds:') >> for oid, val in varBinds: >> print('%s = %s' % (oid.prettyPrint(), val.prettyPrint())) >> return wholeMsg >> >> transportDispatcher = AsynsockDispatcher() >> >> transportDispatcher.registerRecvCbFun(trapCallback) >> >> # UDP/IPv4 >> transportDispatcher.registerTransport( >> udp.domainName, udp.UdpSocketTransport().openServerMode(('localhost', 162)) >> ) >> >> # UDP/IPv6 >> transportDispatcher.registerTransport( >> udp6.domainName, udp6.Udp6SocketTransport().openServerMode(('::1', 162)) >> ) >> >> transportDispatcher.jobStarted(1) >> >> try: >> # Dispatcher will never finish as job#1 never reaches zero >> transportDispatcher.runDispatcher() >> except: >> transportDispatcher.closeDispatcher() >> raise >> ---------------------------------------------------------------------------------------------- >> >> >> As you can see, the script is an example from the project side. >> (http://pysnmp.sourceforge.net/examples/current/v1arch/manager/ntfrcv/v2c-multiple-transports.html) >> >> This is the part of the trap where the hex value is shown instead of the plain text. >> string=0x4f70657261746f722058585858585858585858203a20585858585858585858582c2058585858585858202c204c6f63616c6c792061757468656e74696361746564202d207375636365737366756c6c79207369676e6564206f6e20746f20746865207465726d696e616c20275858582e5858582e582e584058272061742031343a3238207573696e672027585858585858585820585858205858585858585858272e0a4f70657261746f722050726f66696c65733a2058585858585858585858585858 >> >> This is the message I would expect. >> "Operator XXXXXXXXXX : XXXXXXXXXX, XXXXXXX , Locally authenticated - successfully signed on to the terminal 'XXX.XXX.X.X@X' at 14:28using 'XXXXXXXX XXX XXXXXXXX'. >> Operator Profiles: XXXXXXXXXXXXX" >> >> For me, the problem is located at the line feed. Is there a possibility to replace any line feed character with a space or nothing? >> I think this should be done in the pyasn1 module, but I don't know how. >> >> Hope somebody can help me with my problem. >> >> Kind regards, >> Tom >> > |