How to make an SNMP Set request

When making an SNMP Set request, you need to supply a pair of values in your Pdu, OID that you wish to change, and the value to change it to. To be able to change an OID value, first that OID needs to be read-write (you can find this out by reading the MIB files and checking the ACCESS value) and you will need to know what kind of value that OID will accept. It is important to send the right kind of value to the agent to perform a Set operation. If you send a wrong value, for example a OctetString to an OID that accepts Integer32 values, agent will return WrongType error in the SnmpPacket.Pdu.ErrorStatus variable.

In this example we are changing sysLocation.0 MIB variable value. sysLocation.0 takes a value of type OctetString plus two random OIDs set to integer and unsigned integer to demonstrate setting numeric values.

C# example

using System;
using SnmpSharpNet;
using System.Net;
 
namespace CSharpSet
{
  class Program
  {
    static void Main(string[] args)
    {
      // Prepare target
      UdpTarget target = new UdpTarget((IPAddress)new IpAddress("some-host-name"));
          // Create a SET PDU
      Pdu pdu = new Pdu(PduType.Set);
          // Set sysLocation.0 to a new string
      pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.6.0"), new OctetString("Some other value"));
          // Set a value to integer
      pdu.VbList.Add(new Oid("1.3.6.1.2.1.67.1.1.1.1.5.0"), new Integer32(500));
          // Set a value to unsigned integer
      pdu.VbList.Add(new Oid("1.3.6.1.2.1.67.1.1.1.1.6.0"), new UInteger32(101));
          // Set Agent security parameters
      AgentParameters aparam = new AgentParameters(SnmpVersion.Ver2, new OctetString("private"));
          // Response packet
      SnmpV2Packet response;
      try
      {
              // Send request and wait for response
        response = target.Request(pdu, aparam) as SnmpV2Packet;
      }
      catch (Exception ex)
      {
              // If exception happens, it will be returned here
        Console.WriteLine(String.Format("Request failed with exception: {0}", ex.Message));
        target.Close();
        return;
      }
          // Make sure we received a response
      if (response == null)
      {
        Console.WriteLine("Error in sending SNMP request.");
      }
      else
      {
              // Check if we received an SNMP error from the agent
        if (response.Pdu.ErrorStatus != 0)
        {
          Console.WriteLine(String.Format("SNMP agent returned ErrorStatus {0} on index {1}", 
          	response.Pdu.ErrorStatus, response.Pdu.ErrorIndex));
        }
        else
        {
          // Everything is ok. Agent will return the new value for the OID we changed
          Console.WriteLine(String.Format("Agent response {0}: {1}", 
          	response.Pdu[0].Oid.ToString(), response.Pdu[0].Value.ToString()));
        }
      }
    }
  }
}

Vb.Net example

Imports SnmpSharpNet
 
Module Module1
 
    Sub Main()
        ' Prepare target
        Dim target = New UdpTarget(New IpAddress("some-host"), 161, 1000, 0)
        ' Create a SET PDU
        Dim pdu = New Pdu(PduType.Set)
        ' Set sysLocation.0 to a new string
        pdu.VbList.Add(New Oid("1.3.6.1.2.1.1.6.0"), New OctetString("Test description"))
        ' Set value to a integer
        pdu.VbList.Add(New Oid("1.3.6.1.2.1.67.1.1.1.1.5.0"), New Integer32(500))
        ' Set value to a unsigned integer
        pdu.VbList.Add(New Oid("1.3.6.1.2.1.67.1.1.1.1.6.0"), New UInteger32(101))
        ' Set Agent security parameters
        Dim aparam = New AgentParameters(SnmpVersion.Ver2, New OctetString("private"))
        ' Response packet
        Dim response As SnmpV2Packet
        Try
            ' Send request and wait for response
            response = target.Request(pdu, aparam)
        Catch ex As Exception
            ' If exception happens, it will be returned here
            Console.WriteLine(String.Format("Exception: {0}", ex.Message))
            target.Close()
            Return
        End Try
        ' Make sure we received a response
        If response Is Nothing Then
            Console.WriteLine("Error in sending SNMP request.")
        Else
            ' Check if we received an SNMP error from the agent
            If response.Pdu.ErrorStatus <> 0 Then
                Console.WriteLine(String.Format("SNMP agent returned ErrorStatus {0} on index {1}", response.Pdu.ErrorStatus, response.Pdu.ErrorIndex))
            Else
                ' Everything is ok. Agent will return the new value for the OID we changed
                Console.WriteLine(String.Format("Agent response {0}: {1}", response.Pdu(0).Oid.ToString(), response.Pdu(0).Value.ToString()))
            End If
        End If
        target.Close()
    End Sub
 
End Module