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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
# Various uses of the Notification Originator (TRAP/INFORM)
from pysnmp.entity.rfc3413.oneliner import ntforg
from pysnmp.proto import rfc1902
ntfOrg = ntforg.NotificationOriginator()
# Using
# SNMPv2c
# with community name 'public'
# over IPv4/UDP
# send TRAP notification
# with TRAP ID 'coldStart' specified as a MIB symbol
# include managed object information specified as a MIB symbol
errorIndication = ntfOrg.sendNotification(
ntforg.CommunityData('public'),
ntforg.UdpTransportTarget(('localhost', 162)),
'trap',
ntforg.MibVariable('SNMPv2-MIB', 'coldStart'),
(ntforg.MibVariable('SNMPv2-MIB', 'sysName', 0), 'new name')
)
if errorIndication:
print('Notification not sent: %s' % errorIndication)
# Using
# SNMPv1
# with community name 'public'
# over IPv4/UDP
# send TRAP notification
# with Generic Trap #6 (enterpriseSpecific) and Specific Trap 432
# overriding Uptime value with 12345
# overriding Agent Address with '127.0.0.1'
# overriding Enterprise OID with 1.3.6.1.4.1.20408.4.1.1.2
# include managed object information '1.3.6.1.2.1.1.1.0' = 'my system'
errorIndication = ntfOrg.sendNotification(
ntforg.CommunityData('public', mpModel=0),
ntforg.UdpTransportTarget(('localhost', 162)),
'trap',
'1.3.6.1.4.1.20408.4.1.1.2.0.432',
('1.3.6.1.2.1.1.3.0', 12345),
('1.3.6.1.6.3.18.1.3.0', '127.0.0.1'),
('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'),
('1.3.6.1.2.1.1.1.0', rfc1902.OctetString('my system'))
)
if errorIndication:
print('Notification not sent: %s' % errorIndication)
# Using
# SNMPv1
# with community name 'public'
# over IPv4/UDP
# send TRAP notification
# with Generic Trap #1 (warmStart) and Specific Trap 0
# with default Uptime
# with default Agent Address
# with Enterprise OID 1.3.6.1.4.1.20408.4.1.1.2
# include managed object information '1.3.6.1.2.1.1.1.0' = 'my system'
errorIndication = ntfOrg.sendNotification(
ntforg.CommunityData('public', mpModel=0),
ntforg.UdpTransportTarget(('localhost', 162)),
'trap',
'1.3.6.1.6.3.1.1.5.2',
('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'),
('1.3.6.1.2.1.1.1.0', rfc1902.OctetString('my system'))
)
if errorIndication:
print('Notification not sent: %s' % errorIndication)
# Using
# SNMPv2c
# with community name 'public'
# over IPv4/UDP
# send TRAP notification
# with TRAP ID 'coldStart' specified as a MIB symbol
# include managed object information specified as a MIB symbol
# perform response OIDs and values resolution at MIB
errorIndication, errorStatus, errorIndex, varBinds = ntfOrg.sendNotification(
ntforg.CommunityData('public'),
ntforg.UdpTransportTarget(('localhost', 162)),
'inform',
ntforg.MibVariable('SNMPv2-MIB', 'coldStart'),
(ntforg.MibVariable('SNMPv2-MIB', 'sysName', 0), 'my system'),
lookupNames=True, lookupValues=True
)
if errorIndication:
print('Notification not sent: %s' % errorIndication)
elif errorStatus:
print('Notification Receiver returned error: %s @%s' % (errorStatus, errorIndex))
else:
for name, val in varBinds:
print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
# Using
# SNMPv3
# with user 'usr-md5-des', auth: MD5, priv 3DES
# over IPv4/UDP
# send INFORM notification
# with TRAP ID 'warmStart' specified as a string OID
# include managed object information 1.3.6.1.2.1.1.5.0 = 'system name'
errorIndication, errorStatus, errorIndex, varBinds = ntfOrg.sendNotification(
ntforg.UsmUserData('usr-md5-des', 'authkey1', 'privkey1'),
ntforg.UdpTransportTarget(('localhost', 162)),
'inform',
'1.3.6.1.6.3.1.1.5.2',
('1.3.6.1.2.1.1.5.0', rfc1902.OctetString('system name'))
)
if errorIndication:
print('Notification not sent: %s' % errorIndication)
elif errorStatus:
print('Notification Receiver returned error: %s @%s' % (errorStatus, errorIndex))
else:
for name, val in varBinds:
print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
#
# Using
# SNMPv3
# with user 'usr-md5-none', MD5 auth, no priv
# send INFORM notification
# in behalf of contextEngineId = SnmpEngineId, contextName 'my-context'
# over IPv4/UDP
# with TRAP ID 'warmStart' specified as a string OID
#
# Sending SNMPv3 Notification in behalf of non-default ContextName
# requires having a collection of Managed Objects registered under
# the ContextName being used, so we re-create SnmpEngine and
# NotificationOriginator alone.
#
from pysnmp.entity import engine
from pysnmp.entity.rfc3413 import context
from pysnmp.entity.rfc3413.oneliner import ntforg
snmpEngine = engine.SnmpEngine()
snmpContext = context.SnmpContext(snmpEngine)
# register default collection of Managed Objects under new contextName
snmpContext.registerContextName('my-context', snmpContext.getMibInstrum())
ntfOrg = ntforg.NotificationOriginator(snmpEngine, snmpContext)
errorIndication, errorStatus, errorIndex, varBinds = ntfOrg.sendNotification(
ntforg.UsmUserData('usr-md5-none', 'authkey1'),
ntforg.UdpTransportTarget(('localhost', 162)),
'inform',
'1.3.6.1.6.3.1.1.5.2',
contextName='my-context'
)
if errorIndication:
print('Notification not sent: %s' % errorIndication)
elif errorStatus:
print('Notification Receiver returned error: %s @%s' % (errorStatus, errorIndex))
else:
for name, val in varBinds:
print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
#
# Using
# SNMPv3
# with user 'usr-md5-none', MD5 auth, no priv
# send INFORM notification
# in behalf of contextEngineId 0x8000000004030201, contextName ''
# over IPv4/UDP
# with TRAP ID 'warmStart' specified as a string OID
#
# Sending SNMPv3 Notification in behalf of non-default ContextEngineId
# requires having a collection of Managed Objects registered under
# the ContextEngineId being used, so we re-create SnmpEngine and
# NotificationOriginator alone.
#
from pysnmp.entity import engine
from pysnmp.entity.rfc3413 import context
from pysnmp.entity.rfc3413.oneliner import ntforg
from pysnmp.proto import rfc1902
snmpEngine = engine.SnmpEngine()
snmpContext = context.SnmpContext(
snmpEngine,contextEngineId=rfc1902.OctetString(hexValue='8000000004030201')
)
ntfOrg = ntforg.NotificationOriginator(snmpEngine, snmpContext)
errorIndication, errorStatus, errorIndex, varBinds = ntfOrg.sendNotification(
ntforg.UsmUserData('usr-md5-none', 'authkey1'),
ntforg.UdpTransportTarget(('localhost', 162)),
'inform',
'1.3.6.1.6.3.1.1.5.2'
)
if errorIndication:
print('Notification not sent: %s' % errorIndication)
elif errorStatus:
print('Notification Receiver returned error: %s @%s' % (errorStatus, errorIndex))
else:
for name, val in varBinds:
print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
# Using
# SNMPv3
# with user 'usr-none-none', no auth, no priv
# over IPv4/UDP
# send INFORM notification
# with TRAP ID 'warmStart' specified as a string OID
errorIndication, errorStatus, errorIndex, varBinds = ntfOrg.sendNotification(
ntforg.UsmUserData('usr-none-none'),
ntforg.UdpTransportTarget(('localhost', 162)),
'inform',
'1.3.6.1.6.3.1.1.5.2',
contextName=rfc1902.OctetString('')
)
if errorIndication:
print('Notification not sent: %s' % errorIndication)
elif errorStatus:
print('Notification Receiver returned error: %s @%s' % (errorStatus, errorIndex))
else:
for name, val in varBinds:
print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
#
# Using
# SNMPv3
# with local snmpEngineId = 0x8000000001020304
# with user 'usr-sha-aes', auth: SHA, priv: AES128
# over IPv6/UDP
# send TRAP notification
# with TRAP ID 'authenticationFailure' specified as a MIB symbol
# do not include any additional managed object information
#
# SNMPv3 TRAPs requires pre-sharing the Notification Originator's
# value of SnmpEngineId with Notification Receiver. To facilitate that
# we will use static (e.g. not autogenerated) version of snmpEngineId.
# Thus we re-create SnmpEngine and NotificationOriginator alone.
#
from pysnmp.entity import engine
from pysnmp.entity.rfc3413 import context
from pysnmp.entity.rfc3413.oneliner import ntforg
from pysnmp.proto import rfc1902
# This SNMP Engine ID value should also be configured to TRAP receiver.
snmpEngineId = rfc1902.OctetString(hexValue='8000000001020304')
ntfOrg = ntforg.NotificationOriginator(engine.SnmpEngine(snmpEngineId))
errorIndication = ntfOrg.sendNotification(
ntforg.UsmUserData('usr-sha-aes', 'authkey1', 'privkey1',
authProtocol=ntforg.usmHMACSHAAuthProtocol,
privProtocol=ntforg.usmAesCfb128Protocol),
ntforg.Udp6TransportTarget(('::1', 162)),
'trap',
ntforg.MibVariable('SNMPv2-MIB', 'authenticationFailure')
)
if errorIndication:
print('Notification not sent: %s' % errorIndication)
|