コード例 #1
0
ファイル: SnmpValue.cs プロジェクト: cunfate/SnmpWithGUI
    public string[] snmpWalk(string ip, string oid_in)
    {
        StringBuilder tempIP   = new StringBuilder();
        StringBuilder tempOID  = new StringBuilder();
        SnmpOID       oidInput = new SnmpOID(oid_in);
        SnmpOID       oidTemp  = new SnmpOID(oid_in);
        string        result   = "";

        tempIP.Append(ip);
        tempOID.Append(oid_in);
        while ((oidTemp.oidSegInt[oidInput.oidLen - 1]) == (oidInput.oidSegInt[oidInput.oidLen - 1]))
        {
            // IntPtr temp;
            string tempstr;
            tempstr = Marshal.PtrToStringAnsi(snmpGetNextOid(tempIP, tempOID));
            oidTemp.Change(tempstr);

            if ((oidTemp.oidSegInt[oidInput.oidLen - 1]) != (oidInput.oidSegInt[oidInput.oidLen - 1]))
            {
                break;
            }
            tempOID.Clear();
            tempOID.Append(tempstr);

            tempstr = "2c:" + ip + ":" + tempstr + ":" + Marshal.PtrToStringAnsi(snmpGet(tempIP, tempOID));
            result += tempstr + ";";
        }
        string[] result2 = result.Split(new char[] { ';' });
        return(result2);
    }
コード例 #2
0
ファイル: snmp.cs プロジェクト: Jacketbg/firemon
            public string get(string ip, string community, string oid, int timeout, int retry)
            {
                IPAddress ipaddr = IPAddress.Parse(ip);
                SnmpAPI api = new SnmpAPI();
                api.Debug = false;
                SnmpSession session = new SnmpSession(api);

                try
                {
                    session.Open();
                }
                catch (SnmpException e)
                {
                    error = "Error opening socket: " + e;
                    return "false";
                }

                SnmpPDU pdu = new SnmpPDU();

                UDPProtocolOptions option = new UDPProtocolOptions(ipaddr, 161);
                pdu.ProtocolOptions = option;
                pdu.Community = community;
                pdu.Timeout = timeout;
                pdu.Retries = retry;
                pdu.Command = SnmpAPI.GET_REQ_MSG;

                SnmpOID send_oid = new SnmpOID(oid);
                pdu.AddNull(send_oid);

                SnmpPDU result = null;
                try
                {
                    result = session.SyncSend(pdu);
                }
                catch (SnmpException e)
                {
                    error = "Error sending SNMP request: " + e;
                    return "false";
                }

                if (result == null)
                {
                    error = "Request timed out!";
                    return "false";
                }
                else
                {
                    if (result.Errstat == 0)
                    {
                        session.Close();
                        api.Close();
                        return result.GetVariable(0).ToString();
                    }
                    else
                    {
                        session.Close();
                        api.Close();
                        error = result.Error;
                        return "false";
                    }
                }
            }