/// <summary>SNMP SET request</summary> /// <example>Set operation in SNMP version 1: /// <code> /// string snmpAgent = "10.10.10.1"; /// string snmpCommunity = "private"; /// SimpleSnmp snmp = new SimpleSnmp(snmpAgent, snmpCommunity); /// // Create a request Pdu /// List<Vb> vbList = new List<Vb>(); /// Oid setOid = new Oid("1.3.6.1.2.1.1.1.0"); // sysDescr.0 /// OctetString setValue = new OctetString("My personal toy"); /// vbList.Add(new Vb(setOid, setValue)); /// Dictionary<Oid, AsnType> result = snmp.Set(SnmpVersion.Ver1, list.ToArray()); /// if( result == null ) { /// Console.WriteLine("Request failed."); /// } else { /// Console.WriteLine("Success!"); /// } /// </code> /// /// To use SNMP version 2, change snmp.Set() method call first parameter to SnmpVersion.Ver2. /// </example> /// <param name="version">SNMP protocol version number. Acceptable values are SnmpVersion.Ver1 and SnmpVersion.Ver2</param> /// <param name="vbs">Vb array containing Oid/AsnValue pairs for the SET operation</param> /// <returns>Result of the SNMP request in a dictionary format with Oid => AsnType values</returns> public Dictionary <Oid, AsnType> Set(ESnmpVersion version, Vb[] vbs) { if (!Valid) { if (!suppressExceptions) { throw new SnmpException("SimpleSnmp class is not valid."); } return(null); } // function only works on SNMP version 1 and SNMP version 2 requests if (version != ESnmpVersion.Ver1 && version != ESnmpVersion.Ver2) { if (!suppressExceptions) { throw new SnmpInvalidVersionException("SimpleSnmp support SNMP version 1 and 2 only."); } return(null); } Pdu pdu = new Pdu(EPduType.Set); foreach (Vb vb in vbs) { pdu.VbList.Add(vb); } return(Set(version, pdu)); }
/// <summary>Reset the class. Initialize all member values to class defaults.</summary> public void Reset() { targetAddress = new IpAddress(System.Net.IPAddress.Loopback); targetPort = 161; targetVersion = ESnmpVersion.Ver3; timeOut = 2000; retry = 1; engineId = new OctetString(); engineBoots = new Integer32(); engineTime = new Integer32(); engineTimeStamp = DateTime.MinValue; privacyProtocol = EPrivacyProtocols.None; authenticationProtocol = AuthenticationDigests.None; privacySecret = new MutableByte(); authenticationSecret = new MutableByte(); contextEngineId = new OctetString(); contextName = new OctetString(); securityName = new OctetString(); // max message size is initialized to 64KB by default. It will be // to the smaller of the two values after discovery process maximumMessageSize = new Integer32(64 * 1024); reportable = true; }
/// <summary>SNMP GET-NEXT request</summary> /// <example>SNMP GET-NEXT request: /// <code> /// string snmpAgent = "10.10.10.1"; /// string snmpCommunity = "private"; /// SimpleSnmp snmp = new SimpleSnmp(snmpAgent, snmpCommunity); /// Dictionary<Oid, AsnType> result = snmp.GetNext(SnmpVersion.Ver1, new string[] { "1.3.6.1.2.1.1" }); /// if( result == null ) { /// Console.WriteLine("Request failed."); /// } else { /// foreach (KeyValuePair<Oid, AsnType> entry in result) /// { /// Console.WriteLine("{0} = {1}: {2}", entry.Key.ToString(), SnmpConstants.GetTypeName(entry.Value.Type), /// entry.Value.ToString()); /// } /// } /// </code> /// Will return: /// <code> /// 1.3.6.1.2.1.1.1.0 = OctetString: "Dual core Intel notebook" /// </code> /// </example> /// <param name="version">SNMP protocol version number. Acceptable values are SnmpVersion.Ver1 and SnmpVersion.Ver2</param> /// <param name="oidList">List of request OIDs in string dotted decimal format.</param> /// <returns>Result of the SNMP request in a dictionary format with Oid => AsnType values</returns> public Dictionary <Oid, AsnType> GetNext(ESnmpVersion version, string[] oidList) { if (!Valid) { if (!suppressExceptions) { throw new SnmpException("SimpleSnmp class is not valid."); } return(null); } // function only works on SNMP version 1 and SNMP version 2 requests if (version != ESnmpVersion.Ver1 && version != ESnmpVersion.Ver2) { if (!suppressExceptions) { throw new SnmpInvalidVersionException("SimpleSnmp support SNMP version 1 and 2 only."); } return(null); } Pdu pdu = new Pdu(EPduType.GetNext); foreach (string s in oidList) { pdu.VbList.Add(s); } return(GetNext(version, pdu)); }
/// <summary>Constructor</summary> public CTarget() { address = new IpAddress(IPAddress.Loopback); port = 161; version = ESnmpVersion.Ver2; Timeout1 = 2000; retry = 1; community = "public"; }
/// <summary>Constructor. Initialize SNMP version as supplied. </summary> /// <param name="protocolVersion"> /// Protocol version. Acceptable values are SnmpConstants.SNMPV1, /// SnmpConstants.SNMPV2 and SnmpConstants.SNMPV3 /// </param> public SnmpPacket(ESnmpVersion protocolVersion) { this.protocolVersion = new Integer32((int)protocolVersion); }
/// <summary>Constructor</summary> /// <param name="version">SNMP Protocol version</param> /// <param name="community">SNMP community name</param> /// <param name="disableReplySourceCheck">Should reply source IP address/port number be checked on reply reception</param> public AgentParameters(ESnmpVersion version, OctetString community, bool disableReplySourceCheck) : this(version, community) { this.disableReplySourceCheck = disableReplySourceCheck; }
/// <summary>Constructor</summary> /// <param name="version">SNMP Protocol version</param> /// <param name="community">SNMP community name</param> public AgentParameters(ESnmpVersion version, OctetString community) : this(version) { this.community.Set(community); }
/// <summary>Constructor</summary> /// <param name="version">SNMP protocol version. Acceptable values are SnmpConstants.SNMPV1 and /// SnmpConstants.SNMPV2</param> public AgentParameters(ESnmpVersion version) : this() { this.version.Value = (int)version; }
/// <summary>SNMP WALK operation</summary> /// <remarks> /// When using SNMP version 1, walk is performed using GET-NEXT calls. When using SNMP version 2, /// walk is performed using GET-BULK calls. /// </remarks> /// <example>Example SNMP walk operation using SNMP version 1: /// <code> /// string snmpAgent = "10.10.10.1"; /// string snmpCommunity = "private"; /// SimpleSnmp snmp = new SimpleSnmp(snmpAgent, snmpCommunity); /// Dictionary<Oid, AsnType> result = snmp.Walk(SnmpVersion.Ver1, "1.3.6.1.2.1.1"); /// if( result == null ) { /// Console.WriteLine("Request failed."); /// } else { /// foreach (KeyValuePair<Oid, AsnType> entry in result) /// { /// Console.WriteLine("{0} = {1}: {2}", entry.Key.ToString(), SnmpConstants.GetTypeName(entry.Value.Type), /// entry.Value.ToString()); /// } /// </code> /// Will return: /// <code> /// 1.3.6.1.2.1.1.1.0 = OctetString: "Dual core Intel notebook" /// 1.3.6.1.2.1.1.2.0 = ObjectId: 1.3.6.1.9.233233.1.1 /// 1.3.6.1.2.1.1.3.0 = TimeTicks: 0d 0h 0m 1s 420ms /// 1.3.6.1.2.1.1.4.0 = OctetString: "*****@*****.**" /// 1.3.6.1.2.1.1.5.0 = OctetString: "milans-nbook" /// 1.3.6.1.2.1.1.6.0 = OctetString: "Developer home" /// 1.3.6.1.2.1.1.8.0 = TimeTicks: 0d 0h 0m 0s 10ms /// </code> /// /// To use SNMP version 2, change snmp.Set() method call first parameter to SnmpVersion.Ver2. /// </example> /// <param name="version">SNMP protocol version. Acceptable values are SnmpVersion.Ver1 and /// SnmpVersion.Ver2</param> /// <param name="rootOid">OID to start WALK operation from. Only child OIDs of the rootOid will be /// retrieved and returned</param> /// <returns>Oid => AsnType value mappings on success, empty dictionary if no data was found or /// null on error</returns> public Dictionary <Oid, AsnType> Walk(ESnmpVersion version, string rootOid) { if (!Valid) { if (!suppressExceptions) { throw new SnmpException("SimpleSnmp class is not valid."); } return(null); } // function only works on SNMP version 1 and SNMP version 2 requests if (version != ESnmpVersion.Ver1 && version != ESnmpVersion.Ver2) { if (!suppressExceptions) { throw new SnmpInvalidVersionException("SimpleSnmp support SNMP version 1 and 2 only."); } return(null); } if (rootOid.Length < 2) { if (!suppressExceptions) { throw new SnmpException(SnmpException.EErrorCode.InvalidOid, "RootOid is not a valid Oid"); } return(null); } Oid root = new Oid(rootOid); if (root.Length <= 0) { return(null); // unable to parse root oid } Oid lastOid = (Oid)root.Clone(); Dictionary <Oid, AsnType> result = new Dictionary <Oid, AsnType>(); while (lastOid != null && root.IsRootOf(lastOid)) { Dictionary <Oid, AsnType> val = null; if (version == ESnmpVersion.Ver1) { val = GetNext(version, new string[] { lastOid.ToString() }); } else { val = GetBulk(new string[] { lastOid.ToString() }); } // check that we have a result if (val == null || val.Count == 0) { // error of some sort happened. abort... break; } foreach (KeyValuePair <Oid, AsnType> entry in val) { if (root.IsRootOf(entry.Key)) { if (result.ContainsKey(entry.Key)) { if (result[entry.Key].Type != entry.Value.Type) { throw new SnmpException(SnmpException.EErrorCode.OidValueTypeChanged, "OID value type changed for OID: " + entry.Key.ToString()); } else { result[entry.Key] = entry.Value; } } else { result.Add(entry.Key, entry.Value); } lastOid = (Oid)entry.Key.Clone(); } else { // it's faster to check if variable is null then checking IsRootOf lastOid = null; break; } } } return(result); }
/// <summary>SNMP SET request</summary> /// <example>Set operation in SNMP version 1: /// <code> /// string snmpAgent = "10.10.10.1"; /// string snmpCommunity = "private"; /// SimpleSnmp snmp = new SimpleSnmp(snmpAgent, snmpCommunity); /// // Create a request Pdu /// Pdu pdu = new Pdu(); /// pdu.Type = SnmpConstants.SET; // type SET /// Oid setOid = new Oid("1.3.6.1.2.1.1.1.0"); // sysDescr.0 /// OctetString setValue = new OctetString("My personal toy"); /// pdu.VbList.Add(setOid, setValue); /// Dictionary<Oid, AsnType> result = snmp.Set(SnmpVersion.Ver1, pdu); /// if( result == null ) { /// Console.WriteLine("Request failed."); /// } else { /// Console.WriteLine("Success!"); /// } /// </code> /// /// To use SNMP version 2, change snmp.Set() method call first parameter to SnmpVersion.Ver2. /// </example> /// <param name="version">SNMP protocol version number. Acceptable values are SnmpVersion.Ver1 and SnmpVersion.Ver2</param> /// <param name="pdu">Request Protocol Data Unit</param> /// <returns>Result of the SNMP request in a dictionary format with Oid => AsnType values</returns> public Dictionary <Oid, AsnType> Set(ESnmpVersion version, Pdu pdu) { if (!Valid) { if (!suppressExceptions) { throw new SnmpException("SimpleSnmp class is not valid."); } return(null); // class is not fully initialized. } // function only works on SNMP version 1 and SNMP version 2 requests if (version != ESnmpVersion.Ver1 && version != ESnmpVersion.Ver2) { if (!suppressExceptions) { throw new SnmpInvalidVersionException("SimpleSnmp support SNMP version 1 and 2 only."); } return(null); } try { target = new UdpTarget(peerIP, peerPort, timeout, retry); } catch (System.Exception ex) { target = null; if (!suppressExceptions) { throw ex; } } if (target == null) { return(null); } try { AgentParameters param = new AgentParameters(version, new OctetString(community)); SnmpPacket result = target.Request(pdu, param); if (result != null) { if (result.Pdu.ErrorStatus == 0) { Dictionary <Oid, AsnType> res = new Dictionary <Oid, AsnType>(); foreach (Vb v in result.Pdu.VbList) { if (res.ContainsKey(v.Oid)) { if (res[v.Oid].Type != v.Value.Type) { throw new SnmpException(SnmpException.EErrorCode.OidValueTypeChanged, "OID value type changed for OID: " + v.Oid.ToString()); } else { res[v.Oid] = v.Value; } } else { res.Add(v.Oid, v.Value); } } target.Close(); target = null; return(res); } if (!suppressExceptions) { throw new SnmpErrorStatusException("Agent responded with an error", result.Pdu.ErrorStatus, result.Pdu.ErrorIndex); } } } catch (System.Exception ex) { if (!suppressExceptions) { target.Close(); target = null; throw ex; } } target.Close(); target = null; return(null); }
/// <summary>SNMP GET request</summary> /// <example>SNMP GET request: /// <code> /// String snmpAgent = "10.10.10.1"; /// String snmpCommunity = "public"; /// SimpleSnmp snmp = new SimpleSnmp(snmpAgent, snmpCommunity); /// // Create a request Pdu /// Pdu pdu = new Pdu(); /// pdu.Type = SnmpConstants.GET; // type GET /// pdu.VbList.Add("1.3.6.1.2.1.1.1.0"); /// Dictionary<Oid, AsnType> result = snmp.GetNext(SnmpVersion.Ver1, pdu); /// if( result == null ) { /// Console.WriteLine("Request failed."); /// } else { /// foreach (KeyValuePair<Oid, AsnType> entry in result) /// { /// Console.WriteLine("{0} = {1}: {2}", entry.Key.ToString(), SnmpConstants.GetTypeName(entry.Value.Type), /// entry.Value.ToString()); /// } /// } /// </code> /// Will return: /// <code> /// 1.3.6.1.2.1.1.1.0 = OctetString: "Dual core Intel notebook" /// </code> /// </example> /// <param name="version">SNMP protocol version. Acceptable values are SnmpVersion.Ver1 and SnmpVersion.Ver2</param> /// <param name="pdu">Request Protocol Data Unit</param> /// <returns>Result of the SNMP request in a dictionary format with Oid => AsnType values</returns> public Dictionary <Oid, AsnType> Get(ESnmpVersion version, Pdu pdu) { if (!Valid) { if (!suppressExceptions) { throw new SnmpException("SimpleSnmp class is not valid."); } return(null); // class is not fully initialized. } // function only works on SNMP version 1 and SNMP version 2 requests if (version != ESnmpVersion.Ver1 && version != ESnmpVersion.Ver2) { if (!suppressExceptions) { throw new SnmpInvalidVersionException("SimpleSnmp support SNMP version 1 and 2 only."); } return(null); } try { target = new UdpTarget(peerIP, peerPort, timeout, retry); } catch (System.Exception ex) { target = null; if (!suppressExceptions) { throw ex; } } if (target == null) { return(null); } try { AgentParameters param = new AgentParameters(version, new OctetString(community)); SnmpPacket result = target.Request(pdu, param); if (result != null) { if (result.Pdu.ErrorStatus == 0) { Dictionary <Oid, AsnType> res = new Dictionary <Oid, AsnType>(); foreach (Vb v in result.Pdu.VbList) { if (version == ESnmpVersion.Ver2 && (v.Value.Type == SnmpConstants.SmiNoSuchInstance || v.Value.Type == SnmpConstants.SmiNoSuchObject)) { if (!res.ContainsKey(v.Oid)) { res.Add(v.Oid, new Null()); } else { res.Add(Oid.NullOid(), v.Value); } } else { if (!res.ContainsKey(v.Oid)) { res.Add(v.Oid, v.Value); } else { if (res[v.Oid].Type == v.Value.Type) { res[v.Oid] = v.Value; // update value of the existing Oid entry } else { throw new SnmpException(SnmpException.EErrorCode.OidValueTypeChanged, string.Format("Value type changed from {0} to {1}", res[v.Oid].Type, v.Value.Type)); } } } } target.Close(); target = null; return(res); } if (!suppressExceptions) { throw new SnmpErrorStatusException("Agent responded with an error", result.Pdu.ErrorStatus, result.Pdu.ErrorIndex); } } } catch (System.Exception ex) { if (!suppressExceptions) { target.Close(); target = null; throw ex; } } target.Close(); target = null; return(null); }