diff options
author | elie <elie> | 2015-05-31 19:37:32 +0000 |
---|---|---|
committer | elie <elie> | 2015-05-31 19:37:32 +0000 |
commit | ef7cd19afa50ea3bfdc7eb41bef1fb277de8760e (patch) | |
tree | 289490f28e27a8d69d9b69326200897b6d433e41 /examples | |
parent | bea11dfe16e6714737b6f5016697206a038f051b (diff) | |
download | pysnmp-ef7cd19afa50ea3bfdc7eb41bef1fb277de8760e.tar.gz |
missing example added
Diffstat (limited to 'examples')
-rw-r--r-- | examples/smi/mib-lookup.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/examples/smi/mib-lookup.py b/examples/smi/mib-lookup.py new file mode 100644 index 0000000..12fcfb5 --- /dev/null +++ b/examples/smi/mib-lookup.py @@ -0,0 +1,64 @@ +# SNMP variables MIB resolution +from pysnmp.smi import builder, view, rfc1902, error + +# MIB Builder manages pysnmp MIBs +mibBuilder = builder.MibBuilder() + +# MIB View Controller implements various queries to loaded MIBs +mibView = view.MibViewController(mibBuilder) + +# Obtain MIB object information by MIB object name +mibVar = rfc1902.ObjectIdentity('IF-MIB', 'ifInOctets', 1) + +# Optionally attach PySMI MIB compiler to MIB Builder that would +# create pysnmp MIBs on demand from ASN.1 sources downloaded from +# a web site. +try: + mibVar.addMibCompiler('http://mibs.snmplabs.com/asn1/<mib>') +except error.SmiError: + print('WARNING: not using MIB compiler (PySMI not installed)') + +mibVar.resolveWithMib(mibView) + +print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) + +# Obtain MIB object information by its [sequence] OID +mibVar = rfc1902.ObjectIdentity(tuple(mibVar)).resolveWithMib(mibView) + +print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) + +# Obtain MIB object information by its [string] OID +mibVar = rfc1902.ObjectIdentity(str(mibVar)).resolveWithMib(mibView) + +print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) + +# Create an OID-value pair (called variable-binding in SNMP) +varBind = rfc1902.ObjectType( + rfc1902.ObjectIdentity('SNMPv2-MIB', 'sysObjectID', 0), '1.3.6.1' +).resolveWithMib(mibView) + +print(varBind[0].prettyPrint(), varBind[1].__class__.__name__, varBind[1].prettyPrint()) + +# Create just OID +varBind = rfc1902.ObjectType( + rfc1902.ObjectIdentity('SNMPv2-MIB', 'sysObjectID', 0) +).resolveWithMib(mibView) + +print(varBind[0].prettyPrint(), varBind[1].__class__.__name__, varBind[1].prettyPrint()) + +# Create var-binds from MIB notification object (without OBJECTS clause) +varBinds = rfc1902.NotificationType( + rfc1902.ObjectIdentity('SNMPv2-MIB', 'coldStart') +).resolveWithMib(mibView) + +print([ '%s = %s(%s)' % (x[0].prettyPrint(), x[1].__class__.__name__, x[1].prettyPrint()) for x in varBinds]) + +# Create var-binds from MIB notification object (with OBJECTS clause) +varBinds = rfc1902.NotificationType( + rfc1902.ObjectIdentity('IF-MIB', 'linkUp'), + instanceIndex = (1,), + objects= { ('IF-MIB', 'ifOperStatus'): 'down' } +).resolveWithMib(mibView) + +print(varBinds.prettyPrint()) + |