MIB tweaks

Waive 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
  • do not resolve response OIDs and values into human-freidly form

False lookupMib keyword arguments could make pysnmp waiving OIDs and values resolution in response variable-bindings, into human friendly form.

Functionally similar to:

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

for (errorIndication,
     errorStatus,
     errorIndex,
     varBinds) in nextCmd(SnmpEngine(),
                          CommunityData('public'),
                          UdpTransportTarget(('demo.snmplabs.com', 161)),
                          ContextData(),
                          ObjectType(ObjectIdentity('1.3.6.1.2.1.1')),
                          lookupMib=False):

    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 SNMPv3 with user ‘usr-md5-des’, MD5 auth and DES privacy protocols
  • over IPv6/UDP
  • to an Agent at [::1]:161
  • for all OIDs starting from 1.3.6
  • preload all Python MIB modules found in search path

Functionally similar to:

$ snmpwalk -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 -m ALL udp6:[::1]:161 1.3.6
from pysnmp.hlapi import *

for (errorIndication,
     errorStatus,
     errorIndex,
     varBinds) in nextCmd(SnmpEngine(),
                          UsmUserData('usr-md5-des', 'authkey1', 'privkey1'),
                          Udp6TransportTarget(('::1', 161)),
                          ContextData(),
                          ObjectType(ObjectIdentity('1.3.6').loadMibs())):

    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

Functionally similar to:

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

errorIndication, errorStatus, errorIndex, varBinds = next(
    getCmd(SnmpEngine(),
           CommunityData('public'),
           UdpTransportTarget(('demo.snmplabs.com', 161)),
           ContextData(),
           ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1).addAsn1MibSource('file:///usr/share/snmp',
                                                                                 'http://mibs.snmplabs.com/asn1/@mib@')))
)

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 SNMPv3, user ‘usr-none-none’, no authentication, no privacy
  • 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

Functionally similar to:

$ snmpbulkwalk -v3 -lnoAuthNoPriv -u usr-none-none -Cn0 -Cr50 demo.snmplabs.com TCP-MIB::tcpConnTable
from pysnmp.hlapi import *

for (errorIndication,
     errorStatus,
     errorIndex,
     varBinds) in bulkCmd(SnmpEngine(),
                          UsmUserData('usr-none-none'),
                          UdpTransportTarget(('demo.snmplabs.com', 161)),
                          ContextData(),
                          0, 50,
                          ObjectType(
                              ObjectIdentity('TCP-MIB', 'tcpConnTable').addMibSource('/opt/mibs/pysnmp').addMibSource('python_packaged_mibs')
                          ),
                          lexicographicMode=False):

    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.