Use SnmpSource component to asynchronizely retrieve snmp value by oid.
To use this code sample, add a reference to the following
assemblies:
SnmpSource.dll
Then import the following namespaces at the top of your code file with
using
(for C#) or Imports
(for
VB) keywords:
SnmpSource
System.Net
[C#]
public void Asyncget()
{
//create snmp session
SnmpSession s1 = new SnmpSession();
//setup callback function
s1.Processes += new AsyncProcesses(callbackexample);
//create snmp pdu
SnmpPdu pdu = new SnmpPdu();
//Specify the Pdu type
pdu.PduType = EnumPduType.GetPdu;
//Set the community string
pdu.Community = "public";
pdu.Version = SnmpVersion.Version1;
// Try to retrieve snmp record
try
{
// Add snmp variable
SnmpVariable var = SnmpVariable.CreateSnmpVariable("1.3.6.1.2.1.1.5.0");
pdu.AddRequestVariables(var);
// Asynchronized request snmp SyncRequest(hostip, port, pdu)
s1.AsyncRequest("127.0.0.1", 161, pdu);
}
catch (Exception ex)
{
System.Console.Error.WriteLine("Sending PDU: " + ex.Message);
System.Environment.Exit(1);
}
//Waiting for the result
while(true)
{
try
{
System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * 100));
}
catch (System.Exception )
{
}
if (s1.AsyncPendingCount == 0)
{
System.Environment.Exit(0);
}
}
}
//callback function
public void callbackexample(SnmpErrorStatus ErrorCode, SnmpPdu s, EndPoint SendToRemoteAddress, EndPoint RecvFromRemoteAddress)
{
try
{
if (ErrorCode == 0)
{
System.Console.WriteLine("Response oid: " + s.InnerVariables[0].OID);
System.Console.WriteLine("Response value: " + s.InnerVariables[0].Data.ToString());
System.Console.WriteLine("");
}
}
catch(System.Exception ex)
{
System.Console.WriteLine("Error in output: " + ex.Message);
}
}
[Vb]
Public Sub Asyncget
'create snmp session
Dim s1 As New SnmpSession
AddHandler s1.Processes, AddressOf callbackexample
'create snmp pdu
Dim pdu As New SnmpPdu
'Specify the Pdu type
pdu.PduType = EnumPduType.GetPdu
'Set the community string
pdu.Community = "public"
pdu.Version = SnmpVersion.Version1
' Try to retrieve snmp record
Try
' Add snmp variable
Dim variable1 As SnmpVariable = SnmpVariable.CreateSnmpVariable("1.3.6.1.2.1.1.5.0")
pdu.AddRequestVariables(variable1)
s1.AsyncRequest("127.0.0.1", 161, pdu)
Catch exception1 As Exception
Console.Error.WriteLine(("Sending PDU: " & exception1.Message))
Environment.Exit(1)
End Try
'Waiting for the result
While (True)
Try
System.Threading.Thread.Sleep(New System.TimeSpan(CType(10000, System.Int64) * 100))
Catch ex1 As System.Exception
End Try
If (s1.AsyncPendingCount = 0) Then
System.Environment.Exit(0)
End If
End While
End Sub
'callback function
Public Sub callbackexample(ByVal ErrorCode As SnmpErrorStatus, ByVal s As SnmpPdu, ByVal SendToRemoteAddress As EndPoint, ByVal RecvFromRemoteAddress As EndPoint)
Try
If (ErrorCode = 0) Then
Dim k As Integer
Console.WriteLine("Response oid: " & s.InnerVariables(k).OID)
Console.WriteLine("Response value: " & s.InnerVariables(k).Data.ToString)
Console.WriteLine("")
Else
Dim objArray1 As Object() = New Object() {"Asnyc Request Send to: ", SendToRemoteAddress.ToString, "; With Error code: ", ErrorCode}
Console.WriteLine(String.Concat(objArray1))
End If
Catch exception1 As Exception
Console.WriteLine(("Error in output: " & exception1.Message))
End Try
End Sub
The syntax to use included command line example.
Syntax: snmpasyncget [Options] host oid [oid] ...
Options:
/version:v1|v2 or /v:v1|v2 Specify the version number. /port:portnumber or /p:portnumber Specify the port number. /timeout:ms or /t:ms Time out in milliseconds. /debug or /d Turn on the debug setting. /community:community or /c:community Set community. /? or /help Display this usage message.
Example: snmpasyncget /d /v:v2 /c:public 127.0.0.1 1.3.6.1.2.1.1.5.0
Copyright