我一直在尝试使自定义MIB无法正常工作。
理想情况下,我想从ASN1来源获得MIB的完整列表,但是由于这种失败,我尝试了多种选择。
我已经下载了IF-MIB和IANAifType-MIB,并复制了基于文本和PySNMP格式的编译版本,但似乎都看不到。 .py文件没有被编译。
2018-08-21 21:14:18,442 pysmi: running pysmi version 0.3.1
2018-08-21 21:14:18,442 pysmi: debug category 'searcher' enabled
2018-08-21 21:14:18,442 pysmi: debug category 'reader' enabled
2018-08-21 21:14:18,442 pysmi: debug category 'compiler' enabled
2018-08-21 21:14:19,595 pysmi: current MIB source(s): FileReader{"/usr/share/snmp/mibs"}, FileReader{"/usr/share/mibs"}
2018-08-21 21:14:19,595 pysmi: current compiled MIBs location(s): StubSearcher
2018-08-21 21:14:19,595 pysmi: current compiled MIBs location(s): StubSearcher, PyPackageSearcher{"/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs"}, PyPackageSearcher{"/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs/instances"}, PyPackageSearcher{"pysnmp_mibs"}
2018-08-21 21:14:19,595 pysmi: current MIB borrower(s):
2018-08-21 21:14:20,343 pysmi: current MIB source(s): FileReader{"/usr/share/snmp/mibs"}, FileReader{"/usr/share/mibs"}
2018-08-21 21:14:20,343 pysmi: current compiled MIBs location(s): StubSearcher
2018-08-21 21:14:20,343 pysmi: current compiled MIBs location(s): StubSearcher, PyPackageSearcher{"/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs"}, PyPackageSearcher{"/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs/instances"}, PyPackageSearcher{"pysnmp_mibs"}
2018-08-21 21:14:20,343 pysmi: current MIB borrower(s):
2018-08-21 21:14:22,527 pysmi: current MIB source(s): FileReader{"/usr/share/snmp/mibs"}, FileReader{"/usr/share/mibs"}
2018-08-21 21:14:22,527 pysmi: current compiled MIBs location(s): StubSearcher
2018-08-21 21:14:22,527 pysmi: current compiled MIBs location(s): StubSearcher, PyPackageSearcher{"/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs"}, PyPackageSearcher{"/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs/instances"}, PyPackageSearcher{"pysnmp_mibs"}
2018-08-21 21:14:22,527 pysmi: current MIB borrower(s):
:~$ ls /usr/share/snmp/mibs/I*
/usr/share/snmp/mibs/IANA-ADDRESS-FAMILY-NUMBERS-MIB.txt
/usr/share/snmp/mibs/IANAifType-MIB
/usr/share/snmp/mibs/IANAifType-MIB.py
/usr/share/snmp/mibs/IANAifType-MIB.txt
/usr/share/snmp/mibs/IANA-LANGUAGE-MIB.txt
/usr/share/snmp/mibs/IANA-RTPROTO-MIB.txt
/usr/share/snmp/mibs/IF-MIB
/usr/share/snmp/mibs/IF-MIB.py
:~$ ls /usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs/I*
/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs/IANAifType-MIB.py
/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs/IF-MIB.py
/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs/INET-ADDRESS-MIB.py
:~$ ls /usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs/__pycache__/I*
/usr/local/lib/python3.5/dist-packages/pysnmp/smi/mibs/__pycache__/INET-ADDRESS-MIB.cpython-35.pyc
我尝试使用以下方法从snmplabs网站导入:
from pysnmp.smi import builder
# load mibs
MIBDIR = 'http://mibs.snmplabs.com/asn1/@mib@'
mibBuilder = builder.MibBuilder()
mibSources = mibBuilder.getMibSources() + (builder.DirMibSource(MIBDIR),)
mibBuilder.setMibSources(*mibSources)
我还尝试了将.addAsn1MibSource('http://mibs.snmplabs.com/asn1/@mib@')
附加到cmdgen上的OID的末尾。
我仍然得到类似SNMPv2-SMI::mib-2.2.2.1.2.27 = Ethernet0/1/0.101
而不是IF-MIB::ifDescr.27 = STRING: Ethernet0/1/0.101
的输出。我还能做些什么来弄清楚自己在做错什么?
答案 0 :(得分:1)
尝试将.loadMibs('IF-MIB', ...)
附加到传递给cmdgen的ObjectType
对象上。事实是,如果您在查询时未明确引用MIB(例如使用OID),则pysnmp将不知道应该加载哪个MIB来解析响应变量。
如果您的自定义MIB是格式正确的ASN.1文档,则无需将其显式编译为pysnmp-pysnmp将调用pysmi为您编译MIB。
更新:
MIB在第一次.loadMibs()
调用中仅加载一次,此后一直保留在内存中。因此,从技术上讲,您无需在每次迭代中重复进行.loadMibs()
调用。
一种避免显式.loadMibs()
的方法是将查询中的托管对象作为MIB和符号对而不是OID传递。这将触发隐式MIB加载。
但这不适用于反向查找,例如如果您使用SNMP代理,并最终收到属于您从未加载的其他MIB的OID,则可以。在这种情况下,仍建议致电.loadMibs()
。
答案 1 :(得分:0)
@Ilya Etingof
对不起,由于声誉不足,无法回复评论。................请您提供上述答案的示例。我不断得到
pysnmp.smi.error.SmiError: ObjectType object not fully initialized
为
ObjectType(ObjectIdentity('1.3.6.1.2.1.25.2.3').loadMibs('HOST-RESOURCES-MIB'))
到目前为止,我所遵循的步骤是
from pysnmp.hlapi import ObjectIdentity, ObjectType
mibBuilder = builder.MibBuilder()
mibBuilder.addMibSources(builder.DirMibSource('C://Users//Downloads//HOST-RESOURCES-MIB.txt'))
errorIndication, errorStatus, errorIndex, \
varBindTable = cmdgen.CommandGenerator().bulkCmd(
cmdgen.CommunityData('test'),
cmdgen.UdpTransportTarget(('test-ip', 161)),
0,
1,
ObjectType(ObjectIdentity('1.3.6.1.2.1.25.2.3').loadMibs('HOST-RESOURCES-MIB'))
)
if errorIndication:
print(errorIndication)
else:
if errorStatus:
print('%s at %s\n' % (
errorStatus.prettyPrint(),
errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
))
else:
for varBindTableRow in varBindTable:
for name, val in varBindTableRow:
print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))