-
Notifications
You must be signed in to change notification settings - Fork 58
Example SNMPv3 Set Operation
qingxiao_ren edited this page Sep 5, 2018
·
1 revision
This example shows how to perform a simple Set operation using SNMPv3 protocol.
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.Set;
// Add variables you wish to query
Oid setOid = new Oid ("1.3.6.1.2.1.1.4.0");
OctetString setValue = new OctetString ("My test contact");
pdu.VbList.Add(setOid, setValue);
// 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 ) {
// Request failed. Print the exception error message
Console.WriteLine ("Error: {0}", ex.Message);
result = null;
}
if( result != null ) {
// If SNMPv3 request failed because of an error with SNMPv3 processing, PduType.Report is returned.
// OIDs in the response contain the error OID and counter of how many times agent has received
// requests with this type of error
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 {
// Now that we know response is not a Report (or SNMPv3 error), check if there was a more generic SNMP error
if (result.ScopedPdu.ErrorStatus == 0) {
// If no error is found, print all Variable Binding entries
foreach (Vb v in result.ScopedPdu.VbList) {
Console.WriteLine ("{0} -> ({1}) {2}",
v.Oid.ToString (), SnmpConstants.GetTypeName (v.Value.Type), v.Value.ToString ());
}
} else {
// SNMP error was found. Print the message and index of the variable that caused it
Console.WriteLine ("SNMPError: {0} ({1}): {2}",
SnmpError.ErrorMessage (result.ScopedPdu.ErrorStatus),
result.ScopedPdu.ErrorStatus, result.ScopedPdu.ErrorIndex);
}
}
}
target.Close();
}
As long as agent is configured correctly, Set operation is performed as expected (using Mono on OSX for this example):
~> mono SharpSetV3.exe
1.3.6.1.2.1.1.4.0 -> (OctetString) My test contact
Sequence of operations can be seen with a packet capture:
**>> Discovery request**
16:33:03.925103 IP (tos 0x0, ttl 64, id 61819, offset 0, flags [none], proto UDP (17), length 92)
1.1.1.2.59908 > 1.1.1.1.snmp: [udp sum ok] { SNMPv3 { F=r } { USM B=0 T=0 U= } { ScopedPDU E= C= { GetRequest(14) R=383430149 } } }
**>> Discovery reply**
16:33:03.933985 IP (tos 0x0, ttl 255, id 17, offset 0, flags [none], proto UDP (17), length 135)
1.1.1.1.snmp > 1.1.1.2.59908: [udp sum ok] { SNMPv3 { F= } { USM B=10 T=13813852 U= } { ScopedPDU E= 0x800x000x000x090x030x000x000x1B0xD40xB20x1C0x03 C= { Report(31) R=383430149 S:snmpUsmMIB.usmMIBObjects.usmStats.usmStatsUnknownEngineIDs.0=6 } } }
**>> Set request**
16:33:03.988960 IP (tos 0x0, ttl 64, id 50470, offset 0, flags [none], proto UDP (17), length 176)
1.1.1.2.59908 > 1.1.1.1.snmp: [udp sum ok] { SNMPv3 { F=apr } { USM B=10 T=13813853 U=priv1 } { ScopedPDU [!scoped PDU]88_7d_73_ca_83_2f_83_38_89_e8_1c_8e_8e_85_f1_7f_6a_9a_a4_89_28_15_34_45_94_56_3d_d1_ca_c7_c0_1f_4c_e2_e6_f5_11_87_14_27_50_51_c4_a0_1b_b7_2a_e2_1b_86_29_d8_4c_5d_bc_d1_d9_48_2c_a6_6a_1b_c3} }
**>> Set reply**
16:33:03.993535 IP (tos 0x0, ttl 255, id 18, offset 0, flags [none], proto UDP (17), length 175)
1.1.1.1.snmp > 1.1.1.2.59908: [udp sum ok] { SNMPv3 { F=ap } { USM B=10 T=13813852 U=priv1 } { ScopedPDU [!scoped PDU]07_10_44_65_7e_37_21_dc_8c_41_61_f7_8b_f8_7f_b2_16_3f_ab_f5_7b_1b_d8_cd_ea_75_f1_c9_f2_7d_0e_0d_3e_98_0e_de_65_49_b1_c3_fc_c5_67_31_b3_ef_f7_3f_3f_87_e0_43_9c_fe_00_b1_b6_3c_3a_09_2e_55_ec} }
Result can be seen on the agent device:
**! Before Set operation**
switch#show run | i snmp-server contact
snmp-server contact My contact
**! After Set operation**
switch#show run | i snmp-server contact
snmp-server contact My test contact