-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
40 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,63 @@ | ||
using Lextm.SharpSnmpLib; | ||
using Lextm.SharpSnmpLib.Messaging; | ||
using Lextm.SharpSnmpLib.Security; | ||
|
||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Protest.Protocols.Snmp; | ||
|
||
internal static class Polling { | ||
public static async Task PollAsync(IPAddress target, string communityString, string oidString) { | ||
VersionCode version = VersionCode.V3; | ||
|
||
public static byte[] Poll(IPAddress target, string community, string oid, int timeout) { | ||
byte[] bytes = null; | ||
|
||
Task.Run(async () => { | ||
try { | ||
IList<Variable> result = await PollAsync(target, community, oid, timeout); | ||
bytes = Encoding.UTF8.GetBytes(result[0].Data.ToString()); | ||
} | ||
catch (Exception ex) { | ||
bytes = Encoding.UTF8.GetBytes(ex.Message); | ||
} | ||
}).GetAwaiter().GetResult(); | ||
|
||
return bytes; | ||
} | ||
|
||
private static async Task<IList<Variable>> PollAsync(IPAddress target, string communityString, string oidString, int timeout) { | ||
VersionCode version = VersionCode.V2; | ||
IPEndPoint endpoint = new IPEndPoint(target, 161); | ||
OctetString community = new OctetString(communityString); | ||
|
||
ObjectIdentifier oid = new ObjectIdentifier(oidString); | ||
List<Variable> variables = new List<Variable> { new Variable(oid) }; | ||
|
||
IList<Variable> result = await Messenger.GetAsync(version, endpoint, community, variables); | ||
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(timeout); | ||
CancellationToken cancellationToken = cancellationTokenSource.Token; | ||
|
||
if (result is null) { | ||
Console.WriteLine("No data received."); | ||
return; | ||
IList<Variable> result; | ||
try { | ||
result = await Messenger.GetAsync(version, endpoint, community, variables, cancellationToken); | ||
} | ||
catch (OperationCanceledException) { | ||
throw new Exception("Operation timed out"); | ||
} | ||
catch (Exception) { | ||
throw; | ||
} | ||
|
||
if (result is null) { | ||
throw new Exception("No data received"); | ||
} | ||
if (result.Count == 0) { | ||
Console.WriteLine("No result received."); | ||
return; | ||
throw new Exception("No result received"); | ||
} | ||
|
||
Console.WriteLine("The value of OID {0} is {1}", oid, result[0].Data.ToString()); | ||
return result; | ||
} | ||
|
||
public static async void Test() { | ||
await PollAsync(IPAddress.Parse("127.0.0.1"), "public", ""); | ||
} | ||
} |