Use SnmpSource component to make a simple snmp v3 query.
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#]
//discover snmp engine
try
{
// Add snmp variable
SnmpPdu discover = SnmpPdu.V3Discover("127.0.0.1", 161, "iniuser", 1000);
}
catch (Exception ex)
{
System.Console.Error.WriteLine("Discover PDU: " + ex.Message);
System.Environment.Exit(1);
}
//create snmp session
SnmpSession s1 = new SnmpSession();
//create snmp v3 pdu
SnmpPdu pdu = new SnmpPdu();
//Specify the Pdu type
pdu.PduType = EnumPduType.GetPdu;
//Set the snmp v3 setting
pdu.Version = SnmpVersion.Version3;
version3.V3UserName = "Admin";
version3.V3AuthenticationEncryptionFlags = SnmpV3AuthenticationEncryptionFlags.NoAuthNoEncr;
version3.V3AuthoritativeEngineBoots = discovery.V3AuthoritativeEngineBoots;
version3.V3AuthoritativeEngineID = discovery.V3AuthoritativeEngineID;
version3.V3AuthoritativeEngineTime = discovery.V3AuthoritativeEngineTime;
version3.V3ContextEngineID = discovery.V3AuthoritativeEngineID;
version3.V3ContextName = "context";
version3.V3MessageID = new Random().Next(0, 0x7fffffff);
//creat response pdu
SnmpPdu response = null;
// 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);
// Synchronized request snmp SyncRequest(hostip, port, pdu)
response = s1.SyncRequest("127.0.0.1", 161, pdu);
}
catch (Exception ex)
{
System.Console.Error.WriteLine("Sending PDU: " + ex.Message);
System.Environment.Exit(1);
}
// Display the result
try
{
if(response.ErrorStatus == 0)
{
System.Console.WriteLine("Response oid: " + response.InnerVariables[0].OID);
System.Console.WriteLine("Response value: " + response.InnerVariables[0].Data.ToString());
System.Console.WriteLine("");
}
else
{
System.Console.WriteLine("Response error code: " + response.ErrorStatus);
}
}
catch (Exception ex)
{
System.Console.WriteLine("Error in output: " + ex.Message);
}
Please refer to the SnmpV3Get example with the download.
Copyright