summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorIlya Etingof <ietingof@redhat.com>2016-05-11 12:50:44 +0200
committerIlya Etingof <ietingof@redhat.com>2016-05-11 12:50:44 +0200
commitf96e5db5fb2bd5df2d4848c803c410e8dc11ecc2 (patch)
tree23cc59579ae4bb793e70705864869e7ff89e578d /examples
parenta1b34606a6fce1cf30e6717be6559cf0befaa835 (diff)
downloadpysnmp-git-f96e5db5fb2bd5df2d4848c803c410e8dc11ecc2.tar.gz
better examples on MIB compiler
Diffstat (limited to 'examples')
-rw-r--r--examples/smi/manager/configure-mib-viewer-and-resolve-pdu-varbinds.py37
-rw-r--r--examples/smi/manager/convert-between-pdu-varbinds-and-mib-objects.py (renamed from examples/smi/manager/var-binds-mib-resolution.py)9
2 files changed, 43 insertions, 3 deletions
diff --git a/examples/smi/manager/configure-mib-viewer-and-resolve-pdu-varbinds.py b/examples/smi/manager/configure-mib-viewer-and-resolve-pdu-varbinds.py
new file mode 100644
index 00000000..91d7e5ee
--- /dev/null
+++ b/examples/smi/manager/configure-mib-viewer-and-resolve-pdu-varbinds.py
@@ -0,0 +1,37 @@
+"""
+SNMP var-binds MIB resolution (configure MIB compiler)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+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.addMibCompiler(mibBuilder, sources=['file:///usr/share/snmp/mibs',
+ 'http://mibs.snmplabs.com/asn1/@mib@'])
+
+# Pre-load MIB modules we expect to work with
+mibBuilder.loadModules('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]).resolveWithMib(mibViewController) for x in varBinds]
+
+for varBind in varBinds:
+ print(varBind.prettyPrint())
diff --git a/examples/smi/manager/var-binds-mib-resolution.py b/examples/smi/manager/convert-between-pdu-varbinds-and-mib-objects.py
index cee8b7c0..ecc798d3 100644
--- a/examples/smi/manager/var-binds-mib-resolution.py
+++ b/examples/smi/manager/convert-between-pdu-varbinds-and-mib-objects.py
@@ -2,10 +2,13 @@
SNMP var-binds MIB resolution
+++++++++++++++++++++++++++++
-This script explains how Python application (typically pysnmp-based SNMP Manager)
-could enrich SNMP PDU variable-bindings with MIB information or convert MIB objects
-into variable-bindings.
+This script explains how Python application (typically pysnmp-based
+SNMP Manager) could turn SNMP PDU variable-bindings into MIB objects
+or the other way around.
+The code below does not explicitly add MIB compiler - that happens
+behind the scenes. Examples below try to demo different kinds
+of MIB objects to work with.
"""#
from pysnmp.smi import builder, view, rfc1902, error