{"id":3767,"library":"pysmi","title":"PySMI: SNMP MIB Parser and Converter","description":"PySMI is a pure-Python implementation of an SNMP SMI MIB parser and conversion library. It takes ASN.1 MIBs and can transform them into various formats, including JSON documents and PySNMP-compatible Python modules. It understands SMIv1, SMIv2, and common de-facto SMI dialects, and can automatically pull MIBs from local directories, ZIP archives, and HTTP servers. The current version is 1.6.3 and it actively supports Python 3.9+.","status":"active","version":"1.6.3","language":"en","source_language":"en","source_url":"https://github.com/lextudio/pysmi","tags":["SNMP","MIB","SMI","parser","compiler","telecommunications"],"install":[{"cmd":"pip install pysmi","lang":"bash","label":"Install PySMI"}],"dependencies":[{"reason":"Required if generating PySNMP-compatible Python modules for use with pysnmp, but not a direct runtime dependency for core parsing/JSON generation.","package":"pysnmp","optional":true}],"imports":[{"symbol":"MibCompiler","correct":"from pysmi.compiler import MibCompiler"},{"symbol":"SmiV2Parser","correct":"from pysmi.parser.smi import SmiV2Parser"},{"symbol":"JsonCodeGen","correct":"from pysmi.codegen.json import JsonCodeGen"},{"symbol":"PySnmpCodeGen","correct":"from pysmi.codegen.pysnmp import PySnmpCodeGen"},{"symbol":"FileReader","correct":"from pysmi.reader.localfile import FileReader"},{"symbol":"CallbackWriter","correct":"from pysmi.writer.callback import CallbackWriter"}],"quickstart":{"code":"import os\nfrom pysmi.compiler import MibCompiler\nfrom pysmi.parser.smi import SmiV2Parser\nfrom pysmi.codegen.json import JsonCodeGen\nfrom pysmi.reader.callback import CallbackReader\nfrom pysmi.writer.callback import CallbackWriter\n\n# Example MIB content (simplified for demonstration)\nmib_content = \"\"\"\nFOO-MIB DEFINITIONS ::= BEGIN\n  IMPORTS\n    MODULE-IDENTITY, OBJECT-TYPE, enterprises\n      FROM SNMPv2-SMI;\n\n  fooModule MODULE-IDENTITY\n    LAST-UPDATED \"202301010000Z\"\n    ORGANIZATION \"Example Corp\"\n    CONTACT-INFO \"support@example.com\"\n    DESCRIPTION\n      \"The MIB module for example devices.\"\n    REVISION \"202301010000Z\"\n    DESCRIPTION \"Initial revision.\"\n    ::= { enterprises 99999 }\n\n  fooObjects OBJECT IDENTIFIER ::= { fooModule 1 }\n\n  fooStatus OBJECT-TYPE\n    SYNTAX INTEGER { up(1), down(2) }\n    MAX-ACCESS read-only\n    STATUS current\n    DESCRIPTION\n      \"The operational status of a foo device.\"\n    ::= { fooObjects 1 }\n\nEND\n\"\"\"\n\ndef store_json_mib(mib_name, mib_data, *args):\n    print(f\"--- Compiled MIB: {mib_name} ---\")\n    print(mib_data)\n\n# Initialize compiler infrastructure\nmibCompiler = MibCompiler(\n    SmiV2Parser(),\n    JsonCodeGen(),\n    CallbackWriter(store_json_mib)\n)\n\n# Add a reader that can provide our MIB content by name\nmibCompiler.addSources(CallbackReader(lambda mibName, **kwargs: mib_content if mibName == 'FOO-MIB' else None))\n\n# Compile the MIB\n# Note: 'rebuild=True' forces compilation even if cached, 'genTexts=True' includes MIB descriptions\nresults = mibCompiler.compile('FOO-MIB', rebuild=True, genTexts=True)\n\nprint(f\"Compilation results: {results}\")\n","lang":"python","description":"This quickstart demonstrates how to compile an in-memory ASN.1 MIB module (`FOO-MIB`) into a JSON document using PySMI's `MibCompiler`. It sets up a `CallbackReader` to supply the MIB content and a `CallbackWriter` to print the resulting JSON to the console, illustrating the core transformation process."},"warnings":[{"fix":"Pass `genTexts=True` to the `MibCompiler.compile()` method to include full textual descriptions and references from the MIB.","message":"By default, PySMI does not include MIB descriptions, comments, or references in the generated output (e.g., JSON or PySNMP modules) to minimize size. This can lead to a less verbose output than expected.","severity":"gotcha","affected_versions":"All versions"},{"fix":"To force unconditional recompilation, pass `rebuild=True` to the `MibCompiler.compile()` method. For parser lookup tables, configure a cache directory using `--cache-directory` option (if using `mibdump.py`) or similar API for performance.","message":"PySMI performs caching and will not recompile a MIB if a 'fresh enough' version already exists in its search paths. This can be unexpected during development or when changes are made to source MIBs.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Explicitly 'blacklist' such MIBs using `StubSearcher` in your `MibCompiler` configuration. `PySnmpCodeGen.baseMibs` provides a pre-defined list for PySNMP target generation. For example: `mibCompiler.addSearchers(StubSearcher(*PySnmpCodeGen.baseMibs))`.","message":"Some foundational MIBs (e.g., SNMPv2-SMI, SNMPv2-TC, RFC-1212) contain base SMI types or ASN.1 MACRO definitions that should generally not be transformed by PySMI. Attempting to compile these can lead to errors or incorrect output.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Utilize PySMI's 'borrow' feature by configuring a source for pre-compiled MIBs. This allows PySMI to fetch already transformed MIBs if the source ASN.1 MIB cannot be found or parsed. Example: `mibCompiler.addSearchers(PyFileSearcher('/path/to/precompiled/mibs'))` or an `HttpReader` to `mibs.snmplabs.com`.","message":"Poorly formed or 'broken' ASN.1 MIBs are common in practice and can cause parsing failures. While PySMI attempts workarounds, severely broken MIBs may not compile.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}