SimpleSnmp with VB.Net

Performing simple SNMP operations using SimpleSnmp methods is quick and easy with VB.Net. Here is a collection of quick examples that shows how to use it.

SNMP Get operation

Imports System
Imports SnmpSharpNet
 
Module Module1
 
    Sub Main()
        Dim host As String = "localhost"
        Dim community As String = "public"
        Dim requestOid() As String
        Dim result As Dictionary(Of Oid, AsnType)
        requestOid = New String() {"1.3.6.1.2.1.1.1.0", "1.3.6.1.2.1.1.2.0"}
        Dim snmp As SimpleSnmp = New SimpleSnmp(host, community)
        If Not snmp.Valid Then
            Console.WriteLine("Invalid hostname/community.")
            Exit Sub
        End If
        result = snmp.Get(SnmpVersion.Ver1, requestOid)
        If result IsNot Nothing Then
            Dim kvp As KeyValuePair(Of Oid, AsnType)
            For Each kvp In result
                Console.WriteLine("{0}: ({1}) {2}", kvp.Key.ToString(), _
                                  SnmpConstants.GetTypeName(kvp.Value.Type), _
                                  kvp.Value.ToString())
            Next
        Else
            Console.WriteLine("No results received.")
        End If
    End Sub
 
End Module
</vbnet>
 
<h1>SNMP GetNext operation</h1>
<p>Performing GetNext operations is just as easy. With this example you can "walk" MIB entries that start with Oid 1.3.6.1.2.1.1:</p>
<pre lang="vbnet">
Imports System
Imports SnmpSharpNet
 
Module Module1
 
    Sub Main()
        Dim host As String = "localhost"
        Dim community As String = "public"
        Dim requestOid() As String
        Dim result As Dictionary(Of Oid, AsnType)
        Dim rootOid As Oid = New Oid("1.3.6.1.2.1.1")
        Dim nextOid As Oid = rootOid
        Dim keepGoing As Boolean = True
        requestOid = New String() {rootOid.ToString()}
        Dim snmp As SimpleSnmp = New SimpleSnmp(host, community)
        If Not snmp.Valid Then
            Console.WriteLine("Invalid hostname/community.")
            Exit Sub
        End If
        While keepGoing
            result = snmp.GetNext(SnmpVersion.Ver1, New String() {nextOid.ToString()})
            If result IsNot Nothing Then
                Dim kvp As KeyValuePair(Of Oid, AsnType)
                For Each kvp In result
                    If rootOid.IsRootOf(kvp.Key) Then
                        Console.WriteLine("{0}: ({1}) {2}", kvp.Key.ToString(), _
                                              SnmpConstants.GetTypeName(kvp.Value.Type), _
                                              kvp.Value.ToString())
                        nextOid = kvp.Key
                    Else
                        keepGoing = False
                    End If
                Next
            Else
                Console.WriteLine("No results received.")
                keepGoing = False
            End If
        End While
    End Sub
 
End Module

SNMP GetBulk operation

GetBulk is a SNMP v2 request type that operates similarly to the GetNext request except that it returns MaxRepetitions number of GetNext operation resuts as part of a single request.

Here is an example of how to perform a GetBulk query:

Imports System
Imports SnmpSharpNet
 
Module Module1
 
    Sub Main()
        ' SNMP Agent hostname or IP address
        Dim host As String = "localhost"
        ' SNMP community name
        Dim community As String = "public"
        ' Dictionary to store values returned by GetBulk calls
        Dim result As Dictionary(Of Oid, AsnType)
        ' Root Oid for the request (ifTable.ifEntry.ifDescr in this example)
        Dim rootOid As Oid = New Oid("1.3.6.1.2.1.2.2.1.2")
        Dim nextOid As Oid = rootOid
        Dim keepGoing As Boolean = True
        ' Initialize SimpleSnmp class
        Dim snmp As SimpleSnmp = New SimpleSnmp(host, community)
        If Not snmp.Valid Then
            Console.WriteLine("Invalid hostname/community.")
            Exit Sub
        End If
        ' Set NonRepeaters and MaxRepetitions for the GetBulk request (optional)
        snmp.NonRepeaters = 0
        snmp.MaxRepetitions = 20
        While keepGoing
            ' Make a request
            result = snmp.GetBulk(New String() {nextOid.ToString()})
            ' Check SNMP agent returned valid results
            If result IsNot Nothing Then
                Dim kvp As KeyValuePair(Of Oid, AsnType)
                ' Loop through returned values
                For Each kvp In result
                    ' Check that returned Oid is part of the original root Oid
                    If rootOid.IsRootOf(kvp.Key) Then
                        Console.WriteLine("{0}: ({1}) {2}", kvp.Key.ToString(), _
                                              SnmpConstants.GetTypeName(kvp.Value.Type), _
                                              kvp.Value.ToString())
                        ' Store last valid Oid to use in additional GetBulk requests (if required)
                        nextOid = kvp.Key
                    Else
                        ' We found a value outside of the root Oid tree. Do not perform additional GetBulk ops
                        keepGoing = False
                    End If
                Next
            Else
                Console.WriteLine("No results received.")
                keepGoing = False
            End If
        End While
    End Sub
 
End Module

SNMP Walk operation

Walk is not really an SNMP operation but a convenience method that performs multiple GetNext (for SNMP v1) or GetBulk (for SNMP v2) requests and returns all Oid/Value pairs under the requested root Oid.

This example will perform the same function as the above GetNext example:

Imports System
Imports SnmpSharpNet
 
Module Module1
 
    Sub Main()
        Dim host As String = "localhost"
        Dim community As String = "public"
        Dim result As Dictionary(Of Oid, AsnType)
        Dim snmp As SimpleSnmp = New SimpleSnmp(host, community)
        If Not snmp.Valid Then
            Console.WriteLine("Invalid hostname/community.")
            Exit Sub
        End If
        result = snmp.Walk(SnmpVersion.Ver1, "1.3.6.1.2.1.1")
        If result IsNot Nothing Then
            Dim kvp As KeyValuePair(Of Oid, AsnType)
            For Each kvp In result
                Console.WriteLine("{0}: ({1}) {2}", kvp.Key.ToString(), _
                                  SnmpConstants.GetTypeName(kvp.Value.Type), _
                                  kvp.Value.ToString())
            Next
        Else
            Console.WriteLine("No results received.")
        End If
    End Sub
 
End Module

Above example will perform as many GetNext operations in the background as necessary to retrieve all values that are part of the requested MIB branch. If you change the protocol version number in the GetBulk call from SnmpVersion.Ver1 to SnmpVersion.Ver2, Walk function will use GetBulk requests instead of GetNext and be much more efficient.

SNMP Set operation

Set operation allows you to change values associated with individual Object Identifiers. To be able to perform Set operations, target Oid has to be available for writing (you’ll need to check MIB files for the specific device to check if it is) and you will need to know the read/write SNMP community name for the device.

Here is a simple example that shows how to change contact information for a device using SNMP Set method:

Imports System
Imports SnmpSharpNet
 
Module Module1
 
    Sub Main()
        ' SNMP Agent hostname or IP address
        Dim host As String = "localhost"
        ' SNMP community name - has to be read/write access for Set operation
        Dim community As String = "private"
        ' Dictionary to store values returned by GetBulk calls
        Dim result As Dictionary(Of Oid, AsnType)
        ' This is the Oid of the value we will change with this example
        Dim sysContactOid As Oid = New Oid("1.3.6.1.2.1.1.4.0")
        ' The new value for the above Oid
        Dim sysContactNewValue As OctetString = New OctetString("Support department - 555-333-4444")
        ' Initialize SimpleSnmp class
        Dim snmp As SimpleSnmp = New SimpleSnmp(host, community)
        If Not snmp.Valid Then
            Console.WriteLine("Invalid hostname/community.")
            Exit Sub
        End If
        ' Create an array of Variable Bindings (Vb class) of Oid/Values you with to set
        Dim vbCollection() As Vb
        ' Variable Binding contains an Oid and value associated with that Oid
        Dim vb As Vb = New Vb(sysContactOid, sysContactNewValue)
        ' Add one or more Vb classes to the collection
        vbCollection = New Vb() {vb}
 
        ' Make set request
        result = snmp.Set(SnmpVersion.Ver1, vbCollection)
 
        ' Check SNMP agent returned valid results
        If result IsNot Nothing Then
            Dim kvp As KeyValuePair(Of Oid, AsnType)
            ' Loop through returned values
            For Each kvp In result
                Console.WriteLine("{0}: ({1}) {2}", kvp.Key.ToString(), _
                                          SnmpConstants.GetTypeName(kvp.Value.Type), _
                                          kvp.Value.ToString())
            Next
        Else
            Console.WriteLine("Set operation failed.")
        End If
    End Sub
 
End Module