2

I try to load the custom MIB file generated in System32 (HOST-RESSOURCES_MIB) from Windows with PYSNMP.

Code is the following:

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]))

I get an error related to the host object identifier which can't be read:

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

The MIB file beginning is the following:

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 }

Seems like the host OBJECT IDENTIFIER is the problem but I can't find the solution.

Is there a workaround to allow pysnmp reading the host identifier ?

Fusse
  • 21
  • 1
  • You need to serve the standard MIB documents as dependencies, such as the ones I coped from NET-SNMP https://github.com/lextm/sharpsnmppro-mib – Lex Li Jun 10 '20 at 18:20

1 Answers1

0

1) Module to be installed

pip install pysnmp
pip install snmpclitools
pip install pyasn1
pip install pyasn1-modules
pip install pysmi
pip install pysnmpcrypto

2) compile your 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) check if the the file mymib.my exists :

Python37\Lib\site-packages\pysnmp\smi\mibs\mymib.py
Houda
  • 671
  • 6
  • 16