SnmpSource Component

Examples - snmpget

Use SnmpSource component to make a simple snmp 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#]
	//create snmp session
	SnmpSession s1 = new SnmpSession(); 
							
	//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;
	
	//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);
	}

			

[Vb]
	'create snmp session
	Dim s1 As New SnmpSession 
							
	'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
	
	'creat response pdu
	Dim response As SnmpPdu = Nothing
	 
	' 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)
		response = s1.SyncRequest("127.0.0.1", 161, pdu)
	Catch exception1	As	Exception 
		Console.Error.WriteLine(("Sending PDU: " & exception1.Message))
		Environment.Exit(1)
	End Try
	
	' Display the result
	Try
		If (response.ErrorStatus = SnmpErrorStatus.noError) Then
			Console.WriteLine(("Response oid: " & response.InnerVariables.Item(0).OID))
			Console.WriteLine(("Response value: " & response.InnerVariables.Item(0).Data.ToString))
			Console.WriteLine("")
		Else
			Console.WriteLine(("Response error code: " & response.ErrorStatus))
		End If
	Catch exception2 As Exception
		Console.WriteLine(("Error in output: " & exception2.Message))
		Return
	End Try

			

The syntax to use the included command line example.

Syntax: snmpget [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: snmpget /d /v:v2 /c:public 127.0.0.1 1.3.6.1.2.1.1.5.0


Copyright ©2006, SnmpSource All Rights Reserved