Example SNMPv3 Get Operation - rqx110/SnmpSharpNet GitHub Wiki
Example SNMPv3 Get Operation | SnmpSharpNet
Below is the example of host to make a Get request from an agent that requires both authentication and privacy.
using System;
using System.Net;
using SnmpSharpNet;
namespace SharpGetV3
{
class MainClass
{
public static void Main (string[] args)
{
IpAddress ipa = new IpAddress ("192.168.1.1");
UdpTarget target = new UdpTarget((IPAddress)ipa);
SecureAgentParameters param = new SecureAgentParameters();
if (!target.Discovery(param))
{
Console.WriteLine("Discovery failed. Unable to continue...");
target.Close();
return;
}
// Construct a Protocol Data Unit (PDU)
Pdu pdu = new Pdu();
// Set the request type (default is Get)
pdu.Type = PduType.Get;
// Add variables you wish to query
pdu.VbList.Add("1.3.6.1.2.1.1.1.0");
Oid oidVal1 = new Oid (new int[] { 1, 3, 6, 1, 2, 1, 1, 2, 0 });
pdu.VbList.Add(oidVal1);
Oid oidVal2 = new Oid ("1.3.6.1.2.1.1.3.0");
pdu.VbList.Add(oidVal2);
// optional: make sure no authentication or privacy is configured in the
// SecureAgentParameters class (see discovery section above)
param.authPriv (
"priv1",
AuthenticationDigests.MD5, "test1234",
PrivacyProtocols.AES128, "1234test");
// Make a request. Request can throw a number of errors so wrap it in try/catch
SnmpV3Packet result;
try {
result = (SnmpV3Packet)target.Request(pdu, param);
} catch( Exception ex ) {
Console.WriteLine ("Error: {0}", ex.Message);
result = null;
}
if( result != null ) {
if (result.ScopedPdu.Type == PduType.Report) {
Console.WriteLine ("SNMPv3 report:");
foreach (Vb v in result.ScopedPdu.VbList) {
Console.WriteLine ("{0} -> ({1}) {2}",
v.Oid.ToString (),
SnmpConstants.GetTypeName (v.Value.Type), v.Value.ToString ());
}
} else {
if (result.ScopedPdu.ErrorStatus == 0) {
foreach (Vb v in result.ScopedPdu.VbList) {
Console.WriteLine ("{0} -> ({1}) {2}",
v.Oid.ToString (),
SnmpConstants.GetTypeName (v.Value.Type), v.Value.ToString ());
}
} else {
Console.WriteLine ("SNMPError: {0} ({1}): {2}",
SnmpError.ErrorMessage (result.ScopedPdu.ErrorStatus),
result.ScopedPdu.ErrorStatus, result.ScopedPdu.ErrorIndex);
}
}
}
target.Close();
}
}
}
Example output:
C:>SharpGetV3.exe
1.3.6.1.2.1.1.1.0 -> (OctetString) Cisco IOS Software, C3750 Software (C3750-IPSERVICESK9-M), Version 12.2(55)SE, RELEASE SOFTWARE (fc2)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2010 by Cisco Systems, Inc.
Compiled Sat 07-Aug-10 22:45 by prod_rel_team
1.3.6.1.2.1.1.2.0 -> (ObjectId) 1.3.6.1.4.1.9.1.516
1.3.6.1.2.1.1.3.0 -> (TimeTicks) 161d 0h 43m 19s 80ms