diff options
author | elie <elie> | 2012-12-09 21:10:26 +0000 |
---|---|---|
committer | elie <elie> | 2012-12-09 21:10:26 +0000 |
commit | 32f2510ed97944858b29c880b7cf95da0b3a1758 (patch) | |
tree | fac7257f456e39ff4023d384dc8fc1533d748461 /examples/v3arch/manager/cmdgen/getnext-v1.py | |
parent | dda11e834107d039d3bf391ec17f18cbab7340c1 (diff) | |
download | pysnmp-32f2510ed97944858b29c880b7cf95da0b3a1758.tar.gz |
SNMPv3 native API examples extended to cover many use cases
Diffstat (limited to 'examples/v3arch/manager/cmdgen/getnext-v1.py')
-rw-r--r-- | examples/v3arch/manager/cmdgen/getnext-v1.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/examples/v3arch/manager/cmdgen/getnext-v1.py b/examples/v3arch/manager/cmdgen/getnext-v1.py new file mode 100644 index 0000000..75a2857 --- /dev/null +++ b/examples/v3arch/manager/cmdgen/getnext-v1.py @@ -0,0 +1,81 @@ +# +# GETNEXT Command Generator +# +# Send a series of SNMP GETNEXT requests +# with SNMPv1, community 'public' +# over IPv4/UDP +# to an Agent at 127.0.0.1:161 +# for two OIDs in tuple form +# stop on end-of-mib condition for both OIDs +# +# This script performs similar to the following Net-SNMP command: +# +# $ snmpwalk -v1 -c public -ObentU 127.0.0.1 1.3.6.1.2.1.1 1.3.6.1.4.1.1 +# +# GETNEXT Command Generator +from pysnmp.entity import engine, config +from pysnmp.carrier.asynsock.dgram import udp +from pysnmp.entity.rfc3413 import cmdgen + +# Create SNMP engine instance +snmpEngine = engine.SnmpEngine() + +# +# SNMPv1/2c setup +# + +# SecurityName <-> CommunityName mapping +config.addV1System(snmpEngine, 'my-area', 'public') + +# Specify security settings per SecurityName (SNMPv1 - 0, SNMPv2c - 1) +config.addTargetParams(snmpEngine, 'my-creds', 'my-area', 'noAuthNoPriv', 0) + +# +# Setup transport endpoint and bind it with security settings yielding +# a target name (choose one entry depending of the transport needed). +# + +# UDP/IPv4 +config.addSocketTransport( + snmpEngine, + udp.domainName, + udp.UdpSocketTransport().openClientMode() +) +config.addTargetAddr( + snmpEngine, 'my-router', + udp.domainName, ('127.0.0.1', 161), + 'my-creds' +) + +# Error/response reciever +def cbFun(sendRequestHandle, + errorIndication, errorStatus, errorIndex, + varBindTable, cbCtx): + if errorIndication: + print(errorIndication) + return + # SNMPv1 response may contain noSuchName error *and* SNMPv2c exception, + # so we ignore noSuchName error here + if errorStatus and errorStatus != 2: + print('%s at %s' % ( + errorStatus.prettyPrint(), + errorIndex and varBindTable[-1][int(errorIndex)-1] or '?' + ) + ) + return # stop on error + for varBindRow in varBindTable: + for oid, val in varBindRow: + print('%s = %s' % (oid.prettyPrint(), val.prettyPrint())) + return 1 # signal dispatcher to continue + +# Prepare initial request to be sent +cmdgen.NextCommandGenerator().sendReq( + snmpEngine, + 'my-router', + ( ((1,3,6,1,2,1,1), None), + ((1,3,6,1,4,1,1), None), ), + cbFun +) + +# Run I/O dispatcher which would send pending queries and process responses +snmpEngine.transportDispatcher.runDispatcher() |