diff options
author | elie <elie> | 2005-10-09 19:08:32 +0000 |
---|---|---|
committer | elie <elie> | 2005-10-09 19:08:32 +0000 |
commit | 7412e60fb2a7df7988e910bdbe0eefd329e66137 (patch) | |
tree | ba357aa9911b2b37f59f31b9dd580050917a5db8 /examples/v1arch | |
parent | bea4a660d2f1cbbe0a91dfef3afe66923dff8528 (diff) | |
download | pysnmp-git-7412e60fb2a7df7988e910bdbe0eefd329e66137.tar.gz |
trap* example names renamed into ntf* for clarity
Diffstat (limited to 'examples/v1arch')
-rw-r--r-- | examples/v1arch/asyncore/agent/ntforg/send-trap-over-ipv4-and-ipv6.py | 35 | ||||
-rw-r--r-- | examples/v1arch/asyncore/manager/ntfrcv/listen-on-ipv4-and-ipv6-interfaces.py | 50 |
2 files changed, 85 insertions, 0 deletions
diff --git a/examples/v1arch/asyncore/agent/ntforg/send-trap-over-ipv4-and-ipv6.py b/examples/v1arch/asyncore/agent/ntforg/send-trap-over-ipv4-and-ipv6.py new file mode 100644 index 00000000..e1084f6d --- /dev/null +++ b/examples/v1arch/asyncore/agent/ntforg/send-trap-over-ipv4-and-ipv6.py @@ -0,0 +1,35 @@ +"""Notification Originator Application (TRAP)""" +from pysnmp.carrier.asynsock.dispatch import AsynsockDispatcher +from pysnmp.carrier.asynsock.dgram import udp +from pyasn1.codec.ber import encoder +from pysnmp.proto import api + +# Protocol version to use +verID = api.protoVersion1 +pMod = api.protoModules[verID] + +# Build PDU +trapPDU = pMod.TrapPDU() +pMod.apiTrapPDU.setDefaults(trapPDU) + +# Traps have quite different semantics among proto versions +if verID == api.protoVersion1: + pMod.apiTrapPDU.setEnterprise(trapPDU, (1,3,6,1,1,2,3,4,1)) + pMod.apiTrapPDU.setGenericTrap(trapPDU, 'coldStart') + +# Build message +trapMsg = pMod.Message() +pMod.apiMessage.setDefaults(trapMsg) +pMod.apiMessage.setCommunity(trapMsg, 'public') +pMod.apiMessage.setPDU(trapMsg, trapPDU) + +transportDispatcher = AsynsockDispatcher() +transportDispatcher.registerTransport( + udp.domainName, udp.UdpSocketTransport().openClientMode() + ) +transportDispatcher.sendMessage( + encoder.encode(trapMsg), udp.domainName, ('localhost', 162) + ) +transportDispatcher.stopDispatcher() # XXX run only once +transportDispatcher.runDispatcher() +transportDispatcher.closeDispatcher() diff --git a/examples/v1arch/asyncore/manager/ntfrcv/listen-on-ipv4-and-ipv6-interfaces.py b/examples/v1arch/asyncore/manager/ntfrcv/listen-on-ipv4-and-ipv6-interfaces.py new file mode 100644 index 00000000..e225521e --- /dev/null +++ b/examples/v1arch/asyncore/manager/ntfrcv/listen-on-ipv4-and-ipv6-interfaces.py @@ -0,0 +1,50 @@ +"""Notification Receiver Application (TRAP PDU)""" +from pysnmp.carrier.asynsock.dispatch import AsynsockDispatcher +from pysnmp.carrier.asynsock.dgram import udp +from pyasn1.codec.ber import decoder +from pysnmp.proto import api + +def cbFun(transportDispatcher, transportDomain, transportAddress, wholeMsg): + while wholeMsg: + msgVer = int(api.decodeMessageVersion(wholeMsg)) + pMod = api.protoModules[msgVer] + reqMsg, wholeMsg = decoder.decode( + wholeMsg, asn1Spec=pMod.Message(), + ) + print 'Notification message from %s:%s: ' % ( + transportDomain, transportAddress + ) + reqPDU = pMod.apiMessage.getPDU(reqMsg) + if reqPDU.isSameTypeWith(pMod.TrapPDU()): + if msgVer == api.protoVersion1: + print 'Enterprise: %s' % ( + pMod.apiTrapPDU.getEnterprise(reqPDU) + ) + print 'Agent Address: %s' % ( + repr(pMod.apiTrapPDU.getAgentAddr(reqPDU)) + ) + print 'Generic Trap: %s' % ( + pMod.apiTrapPDU.getGenericTrap(reqPDU) + ) + print 'Specific Trap: %s' % ( + pMod.apiTrapPDU.getSpecificTrap(reqPDU) + ) + print 'Uptime: %s' % ( + pMod.apiTrapPDU.getTimeStamp(reqPDU) + ) + print 'Var-binds:' + for varBind in pMod.apiTrapPDU.getVarBindList(reqPDU): + print pMod.apiVarBind.getOIDVal(varBind) + else: + print 'Var-binds:' + for varBind in pMod.apiPDU.getVarBindList(reqPDU): + print pMod.apiVarBind.getOIDVal(varBind) + return wholeMsg + +transportDispatcher = AsynsockDispatcher() +transportDispatcher.registerTransport( + udp.domainName, udp.UdpSocketTransport().openServerMode(('localhost', 1162)) + ) +transportDispatcher.registerRecvCbFun(cbFun) +transportDispatcher.jobStarted(1) # this job would never finish +transportDispatcher.runDispatcher() |