我尝试用PYSNMP从Windows中加载System32中生成的自定义MIB文件(HOST-RESSOURCES_MIB)。
代码如下。
from pysnmp.hlapi import *
from pysmi import debug as pysmi_debug
pysmi_debug.setLogger(pysmi_debug.Debug('compiler'))
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpEngine(),
CommunityData('public', mpModel=0),
UdpTransportTarget(('localhost', 161)),
ContextData(),
ObjectType(ObjectIdentity('HOST-RESOURCES-MIB', 'hrSystemUptime', 0).addAsn1MibSource('file:C:/Users/Fusse/Desktop/SNMP/')))
)
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
for varBind in varBinds:
print(' = '.join([x.prettyPrint() for x in varBind]))
我得到一个与主机对象标识符有关的错误,无法读取。
2020-06-10 13:06:22,601 pysmi: current MIB borrower(s):
2020-06-10 13:06:22,603 pysmi: trying source FileReader{"C:\Users\Fusse\Desktop\SNMP"}
2020-06-10 13:06:22,651 pysmi: failing on error Unknown parent symbol: mib_2 at MIB HOST-RESOURCES-MIB from FileReader{"C:\Users\Fusse\Desktop\SNMP"}
2020-06-10 13:06:22,651 pysmi: no HOST-RESOURCES-MIB found everywhere
2020-06-10 13:06:22,651 pysmi: MIBs analyzed 0, MIBs failed 1
2020-06-10 13:06:22,651 pysmi: MIBs parsed 0, MIBs failed 1
2020-06-10 13:06:22,651 pysmi: MIBs built 0, MIBs failed 1
2020-06-10 13:06:22,652 pysmi: MIBs available for borrowing 0, MIBs failed 1
2020-06-10 13:06:22,652 pysmi: MIBs built 0, MIBs failed 1
2020-06-10 13:06:22,652 pysmi: failing with problem MIBs HOST-RESOURCES-MIB
MIB文件的开头是这样的:
HOST-RESOURCES-MIB DEFINITIONS ::= BEGIN
IMPORTS
DisplayString FROM RFC1213-MIB
TimeTicks,
OBJECT-TYPE,
Counter, Gauge FROM RFC1155-SMI;
host OBJECT IDENTIFIER ::= { mib-2 25 }
hrSystem OBJECT IDENTIFIER ::= { host 1 }
hrStorage OBJECT IDENTIFIER ::= { host 2 }
hrDevice OBJECT IDENTIFIER ::= { host 3 }
hrSWRun OBJECT IDENTIFIER ::= { host 4 }
hrSWRunPerf OBJECT IDENTIFIER ::= { host 5 }
hrSWInstalled OBJECT IDENTIFIER ::= { host 6 }
似乎是主机对象标识符的问题,但我找不到解决办法。
有什么办法可以让pysnmp读取主机标识符?
1) 要安装的模块
pip install pysnmp
pip install snmpclitools
pip install pyasn1
pip install pyasn1-modules
pip install pysmi
pip install pysnmpcrypto
2) 编译你的mib。
rom pysmi.reader import FileReader
from pysmi.searcher import PyFileSearcher, PyPackageSearcher, StubSearcher
from pysmi.writer import PyFileWriter
from pysmi.parser import SmiStarParser
from pysmi.codegen import PySnmpCodeGen
from pysmi.compiler import MibCompiler
inputMibs = ['IF-MIB', 'IP-MIB']
srcDirectories = ['/usr/share/snmp/mibs']
dstDirectory = '.pysnmp-mibs'
# Initialize compiler infrastructure
mibCompiler = MibCompiler(SmiStarParser(),
PySnmpCodeGen(),
PyFileWriter(dstDirectory))
# search for source MIBs here
mibCompiler.addSources(*[FileReader(x) for x in srcDirectories])
# check compiled MIBs in our own productions
mibCompiler.addSearchers(PyFileSearcher(dstDirectory))
# ...and at default PySNMP MIBs packages
mibCompiler.addSearchers(*[PyPackageSearcher(x) for x in PySnmpCodeGen.defaultMibPackages])
# never recompile MIBs with MACROs
mibCompiler.addSearchers(StubSearcher(*PySnmpCodeGen.baseMibs))
# run [possibly recursive] MIB compilation
results = mibCompiler.compile(*inputMibs) #, rebuild=True, genTexts=True)
print('Results: %s' % ', '.join(['%s:%s' % (x, results[x]) for x in results]))
3) 检查mymib.my文件是否存在。
Python37\Lib\site-packages\pysnmp\smi\mibs\mymib.py