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

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

我正在尝试使用 pysnmp 模块为 apc 插座供电。

这是工作正常的手动命令:

# 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)

代码如下:

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()))

我已将 mibs 文件复制到以下路径:

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

我在尝试 运行 脚本时看到以下错误:

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>

如果我遗漏了什么,有人可以告诉我吗?我该如何解决这个错误?

我猜一个问题是错误的 MibVariable 初始化参数。最重要的是,您尝试引用的 MIB 对象未在 SNMPv2-SMI MIB 中定义。

MibVariable(又名 ObjectIdentity)类型需要 MIB-nameobject-nameindices。使用您的代码,您将 object-name + indices 粘合在一起作为 object-name。这就是它无法解析 MIB 对象的原因。

这个怎么样:

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.snmplabs.com', 161)),
      ContextData(),
      ObjectType(ObjectIdentity('SNMPv2-SMI', 'enterprises', '318.1.1.26.9.2.4.1.5.27'), Integer32(1))
    )
)

或者更好的方法是实际使用 PowerNet-MIB:

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

或者您可以将裸 OID 与值类型一起传递给 pysnmp:

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

最后,您不应该将 MIB 复制到 pysnmp 安装目录中。考虑 referring to your own MIBs directory instead. Alternatively, you can ask pysnmp to automatically search for and download requested MIBs from the web.