PYSNMP 4.3.2 mibdump.py输出问题

时间:2016-07-04 19:44:04

标签: python-2.7 snmp pysnmp

我有一个自定义MIB文件syzf-alarms.mib。当我使用mibdump.py生成带有mibdump.py ~/syzf-ALARMS的pysnmp支持的python模块时,我得到:

Source MIB repo
sitories: file:///home/sbhattac, file:///usr/share/snmp/mibs, http://mibs.snmplabs.com/asn1/@mib@
Borrow missing/failed MIBs from: http://mibs.snmplabs.com/pysnmp/notexts/@mib@
Existing/compiled MIB locations: pysnmp.smi.mibs, pysnmp_mibs
Compiled MIBs destination directory: /home/sbhattac/.pysnmp/mibs
MIBs excluded from code generation: RFC-1212, RFC-1215, RFC1065-SMI, RFC1155-SMI, RFC1158-MIB, RFC1213-MIB, SNMP-FRAMEWORK-MIB, SNMP-TARGET-MIB, SNMPv2-CONF, SNMPv2-SMI, SNMPv2-TC, SNMPv2-TM, TRANSPORT-ADDRESS-MIB
MIBs to compile: syzf-ALARMS
Destination format: pysnmp
Parser grammar cache directory: not used
Also compile all relevant MIBs: yes
Rebuild MIBs regardless of age: no
Do not create/update MIBs: no
Byte-compile Python modules: yes (optimization level 0)
Ignore compilation errors: no
Generate OID->MIB index: no
Generate texts in MIBs: no
Try various filenames while searching for MIB module: yes
Created/updated MIBs: syzf-ALARMS
Pre-compiled MIBs borrowed:
Up to date MIBs: SNMPv2-CONF, SNMPv2-SMI, SNMPv2-TC
Missing source MIBs:
Ignored MIBs:
Failed MIBs:

现在它在syzf-alarms.py/pyc生成一个home/sbhattac/.pysnmp/mibs文件。

现在我正在尝试在我的代码中使用此模块:

errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(cmdgen.CommunityData('public'),cmdgen.UdpTransportTarget(('localhost', 161)), cmdgen.ObjectIdentity('1, 3, 6, 1, 4, 1, 211, 1, 24, 11, 2, 1, 1, 7, 1, 3, 3, 1').addMibSource('/home/sbhattac/.pysnmp/mibs', 'syzf-ALARMS'))

我收到这样的错误:

File "/usr/local/lib/python2.7/site-packages/pysnmp/smi/builder.py", line 130, in _init
    p = __import__(self._srcName, globals(), locals(), ['__init__'])
File "syzf-ALARMS.py", line 8, in <module>
    ( Integer, ObjectIdentifier, OctetString, ) = mibBuilder.importSymbols("ASN1", "Integer", "ObjectIdentifier", "OctetString")
NameError: name 'mibBuilder' is not defined

可能是什么原因?以上步骤可以吗?

1 个答案:

答案 0 :(得分:0)

如果您使用的是最新的pysnmp,则代码应如下所示:

from pysnmp.hlapi import *

errorIndication, errorStatus, errorIndex, varBinds = next(
    getCmd(SnmpEngine(),
           CommunityData('public'),
           UdpTransportTarget(('localhost', 161)),
           ContextData(),
           ObjectType(ObjectIdentity('syzf-ALARMS', 'someObject', 0)))
)

其中 someObject 是指您在MIB中定义的标量MIB对象(OBJECT-TYPE)。

如果它位于〜/ .pysnmp / mibs / 中,则无需指定MIB模块的路径 - 此路径硬连线到pysnmp。

此外,您可以省略显式 mibdump.py 编译阶段,因为最新的pysnmp会自动为您的ASN.1 MIB源调用MIB编译模块(pysmi)。您可能需要通过 addAsn1MibSource()指定ASN.1 MIB的路径,如here所述。