Beispiel #1
0
    public SnmpSession[] snmpInquriy_new()
    {
        StringBuilder tempIP      = new StringBuilder();
        StringBuilder tempOID     = new StringBuilder();
        StringBuilder tempContent = new StringBuilder();

        tempIP.Append(inquiryIP);
        tempOID.Append(inquiryOID);
        tempContent.Append(content);
        switch (operation)
        {
        case "get":
            string        t = Marshal.PtrToStringAnsi(snmpGet(tempIP, tempOID));
            SnmpSession[] s = new SnmpSession[1];
            s[0] = new SnmpSession("2c:" + inquiryIP + ":" + inquiryOID + ":" + t);
            return(s);

        // break;
        case "set":
            string        t1 = Marshal.PtrToStringAnsi(snmpSet(tempIP, tempOID, tempContent));
            SnmpSession[] s1 = new SnmpSession[1];
            s1[0] = new SnmpSession("2c:" + inquiryIP + ":" + inquiryOID + ":" + t1);
            return(s1);

        //   break;
        case "walk":
            return(snmpWalk_i());

        default:
            return(null);
        }
    }
Beispiel #2
0
    public SnmpSession[] snmpWalk_i()
    {
        string[] tempresult;
        tempresult = snmpWalk(inquiryIP, inquiryOID);
        SnmpSession[] result = new SnmpSession[tempresult.Length - 1];
        uint          i      = 0;

        for (i = 0; i < tempresult.Length - 1; i++)
        {
            result[i] = new SnmpSession(tempresult[i]);
        }
        return(result);
    }
Beispiel #3
0
            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";
                    }
                }
            }