diff options
author | elie <elie> | 2012-12-09 21:10:26 +0000 |
---|---|---|
committer | elie <elie> | 2012-12-09 21:10:26 +0000 |
commit | 01f9e3a3f5dcc06fe5d8b5b4746c078286559cbc (patch) | |
tree | ac75889713453aec4c077e13c313e834d0bab8ba /examples/v3arch/asyncore/manager/cmdgen/usm-sha-none.py | |
parent | 0d03e50e509a30d9d5cc5ba37bb45683b00b4c1c (diff) | |
download | pysnmp-git-01f9e3a3f5dcc06fe5d8b5b4746c078286559cbc.tar.gz |
SNMPv3 native API examples extended to cover many use cases
Diffstat (limited to 'examples/v3arch/asyncore/manager/cmdgen/usm-sha-none.py')
-rw-r--r-- | examples/v3arch/asyncore/manager/cmdgen/usm-sha-none.py | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/examples/v3arch/asyncore/manager/cmdgen/usm-sha-none.py b/examples/v3arch/asyncore/manager/cmdgen/usm-sha-none.py new file mode 100644 index 00000000..97d4393f --- /dev/null +++ b/examples/v3arch/asyncore/manager/cmdgen/usm-sha-none.py @@ -0,0 +1,76 @@ +# +# SET Command Generator +# +# Send a SNMP SET request +# with SNMPv3 with user 'usr-sha-none', SHA auth and no privacy protocols +# over IPv4/UDP +# to an Agent at 127.0.0.1:161 +# for an OID in tuple form and a string-typed value +# +# This script performs similar to the following Net-SNMP command: +# +# $ snmpset -v3 -l authNoPriv -u usr-sha-none -a SHA -A authkey1 -ObentU 127.0.0.1:161 1.3.6.1.2.1.1.1.0 s 'my new value' +# +# SET Command Generator +from pysnmp.entity import engine, config +from pysnmp.carrier.asynsock.dgram import udp +from pysnmp.entity.rfc3413 import cmdgen +from pysnmp.proto import rfc1902 + +# Create SNMP engine instance +snmpEngine = engine.SnmpEngine() + +# +# SNMPv3/USM setup +# + +# user: usr-sha-none, auth: SHA, priv none +config.addV3User( + snmpEngine, 'usr-sha-none', + config.usmHMACSHAAuthProtocol, 'authkey1' +) +config.addTargetParams(snmpEngine, 'my-creds', 'usr-sha-none', 'authNoPriv') + +# +# 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) + elif errorStatus: + print('%s at %s' % ( + errorStatus.prettyPrint(), + errorIndex and varBindTable[-1][int(errorIndex)-1] or '?' + ) + ) + else: + for oid, val in varBindTable: + print('%s = %s' % (oid.prettyPrint(), val.prettyPrint())) + +# Prepare and send a request message +cmdgen.SetCommandGenerator().sendReq( + snmpEngine, + 'my-router', + ( ((1,3,6,1,2,1,1,1,0), rfc1902.OctetString('my new value')), ), + cbFun +) + +# Run I/O dispatcher which would send pending queries and process responses +snmpEngine.transportDispatcher.runDispatcher() |