/// <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(SnmpVersion version, string rootOid) { if (rootOid.Length < 2) { 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 == SnmpVersion.Ver1) { val = GetNext(version, new string[] { lastOid.ToString() }); } else { val = GetBulk(new string[] { lastOid.ToString() }); } // check that we have a result if (val == null) { // error of some sort happened. abort... return(null); } foreach (KeyValuePair <Oid, AsnType> entry in val) { if (root.IsRootOf(entry.Key)) { 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> /// Return printable string in the format oid: value /// </summary> /// <returns>Format Vb string</returns> public override string ToString() { return(_oid.ToString() + ": (" + SnmpConstants.GetTypeName(_value.Type) + ") " + _value.ToString()); }
/// <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(SnmpVersion 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 != SnmpVersion.Ver1 && version != SnmpVersion.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.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 == SnmpVersion.Ver1) { val = GetNext(version, new string[] { lastOid.ToString() }); } else { val = GetBulk(new string[] { lastOid.ToString() }); } // check that we have a result if (val == null) { // error of some sort happened. abort... return(null); } 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.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> /// Return printable string in the format oid: value /// </summary> /// <returns>Format Vb string</returns> public override string ToString() { return(_oid.ToString() + ": " + _value.ToString()); }