[英]Retrieving objectname from OID for custom MIB in pysnmp
I have a device connected to the network, that uses a custom .MIB-file with various 'parameters'. 我有一个连接到网络的设备,它使用带有各种“参数”的自定义.MIB文件。 I can use a MIB browser and find the OID for an object name such as 'powerSystemCompany'.
我可以使用MIB浏览器找到对象名称的OID,例如'powerSystemCompany'。
But for writing my code, I wish for a get command to see something alike: 但是为了编写我的代码,我希望获得一个get命令来看到类似的东西:
powerSystemCompany = CompanyName
Instead of: 代替:
SNMPv2-SMI::enterprises.12148.10.2.4 = CompanyName
As far as I can understand, I need to somehow compile my MIB file into a pysnmp mib or a JSON format and use the retrieved OID to look up the parameter 'powerSystemCompany'. 据我所知,我需要以某种方式将我的MIB文件编译为pysnmp mib或JSON格式,并使用检索到的OID查找参数'powerSystemCompany'。
But I remain unable to use either mibdump.py or pysnmi to get either .py or .json MIB. 但我仍然无法使用mibdump.py或pysnmi来获取.py或.json MIB。 When I try to use mibdump.py I have tried the following in my Anaconda prompt:
当我尝试使用mibdump.py时,我在Anaconda提示符中尝试了以下内容:
mibdump.py --mib-source='C:\\User\\$user$\\Documents\\pysnmp_Project\\mibs' --destination-format='json'
python mibdump.py --mib-source='C:\\User\\$user$\\Documents\\pysnmp_Project\\mibs' --destination-format='json'
But the first one just opens mibdump.py in my VScode editor and the second gives the error: 但第一个只在我的VScode编辑器中打开mibdump.py,第二个给出了错误:
'python" can't open file 'mibdump.py': [Errno 2] no such file or directory
I have also tried the pysnmi realization shown below: 我也尝试过如下所示的pysnmi实现:
from pysmi.reader import FileReader
from pysmi.searcher import StubSearcher
from pysmi.writer import CallbackWriter
from pysmi.parser import SmiStarParser
from pysmi.codegen import JsonCodeGen
from pysmi.compiler import MibCompiler
# from pysmi import debug
# debug.setLogger(debug.Debug('reader', 'compiler'))
inputMibs = 'SNMPv2-SMI'
srcDirectories = 'C://User//$user$//Documents//pysnmp_Project//mibs'
def printOut(mibName, jsonDoc, cbCtx):
print('\n\n# MIB module %s' % mibName)
print(jsonDoc)
# Initialize compiler infrastructure
mibCompiler = MibCompiler(
SmiStarParser(), JsonCodeGen(), CallbackWriter(printOut)
)
# search for source MIBs here
mibCompiler.addSources(*[FileReader(x) for x in srcDirectories])
# never recompile MIBs with MACROs
mibCompiler.addSearchers(StubSearcher(*JsonCodeGen.baseMibs))
# run recursive MIB compilation
results = mibCompiler.compile(*inputMibs)
print('\n# Results: %s' % ', '.join(['%s:%s' % (x, results[x]) for x in results]))
EDIT: But this seems to get stuck on the mibCompiler.compile(*inputMibs). 编辑:但这似乎卡在mibCompiler.compile(* inputMibs)上。 Using the example code using an HTTP request took only a few seconds, whereas I have waited for several minutes.
使用HTTP请求使用示例代码只需几秒钟,而我已等待几分钟。
The intention for this is automation of my lab equipment, which uses SNMP. 这样做的目的是实验室设备的自动化,它使用SNMP。
I hope I made myself clear, otherwise I'd love to elaborate. 我希望自己清楚明白,否则我很想详细说明。
I think you are overcomplicating the matter! 我觉得你太复杂了! A simple SNMP GET command having relevant MIB loaded should suffice:
加载相关MIB的简单SNMP GET命令应该足够:
from pysnmp.hlapi import *
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpEngine(),
CommunityData('public'),
UdpTransportTarget(('your.snmp.enabled.device.address', 161)),
ContextData(),
ObjectType(ObjectIdentity('YOUR-COMPANY-MIB', 'powerSystemCompany', 0)).addAsn1MibSource('file:///your/snmp/mibs/location')),
)
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]))
You may also need to set up a path to your ASN.1 (non-Python) MIBs so that pysnmp will find, load and compile them. 您可能还需要设置 ASN.1(非Python)MIB 的路径 ,以便pysnmp可以查找,加载和编译它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.