File: v2c-trap-inline-callbacks.py

package info (click to toggle)
python-pysnmp4 4.4.6%2Brepack1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,388 kB
  • sloc: python: 19,792; makefile: 166; sh: 23
file content (44 lines) | stat: -rw-r--r-- 1,255 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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'])