-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnmp_receiver_v3.py
59 lines (49 loc) · 2.08 KB
/
snmp_receiver_v3.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# python snmp v3 trap receiver
from pysnmp.carrier.asyncore.dgram import udp
from pysnmp.entity import engine, config
from pysnmp.entity.rfc3413 import ntfrcv, context
from pysnmp.proto import rfc1902
from datetime import datetime
# replace the credentials as per the environment
engine_id = '8000000004030201'
snmpEngine = engine.SnmpEngine()
snmp_v3_authProtocol = config.usmHMACMD5AuthProtocol
snmp_v3_authKey = 'snmp_v3_authKey'
snmp_v3_privProtocol = config.usmDESPrivProtocol
snmp_v3_privKey = 'snmp_v3_privKey'
snmp_v3_user = 'snmp_v3_user'
snmp_v3_securityEngineId = rfc1902.OctetString(hexValue=engine_id)
# replace the agent and port with you host machine and port
TrapAgentAddress = '127.0.0.1'
Port = 1163
print('Agent is listening SNMP3 Trap on {} , Port : {}'.format(
TrapAgentAddress, Port))
print(
'--------------------------------------------------------------------------'
)
config.addTransport(
snmpEngine, udp.domainName,
udp.UdpTransport().openServerMode((TrapAgentAddress, Port)))
config.addV3User(snmpEngine, snmp_v3_user, snmp_v3_authProtocol,
snmp_v3_authKey, snmp_v3_privProtocol, snmp_v3_privKey,
snmp_v3_securityEngineId)
def cbFun(snmpEngine, stateReference, contextEngineId, contextName, varBinds,
cbCtx):
print('\n{0}New trap message received on {1} {0}'.format(
'-' * 20,
datetime.now().strftime('%d-%b-%Y at %H:%M:%S')))
print('snmpEngine : {0}'.format(snmpEngine))
print('stateReference : {0}'.format(stateReference))
print('contextEngineId : {0}'.format(contextEngineId))
print('contextName : {0}'.format(contextName))
print('cbCtx : {0}'.format(cbCtx))
for name, val in varBinds:
print('{0} = {1}'.format(name.prettyPrint(), val.prettyPrint()))
print('{0}Trap message ends{0}\n'.format('-' * 20))
ntfrcv.NotificationReceiver(snmpEngine, cbFun)
snmpEngine.transportDispatcher.jobStarted(1)
try:
snmpEngine.transportDispatcher.runDispatcher()
except:
snmpEngine.transportDispatcher.closeDispatcher()
raise