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 request/response OIDs and values from/toto human-friendly form
The lookupMib=False keyword argument makes pysnmp NOT 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 import *
iterator = nextCmd(
SnmpEngine(),
CommunityData('public'),
UdpTransportTarget(('demo.snmplabs.com', 161)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.2.1.1')),
lookupMib=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.
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 IPv4/UDP
- to an Agent at demo.snmplabs.com: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 demo.snmplabs.com:161 1.3.6
from pysnmp.hlapi import *
iterator = nextCmd(
SnmpEngine(),
UsmUserData('usr-md5-des', 'authkey1', 'privkey1'),
UdpTransportTarget(('demo.snmplabs.com', 161)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6').loadMibs())
)
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
Functionally similar to:
$ snmpget -v2c -c public -M /usr/share/snmp demo.snmplabs.com IF-MIB::ifInOctets.1
from pysnmp.hlapi import *
iterator = 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@'
)
)
)
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 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. If not found, TCP-MIB will be downloaded from the web, compiled into Python and cached for further use.
Functionally similar to:
$ snmpbulkwalk -v3 -lnoAuthNoPriv -u usr-none-none -Cn0 -Cr50 demo.snmplabs.com TCP-MIB::tcpConnTable
from pysnmp.hlapi import *
iterator = 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'
)
).addAsn1MibSource('http://mibs.snmplabs.com/asn1/@mib@'),
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.