Use SnmpSource component to do the basic SNMP WALK.
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
[C#]
//create snmp session
SnmpSession s1 = new SnmpSession();
//create snmp pdu
SnmpPdu pdu = new SnmpPdu();
//Specify the Pdu type
pdu.PduType = EnumPduType.GetNextPdu;
//Set the community string
pdu.Community = "public";
pdu.Version = SnmpVersion.Version2;
//Creat response pdu.
SnmpPdu response = null;
//Assign the walked oid.
string oid = "1.3.6.1.2.1.1";
string conoid = oid;
// loop for each PDU in the walk
while (oid.StartsWith(conoid))
{
pdu.InnerVariables.Clear();
SnmpVariable var = SnmpVariable.CreateSnmpVariable(oid);
pdu.AddRequestVariables(var);
try
{
response = s1.SyncRequest("127.0.0.1", 161, pdu);
}
catch(Exception ex)
{
System.Console.WriteLine(ex.Message);
oid = "";
break;
}
if (response.ErrorIndex!= 0)
{
System.Console.WriteLine("Response Error:" + response.ErrorStatus);
break;
}
else
{
//Display result and assign new oid
for (int k=0; k< response.InnerVariables.Count; k++)
{
oid = response.InnerVariables[k].OID;
System.Console.WriteLine("Response oid: " + response.InnerVariables[k].OID);
System.Console.WriteLine("Response value: " + response.InnerVariables[k].Data.ToString());
System.Console.WriteLine("");
}
}
response.InnerVariables.Clear();
}
[Vb]
'create snmp session
Dim
s1 As New SnmpSession
'create snmp pdu
Dim pdu As New SnmpPdu
'Specify the Pdu type
pdu.PduType = EnumPduType.GetNextPdu
'Set the community string
pdu.Community = "public"
pdu.Version = SnmpVersion.Version2
'creat response pdu
Dim response As SnmpPdu = Nothing
'Assign the walked oid.
Dim oid As String = "1.3.6.1.2.1.1"
Dim conoid As String = oid
'loop for each PDU in the walk
Do While oid.StartsWith(conoid)
pdu.InnerVariables.Clear()
Dim variable1 As SnmpVariable = SnmpVariable.CreateSnmpVariable(oid)
pdu.AddRequestVariables(variable1)
Try
response = s1.SyncRequest("127.0.0.1", 161, pdu)
Catch exception1 As Exception
Console.WriteLine(exception1.Message)
oid = ""
Exit Do
End Try
If (response.ErrorIndex <> 0) Then
Console.WriteLine(("Response Error:" & response.ErrorStatus))
Exit Do
End If
'Display result and assign new oid
Dim i As Integer
For i = 0 To response.InnerVariables.Count - 1
oid = response.InnerVariables.Item(i).OID
Console.WriteLine(("Response oid: " & response.InnerVariables.Item(i).OID))
Console.WriteLine(("Response value: " & response.InnerVariables.Item(i).Data.ToString))
Console.WriteLine("")
Next i
response.InnerVariables.Clear()
Loop
The syntax to use the included command line example.
Syntax: snmpwalk [Options] host 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: snmpwalk /d /c:public /p:161 127.0.0.1 1.3.6.1.2.1.1
Copyright