How to pass MIB to the Manager¶
How to make use of random MIBs at my Manager application?
Starting from PySNMP 4.3.x, plain-text (ASN.1) MIBs can be automatically parsed into PySNMP form by the PySMI tool. PySNMP will call PySMI automatically, parsed PySNMP MIB will be cached in $HOME/.pysnmp/mibs/ (default location).
MIB compiler could be configured to search for plain-text MIBs at multiple local and remote locations. As for remote MIB repos, you are welcome to use our collection of ASN.1 MIB files at mibs.pysnmp.com as shown below.
import asyncio
from pysnmp.hlapi.v3arch.asyncio import *
async def run():
snmpEngine = SnmpEngine()
errorIndication, errorStatus, errorIndex, varBinds = await get_cmd(
snmpEngine,
CommunityData("public"),
await UdpTransportTarget.create(("demo.pysnmp.com", 161)),
ContextData(),
ObjectType(
ObjectIdentity("IF-MIB", "ifInOctets", 1).add_asn1_mib_source(
"file:///usr/share/snmp", "https://mibs.pysnmp.com/asn1/@mib@"
)
),
)
if errorIndication:
print(errorIndication)
elif errorStatus:
print(
"{} at {}".format(
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
)
)
else:
for varBind in varBinds:
print(" = ".join([x.prettyPrint() for x in varBind]))
asyncio.run(run())
Download
script.
Alternatively, you can invoke the mibdump command (shipped with PySMI) by hand and this way compile plain-text MIB into PySNMP format. Once the compiled MIBs are stored in a directory, add the directory to your MibBuilder’s MibSources.
builder = engine.get_mib_builder()
# Make ./mibs available to all OIDs that are created
# e.g. with "MIB-NAME-MIB::identifier"
builder.add_mib_sources(builder_module.DirMibSource(
os.path.join( HERE, 'mibs')
))