File: configure-mib-viewer-and-resolve-pdu-varbinds.py

package info (click to toggle)
python-pysnmp4 7.1.21-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,564 kB
  • sloc: python: 33,654; makefile: 166; javascript: 4
file content (44 lines) | stat: -rw-r--r-- 1,311 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
PDU var-binds to MIB objects
++++++++++++++++++++++++++++

This script explains how Python application could turn SNMP PDU
variable-bindings into MIB objects or the other way around.

The code that configures MIB compiler is similar to what
happens inside the pysnmp.hlapi API.
"""  #
from pysnmp.smi import builder, view, compiler, rfc1902

# Assemble MIB browser
mibBuilder = builder.MibBuilder()
mibViewController = view.MibViewController(mibBuilder)
compiler.add_mib_compiler(
    mibBuilder,
    sources=["file:///usr/share/snmp/mibs", "https://mibs.pysnmp.com/asn1/@mib@"],
)

# Pre-load MIB modules we expect to work with
mibBuilder.load_modules("SNMPv2-MIB", "SNMP-COMMUNITY-MIB")

# This is what we can get in TRAP PDU
varBinds = [
    ("1.3.6.1.2.1.1.3.0", 12345),
    ("1.3.6.1.6.3.1.1.4.1.0", "1.3.6.1.6.3.1.1.5.2"),
    ("1.3.6.1.6.3.18.1.3.0", "0.0.0.0"),
    ("1.3.6.1.6.3.18.1.4.0", ""),
    ("1.3.6.1.6.3.1.1.4.3.0", "1.3.6.1.4.1.20408.4.1.1.2"),
    ("1.3.6.1.2.1.1.1.0", "my system"),
]

# Run var-binds through MIB resolver
# You may want to catch and ignore resolution errors here
varBinds = [
    rfc1902.ObjectType(rfc1902.ObjectIdentity(x[0]), x[1]).resolve_with_mib(
        mibViewController
    )
    for x in varBinds
]

for varBind in varBinds:
    print(varBind.prettyPrint())