forked from BornToBeRoot/NETworkManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSNMP.cs
More file actions
341 lines (287 loc) · 12.2 KB
/
SNMP.cs
File metadata and controls
341 lines (287 loc) · 12.2 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
using Lextm.SharpSnmpLib;
using Lextm.SharpSnmpLib.Messaging;
using Lextm.SharpSnmpLib.Security;
using NETworkManager.Utilities;
using System;
using System.Collections.Generic;
using System.Net;
using System.Security;
using System.Threading.Tasks;
namespace NETworkManager.Models.Network
{
public class SNMP
{
#region Variables
public int Port = 161;
public int Timeout = 10000;
#endregion
#region Events
public event EventHandler<SNMPReceivedArgs> Received;
protected virtual void OnReceived(SNMPReceivedArgs e)
{
Received?.Invoke(this, e);
}
public event EventHandler TimeoutReached;
protected virtual void OnTimeoutReached()
{
TimeoutReached?.Invoke(this, EventArgs.Empty);
}
public event EventHandler Error;
protected virtual void OnError()
{
Error?.Invoke(this, EventArgs.Empty);
}
public event EventHandler Complete;
protected virtual void OnComplete()
{
Complete?.Invoke(this, EventArgs.Empty);
}
public event EventHandler UserHasCanceled;
protected virtual void OnUserHasCanceled()
{
UserHasCanceled?.Invoke(this, EventArgs.Empty);
}
#endregion
#region Methods
public void GetV1V2CAsync(SNMPVersion version, IPAddress ipAddress, SecureString community, string oid)
{
Task.Run(() =>
{
try
{
foreach (var result in Messenger.Get(version == SNMPVersion.V1 ? VersionCode.V1 : VersionCode.V2, new IPEndPoint(ipAddress, Port), new OctetString(SecureStringHelper.ConvertToString(community)), new List<Variable> { new Variable(new ObjectIdentifier(oid)) }, Timeout))
OnReceived(new SNMPReceivedArgs(result.Id, result.Data));
OnComplete();
}
catch (Lextm.SharpSnmpLib.Messaging.TimeoutException)
{
OnTimeoutReached();
}
catch (ErrorException)
{
OnError();
}
});
}
public void WalkV1V2CAsync(SNMPVersion version, IPAddress ipAddress, SecureString community, string oid, WalkMode walkMode)
{
Task.Run(() =>
{
try
{
IList<Variable> results = new List<Variable>();
Messenger.Walk(version == SNMPVersion.V1 ? VersionCode.V1 : VersionCode.V2, new IPEndPoint(ipAddress, Port), new OctetString(SecureStringHelper.ConvertToString(community)), new ObjectIdentifier(oid), results, Timeout, walkMode);
foreach (var result in results)
OnReceived(new SNMPReceivedArgs(result.Id, result.Data));
OnComplete();
}
catch (Lextm.SharpSnmpLib.Messaging.TimeoutException)
{
OnTimeoutReached();
}
catch (ErrorException)
{
OnError();
}
});
}
public void SetV1V2CAsync(SNMPVersion version, IPAddress ipAddress, SecureString communtiy, string oid, string data)
{
Task.Run(() =>
{
try
{
Messenger.Set(version == SNMPVersion.V1 ? VersionCode.V1 : VersionCode.V2, new IPEndPoint(ipAddress, Port), new OctetString(SecureStringHelper.ConvertToString(communtiy)), new List<Variable> { new Variable(new ObjectIdentifier(oid), new OctetString(data)) }, Timeout);
OnComplete();
}
catch (Lextm.SharpSnmpLib.Messaging.TimeoutException)
{
OnTimeoutReached();
}
catch (ErrorException)
{
OnError();
}
});
}
public void Getv3Async(IPAddress ipAddress, string oid, SNMPV3Security security, string username, SNMPV3AuthenticationProvider authProvider, SecureString auth, SNMPV3PrivacyProvider privProvider, SecureString priv)
{
Task.Run(() =>
{
try
{
var ipEndpoint = new IPEndPoint(ipAddress, Port);
// Discovery
var discovery = Messenger.GetNextDiscovery(SnmpType.GetRequestPdu);
var report = discovery.GetResponse(Timeout, ipEndpoint);
IPrivacyProvider privacy;
switch (security)
{
case SNMPV3Security.AuthPriv:
privacy = GetPrivacy(authProvider, SecureStringHelper.ConvertToString(auth), privProvider, SecureStringHelper.ConvertToString(priv));
break;
// noAuthNoPriv
case SNMPV3Security.AuthNoPriv:
privacy = GetPrivacy(authProvider, SecureStringHelper.ConvertToString(auth));
break;
default:
privacy = GetPrivacy();
break;
}
var request = new GetRequestMessage(VersionCode.V3, Messenger.NextMessageId, Messenger.NextRequestId, new OctetString(username), new List<Variable> { new Variable(new ObjectIdentifier(oid)) }, privacy, Messenger.MaxMessageSize, report);
var reply = request.GetResponse(Timeout, ipEndpoint);
var result = reply.Pdu().Variables[0];
OnReceived(new SNMPReceivedArgs(result.Id, result.Data));
OnComplete();
}
catch (Lextm.SharpSnmpLib.Messaging.TimeoutException)
{
OnTimeoutReached();
}
catch (ErrorException)
{
OnError();
}
});
}
public void WalkV3Async(IPAddress ipAddress, string oid, SNMPV3Security security, string username, SNMPV3AuthenticationProvider authProvider, SecureString auth, SNMPV3PrivacyProvider privProvider, SecureString priv, WalkMode walkMode)
{
Task.Run(() =>
{
try
{
var ipEndpoint = new IPEndPoint(ipAddress, Port);
// Discovery
var discovery = Messenger.GetNextDiscovery(SnmpType.GetRequestPdu);
var report = discovery.GetResponse(Timeout, ipEndpoint);
IPrivacyProvider privacy;
switch (security)
{
case SNMPV3Security.AuthPriv:
privacy = GetPrivacy(authProvider, SecureStringHelper.ConvertToString(auth), privProvider, SecureStringHelper.ConvertToString(priv));
break;
// noAuthNoPriv
case SNMPV3Security.AuthNoPriv:
privacy = GetPrivacy(authProvider, SecureStringHelper.ConvertToString(auth));
break;
default:
privacy = GetPrivacy();
break;
}
var results = new List<Variable>();
Messenger.BulkWalk(VersionCode.V3, ipEndpoint, new OctetString(username), OctetString.Empty, new ObjectIdentifier(oid), results, Timeout, 10, walkMode, privacy, report);
foreach (var result in results)
OnReceived(new SNMPReceivedArgs(result.Id, result.Data));
OnComplete();
}
catch (Lextm.SharpSnmpLib.Messaging.TimeoutException)
{
OnTimeoutReached();
}
catch (ErrorException)
{
OnError();
}
});
}
public void SetV3Async(IPAddress ipAddress, string oid, SNMPV3Security security, string username, SNMPV3AuthenticationProvider authProvider, SecureString auth, SNMPV3PrivacyProvider privProvider, SecureString priv, string data)
{
Task.Run(() =>
{
try
{
var ipEndpoint = new IPEndPoint(ipAddress, Port);
// Discovery
var discovery = Messenger.GetNextDiscovery(SnmpType.GetRequestPdu);
var report = discovery.GetResponse(Timeout, ipEndpoint);
IPrivacyProvider privacy;
switch (security)
{
case SNMPV3Security.AuthPriv:
privacy = GetPrivacy(authProvider, SecureStringHelper.ConvertToString(auth), privProvider, SecureStringHelper.ConvertToString(priv));
break;
// noAuthNoPriv
case SNMPV3Security.AuthNoPriv:
privacy = GetPrivacy(authProvider, SecureStringHelper.ConvertToString(auth));
break;
default:
privacy = GetPrivacy();
break;
}
var request = new SetRequestMessage(VersionCode.V3, Messenger.NextMessageId, Messenger.NextRequestId, new OctetString(username), OctetString.Empty, new List<Variable> { new Variable(new ObjectIdentifier(oid), new OctetString(data)) }, privacy, Messenger.MaxMessageSize, report);
var reply = request.GetResponse(Timeout, ipEndpoint);
OnComplete();
}
catch (Lextm.SharpSnmpLib.Messaging.TimeoutException)
{
OnTimeoutReached();
}
catch (ErrorException)
{
OnError();
}
});
}
// noAuthNoPriv
private static IPrivacyProvider GetPrivacy()
{
return new DefaultPrivacyProvider(DefaultAuthenticationProvider.Instance);
}
// authNoPriv
private static IPrivacyProvider GetPrivacy(SNMPV3AuthenticationProvider authProvider, string auth)
{
IAuthenticationProvider authenticationProvider;
if (authProvider == SNMPV3AuthenticationProvider.MD5)
authenticationProvider = new MD5AuthenticationProvider(new OctetString(auth));
else
authenticationProvider = new SHA1AuthenticationProvider(new OctetString(auth));
return new DefaultPrivacyProvider(authenticationProvider);
}
// authPriv
private static IPrivacyProvider GetPrivacy(SNMPV3AuthenticationProvider authProvider, string auth, SNMPV3PrivacyProvider privProvider, string priv)
{
IAuthenticationProvider authenticationProvider;
if (authProvider == SNMPV3AuthenticationProvider.MD5)
authenticationProvider = new MD5AuthenticationProvider(new OctetString(auth));
else
authenticationProvider = new SHA1AuthenticationProvider(new OctetString(auth));
if (privProvider == SNMPV3PrivacyProvider.DES)
return new DESPrivacyProvider(new OctetString(priv), authenticationProvider);
return new AESPrivacyProvider(new OctetString(priv), authenticationProvider);
}
#endregion
#region Enum
public enum SNMPVersion
{
V1,
V2C,
V3
}
// Trap and Inform not implemented
public enum SNMPMode
{
Get,
Walk,
Set,
Trap,
Inform
}
public enum SNMPV3Security
{
NoAuthNoPriv,
AuthNoPriv,
AuthPriv
}
public enum SNMPV3AuthenticationProvider
{
MD5,
SHA1
}
public enum SNMPV3PrivacyProvider
{
DES,
AES
}
#endregion
}
}