简体   繁体   中英

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'. I can use a MIB browser and find the OID for an object name such as 'powerSystemCompany'.

But for writing my code, I wish for a get command to see something alike:

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'.

But I remain unable to use either mibdump.py or pysnmi to get either .py or .json MIB. When I try to use mibdump.py I have tried the following in my Anaconda prompt:

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:

'python" can't open file 'mibdump.py': [Errno 2] no such file or directory

I have also tried the pysnmi realization shown below:

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). Using the example code using an HTTP request took only a few seconds, whereas I have waited for several minutes.

The intention for this is automation of my lab equipment, which uses 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM