diff options
-rw-r--r-- | README.md | 82 | ||||
-rw-r--r-- | examples/v1arch/asyncore/agent/ntforg/send-inform-over-ipv4-and-ipv6.py | 20 |
2 files changed, 51 insertions, 51 deletions
@@ -44,9 +44,9 @@ Installation Just run: - ```bash - $ pip install pysnmp - ``` +```bash +$ pip install pysnmp +``` to download and install PySNMP along with its dependencies: @@ -57,16 +57,16 @@ to download and install PySNMP along with its dependencies: Besides the library, command-line [SNMP utilities](https://github.com/etingof/pysnmp-apps) written in pure-Python could be installed via: - ```bash - $ pip install pysnmp-apps - ``` +```bash +$ pip install pysnmp-apps +``` and used in the very similar manner as conventional Net-SNMP tools: - ```bash - $ snmpget.py -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 demo.snmplabs.com sysDescr.0 - SNMPv2-MIB::sysDescr.0 = DisplayString: SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m - ``` +```bash +$ snmpget.py -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 demo.snmplabs.com sysDescr.0 +SNMPv2-MIB::sysDescr.0 = DisplayString: SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m +``` Examples -------- @@ -74,43 +74,43 @@ Examples PySNMP is designed highly modular and implements many programming interfaces. Most high-level and easy to use API is called *hlapi* and can be used like this: - ```python - from pysnmp.hlapi import * - - iterator = getCmd( - SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)) - ) - - errorIndication, errorStatus, errorIndex, varBinds = next(iterator) - - if errorIndication: # SNMP engine errors - print errorIndication +```python +from pysnmp.hlapi import * + +iterator = getCmd( + SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)) +) + +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + +if errorIndication: # SNMP engine errors + print errorIndication +else: + if errorStatus: # SNMP agent errors + print(%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex)-1] or '?')) else: - if errorStatus: # SNMP agent errors - print(%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex)-1] or '?')) - else: - for varBind in varBinds: # SNMP response contents - print('='.join([x.prettyPrint() for x in varBind])) - ``` + for varBind in varBinds: # SNMP response contents + print('='.join([x.prettyPrint() for x in varBind])) +``` We maintain publically available SNMP Agent and TRAP sink at [demo.snmplabs.com](http://snmpsim.sourceforge.net/public-snmp-simulator.html). You are welcome to play with it while experimenting with your PySNMP scripts. - ```bash - $ python3 examples/hlapi/asyncore/sync/manager/cmdgen/usm-sha-aes128.py - SNMPv2-MIB::sysDescr.0 = SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m - $ - $ python3 examples//hlapi/asyncore/sync/agent/ntforg/v3-inform.py - SNMPv2-MIB::sysUpTime.0 = 0 - SNMPv2-MIB::snmpTrapOID.0 = SNMPv2-MIB::warmStart - SNMPv2-MIB::sysName.0 = system name - ``` +```bash +$ python3 examples/hlapi/asyncore/sync/manager/cmdgen/usm-sha-aes128.py +SNMPv2-MIB::sysDescr.0 = SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m +$ +$ python3 examples//hlapi/asyncore/sync/agent/ntforg/v3-inform.py +SNMPv2-MIB::sysUpTime.0 = 0 +SNMPv2-MIB::snmpTrapOID.0 = SNMPv2-MIB::warmStart +SNMPv2-MIB::sysName.0 = system name +``` Other than that, PySNMP is capable to automatically fetch required MIBs from HTTP, FTP sites or local directories. You could configure any MIB source available to you (including diff --git a/examples/v1arch/asyncore/agent/ntforg/send-inform-over-ipv4-and-ipv6.py b/examples/v1arch/asyncore/agent/ntforg/send-inform-over-ipv4-and-ipv6.py index 6e29c4c6..7221355d 100644 --- a/examples/v1arch/asyncore/agent/ntforg/send-inform-over-ipv4-and-ipv6.py +++ b/examples/v1arch/asyncore/agent/ntforg/send-inform-over-ipv4-and-ipv6.py @@ -20,7 +20,7 @@ The following Net-SNMP command will produce similar SNMP notification: from time import time from pysnmp.carrier.asynsock.dispatch import AsynsockDispatcher from pysnmp.carrier.asynsock.dgram import udp, udp6 -from pyasn1.codec.ber import encoder +from pyasn1.codec.ber import encoder, decoder from pysnmp.proto.api import v2c as pMod # Build PDU @@ -38,7 +38,7 @@ startedAt = time() def cbTimerFun(timeNow): if timeNow - startedAt > 3: raise Exception("Request timed out") - + def cbRecvFun(transportDispatcher, transportDomain, transportAddress, wholeMsg, reqPDU=reqPDU): while wholeMsg: @@ -56,7 +56,7 @@ def cbRecvFun(transportDispatcher, transportDomain, transportAddress, print('%s = %s' % (oid.prettyPrint(), val.prettyPrint())) transportDispatcher.jobFinished(1) return wholeMsg - + transportDispatcher = AsynsockDispatcher() transportDispatcher.registerRecvCbFun(cbRecvFun) @@ -72,13 +72,13 @@ transportDispatcher.sendMessage( transportDispatcher.jobStarted(1) # UDP/IPv6 -transportDispatcher.registerTransport( - udp6.domainName, udp6.Udp6SocketTransport().openClientMode() -) -transportDispatcher.sendMessage( - encoder.encode(trapMsg), udp6.domainName, ('::1', 162) -) -transportDispatcher.jobStarted(1) +#transportDispatcher.registerTransport( +# udp6.domainName, udp6.Udp6SocketTransport().openClientMode() +#) +#transportDispatcher.sendMessage( +# encoder.encode(trapMsg), udp6.domainName, ('::1', 162) +#) +#transportDispatcher.jobStarted(1) # Dispatcher will finish as all scheduled messages are sent transportDispatcher.runDispatcher() |