PYSNMP无法读取自定义windows MIB

2024-10-02 18:16:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用PYSNMP从Windows加载System32(HOST-Resources_MIB)中生成的自定义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读取主机标识符的解决方法


Tags: fromdebughostobjectmibresourcesidentifierfailed
1条回答
网友
1楼 · 发布于 2024-10-02 18:16:15

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

相关问题 更多 >