Re: [pysnmp-users] pysnmp / pyasn1 decode problem with multi-line traps
Brought to you by:
elie
From: Ilya E. <il...@gl...> - 2015-03-10 06:03:20
|
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 <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:28 <x-apple-data-detectors://4>using '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 > |