/// <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(SnmpVersion 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 != SnmpVersion.Ver1 && version != SnmpVersion.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 (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 == SnmpVersion.Ver2 && (v.Value.Type == SnmpConstants.SMI_NOSUCHINSTANCE || v.Value.Type == SnmpConstants.SMI_NOSUCHOBJECT)) { 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.OidValueTypeChanged, string.Format("Value type changed from {0} to {1}", res[v.Oid].Type, v.Value.Type)); } } } } _target.Close(); _target = null; return(res); } else { if (!_suppressExceptions) { throw new SnmpErrorStatusException("Agent responded with an error", result.Pdu.ErrorStatus, result.Pdu.ErrorIndex); } } } } catch (Exception ex) { if (!_suppressExceptions) { _target.Close(); _target = null; throw ex; } } _target.Close(); _target = null; return(null); }