MIB tweaks

Enable MIB lookup

Perform SNMP GETNEXT operation with the following options:

  • with SNMPv2c, community ‘public’
  • over IPv4/UDP
  • to an Agent at demo.snmplabs.com:161
  • for an OID in string form
  • resolve request and response OIDs and values from/to human-friendly form

The lookupMib=True keyword argument makes pysnmp resolving request and response variable-bindings from/to human-friendly form.

Functionally similar to:

$ snmpwalk -v2c -c public -ObentU demo.snmplabs.com 1.3.6.1.2.1
from pysnmp.hlapi.v1arch import *

iterator = nextCmd(
    SnmpDispatcher(),
    CommunityData('public'),
    UdpTransportTarget(('demo.snmplabs.com', 161)),
    ObjectType(ObjectIdentity('1.3.6.1.2.1.1')),
    lookupMib=True
)

for errorIndication, errorStatus, errorIndex, varBinds in iterator:

    if errorIndication:
        print(errorIndication)
        break

    elif errorStatus:
        print('%s at %s' % (errorStatus.prettyPrint(),
                            errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
        break

    else:
        for varBind in varBinds:
            print(' = '.join([x.prettyPrint() for x in varBind]))

Download script.

Preload PySNMP MIBs

Send a series of SNMP GETNEXT requests using the following options:

  • with SNMPv2c, community name “public”
  • over IPv4/UDP
  • to an Agent at demo.snmplabs.com:161
  • for all OIDs starting from 1.3.6
  • with MIB lookup enabled
  • preload all Python MIB modules found in search path

Functionally similar to:

$ snmpwalk -v2c -c public -m ALL demo.snmplabs.com:161 1.3.6
from pysnmp.hlapi.v1arch import *

iterator = nextCmd(
    SnmpDispatcher(),
    CommunityData('public'),
    UdpTransportTarget(('demo.snmplabs.com', 161)),
    ObjectType(ObjectIdentity('1.3.6').loadMibs()),
    lookupMib=True
)

for errorIndication, errorStatus, errorIndex, varBinds in iterator:

    if errorIndication:
        print(errorIndication)
        break

    elif errorStatus:
        print('%s at %s' % (errorStatus.prettyPrint(),
                            errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
        break

    else:
        for varBind in varBinds:
            print(' = '.join([x.prettyPrint() for x in varBind]))

Download script.

Custom ASN.1 MIB path

Send SNMP GET request using the following options:

  • with SNMPv2c, community ‘public’
  • over IPv4/UDP
  • to an Agent at demo.snmplabs.com:161
  • for IF-MIB::ifInOctets.1 MIB object
  • pass non-default ASN.1 MIB source to MIB compiler
  • with MIB lookup enabled

Functionally similar to:

$ snmpget -v2c -c public -M /usr/share/snmp demo.snmplabs.com IF-MIB::ifInOctets.1
from pysnmp.hlapi.v1arch import *

iterator = getCmd(
    SnmpDispatcher(),
    CommunityData('public'),
    UdpTransportTarget(('demo.snmplabs.com', 161)),
    ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1).addAsn1MibSource(
        'file:///usr/share/snmp',
        'http://mibs.snmplabs.com/asn1/@mib@')
    ),
    lookupMib=True
)

errorIndication, errorStatus, errorIndex, varBinds = next(iterator)

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

Download script.

Custom PySNMP MIBs location

Send a series of SNMP GETBULK requests using the following options:

  • with SNMPv2c, community ‘public’
  • over IPv4/UDP
  • to an Agent at demo.snmplabs.com:161
  • for all OIDs within TCP-MIB::tcpConnTable column
  • TCP-MIB Python module will be searched by a user-specified filesystem path (/opt/mib/pysnmp) and in Python package (python_packaged_mibs) which should be in sys.path. If not found, TCP-MIB will be downloaded from the web, compiled into Python and cached for further use.
  • with MIB lookup enabled

Functionally similar to:

$ snmpbulkwalk -v2c -c public -Cn0 -Cr50 demo.snmplabs.com TCP-MIB::tcpConnTable
from pysnmp.hlapi.v1arch import *

iterator = bulkCmd(
    SnmpDispatcher(),
    CommunityData('public'),
    UdpTransportTarget(('demo.snmplabs.com', 161)),
    0, 50,
    ObjectType(
      ObjectIdentity('TCP-MIB', 'tcpConnTable').addMibSource(
          '/opt/mibs/pysnmp').addMibSource(
          'python_packaged_mibs')
    ).addAsn1MibSource('http://mibs.snmplabs.com/asn1/@mib@'),
    lookupMib=True,
    lexicographicMode=False
)

for errorIndication, errorStatus, errorIndex, varBinds in iterator:

    if errorIndication:
        print(errorIndication)
        break

    elif errorStatus:
        print('%s at %s' % (errorStatus.prettyPrint(),
                            errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
        break

    else:
        for varBind in varBinds:
            print(' = '.join([x.prettyPrint() for x in varBind]))

Download script.

See also: library reference.