Search code examples
pythonpython-2.7pysnmp

pysnmp.smi.error.SmiError: No symbol error while trying to power on the apc outlet with pysnmp


I am trying to use pysnmp module to power on apc outlet.

This is the manual command which works fine:

# snmpset -v1 -c comstring 10.x.x.x SNMPv2-SMI::enterprises.318.1.1.26.9.2.4.1.5.27 i 1
PowerNet-MIB::rPDU2OutletSwitchedControlCommand.27 = INTEGER: immediateOn(1)

Here is the code:

from pysnmp.entity.rfc3413.oneliner import cmdgen

cmdGen = cmdgen.CommandGenerator()

errorIndication, errorStatus, errorIndex, varBinds = cmdGen.setCmd(
    cmdgen.CommunityData('comstring'),
    cmdgen.UdpTransportTarget(('10.x.x.x', 161)),
    (cmdgen.MibVariable('SNMPv2-SMI', 'enterprises.318.1.1.26.9.2.4.1.5', "27"), 1)
)

# Check for errors and print out results
if errorIndication:
    print(errorIndication)
else:
    if errorStatus:
        print('%s at %s' % (
            errorStatus.prettyPrint(),
            errorIndex and varBinds[int(errorIndex)-1] or '?'
            )
        )
    else:
        for name, val in varBinds:
            print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))

I have copied mibs file to following paths:

/usr/lib/python2.7/site-packages/pysnmp/smi/mibs
/usr/lib/python2.7/site-packages/pysnmp/smi/mibs/instances

I am seeing below error when I try to run the script:

Traceback (most recent call last):
  File "snmp3.py", line 128, in <module>
    (cmdgen.MibVariable('SNMPv2-SMI', 'enterprises.318.1.1.26.9.2.4.1.5', "27"), 1)
  File "/usr/lib/python2.7/site-packages/pysnmp/entity/rfc3413/oneliner/cmdgen.py", line 200, in setCmd
    **kwargs):
  File "/usr/lib/python2.7/site-packages/pysnmp/hlapi/asyncore/sync/cmdgen.py", line 217, in setCmd
    lookupMib=options.get('lookupMib', True)))
  File "/usr/lib/python2.7/site-packages/pysnmp/hlapi/asyncore/cmdgen.py", line 239, in setCmd
    contextData.contextName, vbProcessor.makeVarBinds(snmpEngine, varBinds),
  File "/usr/lib/python2.7/site-packages/pysnmp/hlapi/varbinds.py", line 39, in makeVarBinds
    __varBinds.append(varBind.resolveWithMib(mibViewController))
  File "/usr/lib/python2.7/site-packages/pysnmp/smi/rfc1902.py", line 845, in resolveWithMib
    self.__args[0].resolveWithMib(mibViewController)
  File "/usr/lib/python2.7/site-packages/pysnmp/smi/rfc1902.py", line 481, in resolveWithMib
    self.__modName, self.__symName
  File "/usr/lib/python2.7/site-packages/pysnmp/smi/builder.py", line 407, in importSymbols
    'No symbol %s::%s at %s' % (modName, symName, self)
pysnmp.smi.error.SmiError: No symbol SNMPv2-SMI::enterprises.318.1.1.26.9.2.4.1.5 at <pysnmp.smi.builder.MibBuilder object at 0x367f550>

Can someone please let me know if I am missing anything here? How can I resolve this error?


Solution

  • I guess one problem is with wrong MibVariable initialization parameters. On top of that, the MIB object you are trying to refer to is not defined in the SNMPv2-SMI MIB.

    The MibVariable (AKA ObjectIdentity) type expects MIB-name, object-name, indices. With your code you pass object-name + indices glued together as object-name. This is why it fails to resolve the MIB object.

    How about this:

    from pysnmp.hlapi import *
    from pysnmp import debug
    
    debug.setLogger(debug.Debug('msgproc'))
    
    snmpEngine = SnmpEngine()
    
    (errorIndication,
     errorStatus,
     errorIndex,
     varBinds) = next(
        setCmd(
          snmpEngine,
          CommunityData('public'),
          UdpTransportTarget(('demo.pysnmp.com', 161)),
          ContextData(),
          ObjectType(ObjectIdentity('SNMPv2-SMI', 'enterprises', '318.1.1.26.9.2.4.1.5.27'), Integer32(1))
        )
    )
    

    Or much better approach would be to actually use the PowerNet-MIB:

    ObjectType(ObjectIdentity('PowerNet-MIB', 'rPDU2OutletSwitchedControlCommand', 27), 1) 
    

    Or you can just pass that bare OID along with value type to pysnmp:

    ObjectType(ObjectIdentity('1.3.6.1.4.1.318.1.1.26.9.2.4.1.5.27'), Integer32(1))
    

    Finally, you should not copy your MIBs into pysnmp installation directory. Consider referring to your own MIBs directory instead. Alternatively, you can ask pysnmp to automatically search for and download requested MIBs from the web.