summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorelie <elie>2015-05-17 21:01:43 +0000
committerelie <elie>2015-05-17 21:01:43 +0000
commita4fb54639e97cb572689e24a5c5e42b4f66a62e0 (patch)
tree96ae7dcd54ba6cbc090d33018993f4f993bab72e /examples
parent1118e0d43b694f8a1b68e4d0b9d72b2e7e6c8277 (diff)
downloadpysnmp-a4fb54639e97cb572689e24a5c5e42b4f66a62e0.tar.gz
- Initial PySMI integration. Original ASN.1 MIBs could now be parsed, stored
at a local pysnmp MIBs repository and loaded into SNMP Engine. - smi.MibBuilder will now raise more specific exceptions (MibLoadError, MibNotFoundError) on MIB loading problems rather than more generic SmiError. - MibBuilder.addMibSources() convenience method added.
Diffstat (limited to 'examples')
-rw-r--r--examples/smi/view.py20
-rw-r--r--examples/v3arch/oneliner/manager/cmdgen/get-v2c-with-asn1-mib-lookup.py37
2 files changed, 48 insertions, 9 deletions
diff --git a/examples/smi/view.py b/examples/smi/view.py
index e0134c2..549ea36 100644
--- a/examples/smi/view.py
+++ b/examples/smi/view.py
@@ -1,21 +1,23 @@
# SNMP manager-side MIB management
-from pysnmp.smi import builder, view, error
+from pysnmp.smi import builder, view, compiler, error
# Create MIB loader/builder
mibBuilder = builder.MibBuilder()
-# Optionally set an alternative path to compiled MIBs
-#print('Setting MIB sources...')
-#mibSources = mibBuilder.getMibSources() + (
-# builder.DirMibSource('/opt/pysnmp_mibs'),
-# )
-#mibBuilder.setMibSources(*mibSources)
-#print(mibBuilder.getMibSources())
+# Optionally attach PySMI MIB compiler (if installed)
+#print('Attaching MIB compiler...'),
+#compiler.addMibCompiler(mibBuilder, sources=['/usr/share/snmp/mibs'])
#print('done')
+# Optionally set an alternative path to compiled MIBs
+print('Setting MIB sources...')
+mibBuilder.addMibSources(builder.DirMibSource('/opt/pysnmp_mibs'))
+print(mibBuilder.getMibSources())
+print('done')
+
print('Loading MIB modules...'),
mibBuilder.loadModules(
- 'SNMPv2-MIB', 'SNMP-FRAMEWORK-MIB', 'SNMP-COMMUNITY-MIB'
+ 'SNMPv2-MIB', 'SNMP-FRAMEWORK-MIB', 'SNMP-COMMUNITY-MIB', 'IP-MIB'
)
print('done')
diff --git a/examples/v3arch/oneliner/manager/cmdgen/get-v2c-with-asn1-mib-lookup.py b/examples/v3arch/oneliner/manager/cmdgen/get-v2c-with-asn1-mib-lookup.py
new file mode 100644
index 0000000..a8f1e73
--- /dev/null
+++ b/examples/v3arch/oneliner/manager/cmdgen/get-v2c-with-asn1-mib-lookup.py
@@ -0,0 +1,37 @@
+#
+# Command Generator
+#
+# 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
+#
+from pysnmp.entity.rfc3413.oneliner import cmdgen
+from pysnmp import debug
+
+#debug.setLogger(debug.Debug('mibbuild'))
+
+cmdGen = cmdgen.CommandGenerator()
+
+errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
+ cmdgen.CommunityData('public'),
+ cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)),
+ cmdgen.MibVariable('IF-MIB', 'ifInOctets', '1').addAsn1Sources('file:///usr/share/snmp', 'http://mibs.snmplabs.com/asn1/<mib>'),
+ lookupNames=True, lookupValues=True
+)
+
+# Check for errors and print out results
+if errorIndication:
+ print(errorIndication)
+else:
+ if errorStatus:
+ print('%s at %s' % (
+ errorStatus.prettyPrint(),
+ errorIndex and varBinds[int(errorIndex)-1][0] or '?'
+ )
+ )
+ else:
+ for name, val in varBinds:
+ print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))