0

I am trying to use the below code to perform simple SNMP operation. Please help me figure out the error.

    from pysnmp.entity.rfc3413.oneliner import cmdgen

    cmdGen = cmdgen.CommandGenerator()

    errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
        cmdgen.CommunityData('public'),
        cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)),
        cmdgen.MibVariable('SNMPv2-MIB', 'sysName', 0),
    )

    # Check for errors and print out results
    if errorIndication:
        print(errorIndication)
    elif errorStatus:
        print(errorStatus)
    else:
        for name, val in varBinds:
            print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))

The above code gives the following error:

Traceback (most recent call last):
  File "tut.py", line 1, in <module>
    from pysnmp.entity.rfc3413.oneliner import cmdgen
  File "/usr/lib/python2.6/site-packages/pysnmp/entity/rfc3413/oneliner/cmdgen.py", line 1, in <module>
    from pysnmp.entity import engine, config
  File "/usr/lib/python2.6/site-packages/pysnmp/entity/engine.py", line 2, in <module>
    from pysnmp.proto.rfc3412 import MsgAndPduDispatcher
  File "/usr/lib/python2.6/site-packages/pysnmp/proto/rfc3412.py", line 3, in <module>
    from pyasn1.compat.octets import null
ImportError: No module named compat.octets
user3732361
  • 377
  • 8
  • 13
  • how did you install `pysnmp` package? This code works fine for me . I get `1.3.6.1.2.1.1.5.0 = zeus.snmplabs.com ` – agstudy Jun 22 '14 at 23:06
  • I am not allowed to install any packages on the linux machine that the company has provided me. It is very annoying. As you can see, they are still making us use python 2.6. I can't do anything until the IT department accepts the request and according to my manager, they always reject the requests. So if this code only works with the latest pysnmp package then please suggest me an alternate way of achieving this with any built-in libraries in python. – user3732361 Jun 23 '14 at 00:43

2 Answers2

0

Chances are your system pyasn1 an pysnmp packages are version-incompatible with each other. I'd suggest putting both pyasn1 and pysnmp packages (the latest ones) into your $HOME and setting $PYTHONPATH pointing to each of them.

Ilya Etingof
  • 5,440
  • 1
  • 17
  • 21
0

This should be probably a comment to the previous post. Ilya is right with his answer. On my side I've been able to rectify this problem by switching to a higher version of pyasn1. Was using 1 0.0.11a-1ubuntu1 and switched to 1_0.1.7-1ubuntu2. Would be better if this dependency was better formulated in the dependencies of python-pysnmp4

costas
  • 1