From 708062acdea970883a7c7de465f719451012ad6f Mon Sep 17 00:00:00 2001 From: Ilya Etingof Date: Sat, 18 Mar 2017 13:23:38 +0100 Subject: More twisted examples (#47) * twisted inline callback example --- .../agent/ntforg/v2c-trap-inline-callbacks.py | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 examples/hlapi/twisted/agent/ntforg/v2c-trap-inline-callbacks.py (limited to 'examples') diff --git a/examples/hlapi/twisted/agent/ntforg/v2c-trap-inline-callbacks.py b/examples/hlapi/twisted/agent/ntforg/v2c-trap-inline-callbacks.py new file mode 100644 index 00000000..e326108c --- /dev/null +++ b/examples/hlapi/twisted/agent/ntforg/v2c-trap-inline-callbacks.py @@ -0,0 +1,44 @@ +""" +SNMPv2c TRAP via Twisted inline callbacks ++++++++++++++++++++++++++++++++++++++++++ + +Send SNMPv2c TRAP through unified SNMPv3 message processing framework +using the following options: + +* SNMPv2c +* with community name 'public' +* over IPv4/UDP +* send TRAP notification +* with Generic Trap #1 (warmStart) and Specific Trap 0 +* include managed object information '1.3.6.1.2.1.1.1.0' = 'my system' + +Functionally similar to: + +| $ snmptrap -v2c -c public demo.snmplabs.com 12345 1.3.6.1.6.3.1.1.5.2 1.3.6.1.2.1.1.1.0 s "Hello from Twisted" + +"""# +from twisted.internet.task import react, defer +from pysnmp.hlapi.twisted import * + + +@defer.inlineCallbacks +def sendtrap(reactor, snmpEngine, hostname): + + yield sendNotification( + snmpEngine, + CommunityData('public', mpModel=0), + UdpTransportTarget((hostname, 162)), + ContextData(), + 'trap', + NotificationType( + ObjectIdentity('1.3.6.1.6.3.1.1.5.2') + ).addVarBinds( + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'), OctetString('Hello from Twisted')) + ) + ) + +# Preserve SnmpEngine instance across [potentially] multiple calls to safe on initialization +snmpEngine = SnmpEngine() + +react(sendtrap, [snmpEngine, 'demo.snmplabs.com']) + -- cgit v1.2.1