Snmp Protocol
$ dotnet add package EonaCat.SnmpEonaCat SNMP
SNMP operations include:
GetRequest (Get): The GetRequest PDU is used by the SNMP manager to retrieve the value of one or more managed objects (variables) from the SNMP agent. The manager specifies the OIDs (Object Identifiers) of the variables it wants to retrieve in the request. The agent responds with a GetResponse PDU containing the requested variable values.
GetNextRequest (GetNext): The GetNextRequest PDU is used by the SNMP manager to retrieve the value of the next available managed object in the MIB hierarchy. The manager specifies an OID in the request, and the agent returns the value of the next OID in lexicographical order. This operation is used for walking through the MIB tree.
GetBulkRequest (GetBulk): The GetBulkRequest PDU is used for efficient retrieval of multiple managed objects. It allows the manager to request a specified number of variables in a single request. The agent responds with a GetResponse PDU containing the requested variables. This operation reduces the number of round-trip requests compared to GetRequest.
SetRequest (Set): The SetRequest PDU is used by the SNMP manager to modify the values of managed objects in the SNMP agent. The manager specifies the OIDs and the new values for the variables it wants to modify. The agent processes the request and sends a response (SetResponse) indicating success or failure.
GetResponse (Response): The GetResponse PDU is sent by the SNMP agent in response to Get, GetNext, GetBulk, and Set requests from the manager. It contains the requested variable values or a response indicating the success or failure of a Set request.
Trap: SNMP Traps are unsolicited messages sent by the SNMP agent to the manager to notify it of specific events or conditions. Traps are used for event notification and are not initiated by a manager's request. Common trap types include cold start, link up/down, authentication failure, and custom traps defined by device manufacturers.
InformRequest (Inform): The InformRequest PDU is similar to a Trap but expects an acknowledgment from the manager. It is used when the agent wants to ensure that the manager received and processed the notification. The manager responds with an InformResponse.
The Report PDU is used by the SNMP agent to notify the manager about certain conditions that may require special handling. It is typically used in SNMPv3 for error reporting and security-related issues. These SNMP operations enable network administrators and management systems to monitor and control network devices, collect performance data, and receive notifications about important events. SNMP is widely used in network management for its simplicity and effectiveness in managing diverse networked devices.
The SnmpHelper class is a part of the EonaCat.Snmp library and provides helper methods for performing SNMP (Simple Network Management Protocol) operations. This class allows you to send SNMP requests, retrieve SNMP variables, and interact with SNMP managers and agents. The SnmpHelper class provides a set of methods for performing SNMP operations asynchronously. It includes methods for sending SNMP requests, retrieving SNMP variables, and sending SNMP traps and informs.
GetRequestAsyncpublic async Task<IList<Variable>> GetRequestAsync(string oid, Version version = Version.V2, Snmp3Properties snmp3Properties = null, int timeoutInMilliSeconds = 5000)
oid (string): The Object Identifier (OID) of the variable to retrieve.version (Version, optional): The SNMP version to use (default is Version 2).snmp3Properties (Snmp3Properties, optional): Optional SNMPv3 properties for authentication and privacy.timeoutInMilliSeconds (int, optional): The timeout for the request in milliseconds (default is 5000ms).Task<IList<Variable>>: A list of Variable objects containing the retrieved values.GetTableRequestpublic Variable[,] GetTableRequest(string oid, Version version = Version.V2, int timeoutInMilliSeconds = 5000, int maxRepetitions = 50)
oid (string): The root OID of the table to retrieve.version (Version, optional): The SNMP protocol version to use (default is SNMP Version 2).timeoutInMilliSeconds (int, optional): The timeout for the request in milliseconds (default is 5000 milliseconds).maxRepetitions (int, optional): The maximum repetitions for the request (default is 50).Variable[,]: A two-dimensional array of Variable objects representing the retrieved SNMP table, where the first dimension represents rows, and the second dimension represents columns.GetNextRequestAsyncpublic async Task<IList<Variable>> GetNextRequestAsync(string oid, Version version = Version.V2, int requestId = -1, int messageId = -1, int timeoutInMilliSeconds = 5000, Snmp3Properties snmp3Properties = null)
oid (string): The Object Identifier (OID) for which to perform the Get-Next request.version (Version, optional): The SNMP protocol version to use (default is SNMP Version 2).requestId (int, optional): The SNMP request ID to use (default is -1, allowing automatic assignment).messageId (int, optional): The SNMP message ID to use (default is -1, allowing automatic assignment).timeoutInMilliSeconds (int, optional): The timeout for the request in milliseconds (default is 5000 milliseconds).snmp3Properties (Snmp3Properties, optional): The SNMPv3 properties if using SNMPv3 (default is null).Task<IList<Variable>>: A list of Variable objects representing the SNMP variables retrieved in response to the Get-Next request.WalkRequestAsyncpublic async Task<IList<Variable>> WalkRequestAsync(string oid, Version version = Version.V2, WalkMode walkMode = WalkMode.SubTree, int maxRepetitions = 50, int timeoutInMilliSeconds = 30000, Snmp3Properties snmp3Properties = null)
oid (string): The Object Identifier (OID) representing the starting point of the walk.version (Version, optional): The SNMP protocol version to use (default is SNMP Version 2).walkMode (WalkMode, optional): The walk mode for traversing the SNMP tree (default is SubTree).maxRepetitions (int, optional): The maximum number of repetitions for bulk requests (default is 50).timeoutInMilliSeconds (int, optional): The timeout for the request in milliseconds (default is 30000 milliseconds).snmp3Properties (Snmp3Properties, optional): The SNMPv3 properties if usingSNMPv3 (default is null).
Task<IList<Variable>>: A list of Variable objects containing the retrieved SNMP variables.WalkBulkRequestAsyncpublic async Task<IList<Variable>> WalkBulkRequestAsync(string oid, Version version = Version.V2, int maxRepetitions = 50, int timeoutInMilliSeconds = 30000, Snmp3Properties snmp3Properties = null)
oid (string): The Object Identifier (OID) representing the starting point of the walk.version (Version, optional): The SNMP protocol version to use (default is SNMP Version 2).maxRepetitions (int, optional): The maximum number of repetitions for bulk requests (default is 50).timeoutInMilliSeconds (int, optional): The timeout for the request in milliseconds (default is 30000 milliseconds).snmp3Properties (Snmp3Properties, optional): The SNMPv3 properties if using SNMPv3 (default is null).Task<IList<Variable>>: A list of Variable objects containing the retrieved SNMP variables.SendTrapRequestV1Asyncpublic async Task SendTrapRequestV1Async(string ipAddress, string community, TrapV1 trapV1)
ipAddress (string): The IP address of the SNMP manager to which the trap is sent.community (string): The SNMP community string to use.trapV1 (TrapV1): The SNMPv1 trap data to send.Task: An asynchronous task that represents the sending of the SNMPv1 trap.SendTrapRequestV2Asyncpublic async Task SendTrapRequestV2Async(string ipAddress, Trap trapV2)
ipAddress (string): The IP address of the SNMP manager to which the trap is sent.trapV2 (TrapV2): The SNMPv2c trap data to send.Task: An asynchronous task that represents the sending of the SNMPv2c trap.SendTrapRequestV3Asyncpublic async Task SendTrapRequestV3Async(string ipAddress, Trap trapV3)
ipAddress (string): The IP address of the SNMP manager to which the trap is sent.trapV3 (TrapV3): The SNMPv3 trap data to send.Task: An asynchronous task that represents the sending of the SNMPv3 trap.SendInformRequestAsyncpublic async Task SendInformRequestAsync(string ipAddress, string community, TrapV2 informV2)
ipAddress (string): The IP address of the SNMP manager to which the Inform request is sent.community (string): The SNMP community string to use.informV2 (TrapV2): The SNMPv2c Inform data to send.Task: An asynchronous task that represents the sending of the SNMP Inform request.GetBulkRequestAsyncpublic async Task<IList<Variable>> GetBulkRequestAsync(string oid, Version version = Version.V2, Snmp3Properties snmp3Properties = null, int maxRepetitions = 50, int timeoutInMilliSeconds = 5000)
oid (string): The Object Identifier (OID) for which to perform the GetBulk request.version (Version, optional): The SNMP protocol version to use (default is SNMP Version 2).snmp3Properties (Snmp3Properties, optional): The SNMPv3 properties if using SNMPv3 (default is null).maxRepetitions (int, optional): The maximum number of repetitions for bulk requests (default is 50).timeoutInMilliSeconds (int, optional): The timeout for the request in milliseconds (default is 5000 milliseconds).Task<IList<Variable>>: A list of Variable objects containing the retrieved SNMP variables.SetRequestAsyncpublic async Task SetRequestAsync(string oid, Version version = Version.V2, Snmp3Properties snmp3Properties = null, IList<Variable> variables = null, int timeoutInMilliSeconds = 5000)
oid (string): The Object Identifier (OID) of the variable to set.version (Version, optional): The SNMP protocol version to use (default is SNMP Version 2).snmp3Properties (Snmp3Properties, optional): The SNMPv3 properties if using SNMPv3 (default is null).variables (IList<Variable>, optional): A list of Variable objects representing the variables to set (default is null).timeoutInMilliSeconds (int, optional): The timeout for the request in milliseconds (default is 5000 milliseconds).Snmp3Properties parameter in methods that support SNMPv3.Here is an example of how to use the SnmpHelper class to perform an SNMP Get Request asynchronously:
using EonaCat.Snmp;
using EonaCat.Snmp.Exceptions;
using EonaCat.Snmp.Extensions.Message;
using EonaCat.Snmp.Models;
using EonaCat.Snmp.Models.Messages;
using EonaCat.Snmp.Models.Types;
using EonaCat.Snmp.Security.PrivacyProvider;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
// Create an instance of SnmpHelper
var snmpHelper = new SnmpHelper();
try
{
// Perform an SNMP Get Request to retrieve the value of a specific OID
var oid = "1.3.6.1.2.1.1.1.0"; // SysDescr
IList<Variable> result = await snmpHelper.GetRequestAsync(oid);
// Print the retrieved variables
foreach (var variable in result)
{
Console.WriteLine($"{variable.Id} ({variable.Type}): {variable.Data}");
}
}
catch (SnmpTimeoutException ex)
{
Console.WriteLine($"SNMP request timed out: {ex.Message}");
}
catch (SnmpException ex)
{
Console.WriteLine($"SNMP error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
In this example, we perform an SNMP Get Request to retrieve the value of the SysDescr OID (1.3.6.1.2.1.1.1.0). The retrieved variables are then printed to the console. Be sure to handle exceptions and errors according to your application's requirements.