PySMI (lextudio)

1.4.3 · active · verified Fri Apr 17

PySMI (lextudio) is a pure-Python library for parsing and converting SNMP/SMI MIB definitions. It provides tools to work with MIB files, resolve dependencies, and generate various output formats, including PySNMP-compatible Python modules. The current version is 1.4.3 and it maintains an active, though not rapid, release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse a simple MIB file using PySMI. It creates a temporary MIB file and a dummy 'SNMPv2-SMI' for basic dependency resolution, then uses `SmiBuilder` and `FileReader` to load and verify the MIB module.

import os
import tempfile
from pysmi.builder import SmiBuilder
from pysmi.reader import FileReader

mib_content = """
DUMMY-MIB DEFINITIONS ::= BEGIN
    IMPORTS
        MODULE-IDENTITY, OBJECT-TYPE, enterprises
            FROM SNMPv2-SMI;

    dummyMIB MODULE-IDENTITY
        LAST-UPDATED "202310260000Z"
        ORGANIZATION "Example"
        CONTACT-INFO "example@example.com"
        DESCRIPTION
            "A dummy MIB for testing purposes."
        REVISION "202310260000Z"
        DESCRIPTION
            "Initial revision."
        ::= { enterprises 99999 }

    dummyObject OBJECT-TYPE
        SYNTAX      INTEGER
        MAX-ACCESS  read-only
        STATUS      current
        DESCRIPTION
            "A dummy object."
        ::= { dummyMIB 1 }

END
"""

with tempfile.TemporaryDirectory() as temp_dir:
    mib_path = os.path.join(temp_dir, "DUMMY-MIB.mib")
    # PySMI often needs standard MIBs; for simplicity, we mock an empty SNMPv2-SMI
    # In a real scenario, you'd point to a directory with actual standard MIBs
    snmpv2_smi_path = os.path.join(temp_dir, "SNMPv2-SMI.mib")
    with open(snmpv2_smi_path, "w") as f:
        f.write("SNMPv2-SMI DEFINITIONS ::= BEGIN END")

    with open(mib_path, "w") as f:
        f.write(mib_content)

    mibBuilder = SmiBuilder()
    mibBuilder.addMibSources(FileReader(temp_dir))

    try:
        mibBuilder.loadModules("DUMMY-MIB")
        # Retrieve the MIB node by name or OID to verify
        if mibBuilder.getMibNode(('DUMMY-MIB', 1)):
            print("Successfully loaded 'DUMMY-MIB' and found 'dummyObject'.")
        else:
            print("Failed to find 'dummyObject' within 'DUMMY-MIB'.")
    except Exception as e:
        print(f"Error loading MIB: {e}")

view raw JSON →