Example #1
0
        public string getSnmp(string oid, string host, int port)
        {
            string hostIP;
            int    Port;
            string Oid;
            string get = "";

            Oid    = oid;
            hostIP = host;
            Port   = port;

            //共同体类型
            OctetString     community = new OctetString("public");
            AgentParameters param     = new AgentParameters(community);

            //设置snmp版本
            param.Version = (int)SnmpVersion.Ver1;
            IpAddress agent = new IpAddress(hostIP);

            //构建目标
            UdpTarget target = new UdpTarget((IPAddress)agent, port, 2000, 1);



            Pdu pdu = new Pdu(PduType.Get);

            pdu.VbList.Add(Oid);

            //发送snmp请求
            SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);

            get = result.Pdu.VbList[0].Value.ToString();

            return(get);
        }
        }//将对应id的条目的值设置为data

        private void GetPrinterResponse(object input_id)
        {
            string id     = input_id.ToString();
            string ipaddr = null;

            for (int i = 0; i < Common.dt_printer.Rows.Count; i++)
            {
                string tmp = Common.dt_printer.Rows[i]["Id"].ToString();
                if (tmp == (string)id)
                {
                    ipaddr = Common.dt_printer.Rows[i]["dev_ip"].ToString();
                }
            }
            int             feq       = Common.getfequency((string)id);
            OctetString     community = new OctetString("public");
            AgentParameters param     = new AgentParameters(community);

            param.Version = SnmpVersion.Ver1;
            IpAddress agent  = new IpAddress(ipaddr);
            UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
            Pdu       pdu    = new Pdu(PduType.Get);

            pdu.VbList.Add("1.3.6.1.2.1.43.11.1.1.9.1.1");
            for (; ;)
            {
                if (canceltokensource.IsCancellationRequested == true)
                {
                    Common.writetologfrm(string.Format("打印机 设备ID:{1} 线程{0}终止", Thread.CurrentThread.ManagedThreadId, id.ToString()));
                    target.Dispose();
                    this.Close();
                    break;//若收到cancellationToken消息则取消此线程
                }
                try
                {
                    SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);
                    if (result != null)
                    {
                        if (result.Pdu.ErrorStatus != 0)
                        {
                            Common.writetologfrm(string.Format("打印机 ID:{0} 获取信息异常码{1}在{2}上", (string)id, result.Pdu.ErrorStatus, result.Pdu.ErrorIndex));
                        }
                        else
                        {
                            int i = Convert.ToInt32(result.Pdu.VbList[0].Value.ToString());

                            this.Invoke(new MethodInvoker(() => { show_data(id, i); }));
                        }
                    }
                    else
                    {
                        Common.writetologfrm(string.Format("打印机 ID:{0} 未收到回复", (string)id));
                    }
                }
                catch {
                    Common.writetologfrm(string.Format("打印机 ID:{0} 获取信息异常", (string)id));
                }
                Thread.Sleep(Common.getfequency((string)id));
            }
            //target.Dispose();
        }//Printer线程工作内容
Example #3
0
        public void setSnmp(string oid, string host, int port, string value)
        {
            string hostIP;
            int    Port;
            string Oid;
            string Value;

            Oid    = oid;
            hostIP = host;
            Port   = port;
            Value  = value;

            //共同体类型
            OctetString     community = new OctetString("private");
            AgentParameters param     = new AgentParameters(community);

            //设置snmp版本
            param.Version = (int)SnmpVersion.Ver1;
            IpAddress agent = new IpAddress(hostIP);

            //构建目标
            UdpTarget target = new UdpTarget((IPAddress)agent, port, 2000, 1);



            Pdu pdu = new Pdu(PduType.Set);

            pdu.VbList.Add(new SnmpSharpNet.Oid(Oid), new OctetString(Value));

            //发送snmp请求
            SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);
        }
Example #4
0
        private string GetCounters(string ip, string query)
        {
            try
            {
                OctetString     community = new OctetString("public");
                AgentParameters param     = new AgentParameters(community);
                param.Version = SnmpVersion.Ver1;
                IpAddress agent  = new IpAddress(ip);
                UdpTarget target = new UdpTarget((IPAddress)agent, 161, 3000, 1);

                Pdu pdu = new Pdu(PduType.Get);
                pdu.VbList.Add(query);
                SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);

                if (result != null)
                {
                    return(result.Pdu.VbList[0].Value.ToString());
                }

                else
                {
                    return("-");
                }
            }
            catch
            {
                return("-");
            }
        }
Example #5
0
        /**
         *  Preparing informations for labeling.
         */
        private string getBilling(AgentParameters param, UdpTarget target, string OID_Billing)
        {
            // Pdu class used for all requests
            Pdu pdu = new Pdu(PduType.Get);

            pdu.VbList.Add(OID_Billing);

            // Make SNMP request
            SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);

            long value = 0;

            try
            {
                value = long.Parse(result.Pdu.VbList[0].Value.ToString());
            }
            catch (OverflowException e)
            {
                Console.WriteLine(e.Message);
            }

            printer.setBilling(value.ToString("#,#", CultureInfo.InvariantCulture));

            return(value.ToString("#,#", CultureInfo.InvariantCulture));
        }
Example #6
0
        private void Snmp_Get(string ip, string oid, string comm)
        {
            OctetString     community = new OctetString(comm);
            AgentParameters param     = new AgentParameters(community);

            param.Version = SnmpVersion.Ver1;
            IpAddress agent  = new IpAddress(ip);
            UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
            Pdu       pdu    = new Pdu(PduType.Get);

            pdu.VbList.Add(oid);
            SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);

            if (result != null)
            {
                if (result.Pdu.ErrorStatus != 0)
                {
                    this.txtTest.Text += string.Format("Error in SNMP reply. Error {0} index {1} \r\n", result.Pdu.ErrorStatus, result.Pdu.ErrorIndex);
                }
                else
                {
                    int index = this.dataGridView1.Rows.Add();
                    this.dataGridView1.Rows[index].Cells[0].Value = "hello";
                    this.dataGridView1.Rows[index].Cells[1].Value = result.Pdu.VbList[0].Oid.ToString();
                    this.dataGridView1.Rows[index].Cells[2].Value = SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type);
                    this.dataGridView1.Rows[index].Cells[3].Value = result.Pdu.VbList[0].Value.ToString();
                }
            }
        }
Example #7
0
    /// <summary>
    /// Prints the packets
    /// </summary>
    /// <param name="result"></param>
    public void PrintPacketRecv(SnmpV1Packet result)
    {
        // ErrorStatus other then 0 is an error returned by
        // the Agent - see SnmpConstants for error definitions

        if (result.Pdu.Type == PduType.Set)
        {
            PacketPrinterRcv.text = "Set ";
        }
        if (result.Pdu.Type == PduType.Get)
        {
            PacketPrinterRcv.text = "Get ";
        }
        if (result.Pdu.ErrorStatus != 0)
        {
            // agent reported an error with the request
            PacketPrinterRcv.text = "Error: status " + result.Pdu.ErrorStatus + " Index " + result.Pdu.ErrorIndex;
        }
        else
        {
            // Reply variables are returned in the same order as they were added
            //  to the VbList
            foreach (Vb VarBind in result.Pdu.VbList)
            {
                PacketPrinterRcv.text = "Request OID: " + VarBind.Oid.ToString() + " Type " + SnmpConstants.GetTypeName(VarBind.Value.Type) + " Value " + VarBind.Value.ToString();
            }
        }
    }
        public void ParseSnmResponse2()
        {
            var packetVersion = SnmpPacket.GetProtocolVersion(Packet2, Packet2.Length);

            Assert.Equal(ESnmpVersion.Ver1, packetVersion);

            var packet = new SnmpV1Packet();

            packet.Decode(Packet2, Packet2.Length);

            Assert.Equal("public", packet.Community.ToString());

            Assert.False(packet.IsRequest);
            Assert.True(packet.IsResponse);

            Assert.Equal(38, packet.Pdu.RequestId);
            Assert.Equal(EPduErrorStatus.NoError, packet.Pdu.ErrorStatus);
            Assert.Equal(0, packet.Pdu.ErrorIndex);

            Assert.Equal(1, packet.Pdu.VbCount);

            var vb = packet.Pdu.GetVb(0);

            Assert.NotNull(vb);

            Assert.Equal(new Oid("1.3.6.1.2.1.1.2.0"), vb.Oid);
            Assert.Equal((byte)(EAsnType.Constructor | EAsnType.Sequence), vb.Type);
            Assert.Equal((byte)EAsnType.ObjectId, vb.Value.Type);
            Assert.Equal(new Oid("1.3.6.1.4.1.2001.1.1.1.297.93.1.27.2.2.1"), vb.Value);
        }
Example #9
0
        /*
         * Fetching informations from Printer.
         */
        private void setCheckerPrinter(AgentParameters param, UdpTarget target, string s)
        {
            try
            {
                // Pdu class used for all requests
                Pdu pdu = new Pdu(PduType.Get);
                pdu.VbList.Add(s);

                // Make SNMP request
                SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);

                printer.uniquePrinterNumber = result.Pdu.VbList[0].Value.ToString();

                printer.setBilling(getBilling(param, target, OID_Billing));
                addMessagesToList(param, target);
                printer.setStatus(getStatus(param, target, OID_Status));

                //Console.WriteLine(getMessages(param, target, OID_Message5).Equals("Null"));
                //printer.setMessage(getMessages(param,target,OID_Message1));
                //Console.WriteLine(printer.getName() + " message: " + getMessages(param, target, OID_Message3));
            }
            catch (SnmpException e)
            {
                printer.setBilling("0");
                Console.WriteLine(e.Message);
            }
        }
Example #10
0
        private SNMPJsonPacket sendRequest(string oid)
        {
            SnmpV1Packet   packet     = this.GetRequest((string)oid);
            SNMPJsonPacket packetJson = new SNMPJsonPacket(packet.Pdu.VbList[0].Oid.ToString(), packet.Pdu.VbList[0].Value.ToString(), packet.Pdu.VbList[0].Type.ToString());

            return(packetJson);
        }
Example #11
0
        private void GetMore(int n)
        {
            OctetString     community = new OctetString(txtConStr.Text);
            AgentParameters param     = new AgentParameters(community);

            param.Version = SnmpVersion.Ver1;
            try
            {
                IpAddress agent  = new IpAddress(txtIPAgent.Text);
                UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
                Pdu       pdu    = new Pdu(PduType.Get);
                string[]  row;
                for (int i = 1; i <= n; i++)
                {
                    pdu.VbList.Add("1.3.6.1.2.1.2.2.1.2." + i.ToString());
                    pdu.VbList.Add("1.3.6.1.2.1.2.2.1.3." + i.ToString());
                    pdu.VbList.Add("1.3.6.1.2.1.2.2.1.5." + i.ToString());
                    pdu.VbList.Add("1.3.6.1.2.1.2.2.1.10." + i.ToString());
                    pdu.VbList.Add("1.3.6.1.2.1.2.2.1.16." + i.ToString());
                    SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);
                    row = new string[] { i.ToString(), result.Pdu.VbList[0].Value.ToString(), GetType(int.Parse(result.Pdu.VbList[1].Value.ToString())), result.Pdu.VbList[2].Value.ToString(), result.Pdu.VbList[3].Value.ToString(), result.Pdu.VbList[4].Value.ToString() };
                    dgvList.Rows.Add(row);
                    pdu.Reset();
                }
                target.Close();
            }
            catch (Exception ex)
            {
                rtbGuiGet.Text = "Đã xảy ra lỗi: " + ex.Message.ToString();
            }
        }
        public void ParseSnmpGet1()
        {
            var packetVersion = SnmpPacket.GetProtocolVersion(Packet1, Packet1.Length);

            Assert.Equal(ESnmpVersion.Ver1, packetVersion);

            var packet = new SnmpV1Packet();

            packet.Decode(Packet1, Packet1.Length);

            Assert.Equal("public", packet.Community.ToString());

            Assert.True(packet.IsRequest);
            Assert.False(packet.IsResponse);

            Assert.Equal(38, packet.Pdu.RequestId);
            Assert.Equal(EPduErrorStatus.NoError, packet.Pdu.ErrorStatus);
            Assert.Equal(0, packet.Pdu.ErrorIndex);

            Assert.Equal(1, packet.Pdu.VbCount);

            var vb = packet.Pdu.GetVb(0);

            Assert.NotNull(vb);

            Assert.Equal(new Oid("1.3.6.1.2.1.1.2.0"), vb.Oid);
        }
        public void ParseSnmpGet7()
        {
            var packetVersion = SnmpPacket.GetProtocolVersion(Packet7, Packet7.Length);

            Assert.Equal(ESnmpVersion.Ver1, packetVersion);

            var packet = new SnmpV1Packet();

            packet.Decode(Packet7, Packet7.Length);

            Assert.Equal("public", packet.Community.ToString());

            Assert.True(packet.IsRequest);
            Assert.False(packet.IsResponse);

            Assert.Equal(41, packet.Pdu.RequestId);
            Assert.Equal(EPduErrorStatus.NoError, packet.Pdu.ErrorStatus);
            Assert.Equal(0, packet.Pdu.ErrorIndex);

            Assert.Equal(3, packet.Pdu.VbCount);

            var vb = packet.Pdu.GetVb(0);

            Assert.NotNull(vb);
            Assert.Equal(new Oid("1.3.6.1.4.1.253.8.64.4.2.1.7.10.14130104"), vb.Oid);

            vb = packet.Pdu.GetVb(1);
            Assert.NotNull(vb);
            Assert.Equal(new Oid("1.3.6.1.4.1.253.8.64.4.2.1.7.10.14130102"), vb.Oid);

            vb = packet.Pdu.GetVb(2);
            Assert.NotNull(vb);
            Assert.Equal(new Oid("1.3.6.1.4.1.253.8.64.4.2.1.5.10.14130400"), vb.Oid);
        }
Example #14
0
        public static string getRequestProxy(string content)
        {
            string result = "";

            content = content.Remove(content.Length - 1);
            SnmpV1Packet packet = Instance.GetRequest(content);

            return(packet.Pdu.VbList[0].Value.ToString());
        }
Example #15
0
 public void monitorObject(object OID)
 {
     while (monitor)
     {
         SnmpV1Packet packet = this.GetRequest((string)OID);
         this.windowHandler.addMonitorRow(packet.Pdu.VbList[0].Oid.ToString(), packet.Pdu.VbList[0].Value.ToString(), packet.Pdu.VbList[0].Type.ToString(), this.ipPort);
         Thread.Sleep(5000);
     }
 }
Example #16
0
        public SnmpV1Packet GetMonitorRequest(string OID)
        {
            this.param.Version = SnmpVersion.Ver1;
            Pdu pduM = new Pdu(PduType.Get);

            pduM.VbList.Add(OID);
            SnmpV1Packet resultM = (SnmpV1Packet)target.Request(pduM, param);

            return(resultM);
        }
Example #17
0
 //多執行緒檢察linux
 private void CheckLinuxDiskThread(object obj)
 {
     //0 = TargetName, 1= TargetIP ,2=TargetAlert ,3=TargetSpaceLog ,4=id ,5=TargetWarning
     string[] Askitem = new string[6];
     Askitem = (string[])obj;
     try
     {
         // SNMP community name
         OctetString     community = new OctetString("public");
         AgentParameters param     = new AgentParameters(community);
         param.Version = SnmpVersion.Ver1;
         IpAddress agent = new IpAddress(Askitem[1]);//TargetIP
         // Construct target
         UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
         // Pdu class used for all requests
         Pdu pdu = new Pdu(PduType.Get);
         pdu.VbList.Add(".1.3.6.1.4.1.2021.9.1.2.1"); //代號
         pdu.VbList.Add(".1.3.6.1.4.1.2021.9.1.6.1"); //總空間
         pdu.VbList.Add(".1.3.6.1.4.1.2021.9.1.8.1"); //已用空間
         // Make SNMP request
         SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);
         //取得 總空間kb 已用空間kb 分割區帶號
         int    AllKB  = int.Parse(result.Pdu.VbList[1].Value.ToString());
         int    UseKB  = int.Parse(result.Pdu.VbList[2].Value.ToString());
         int    FreeKB = AllKB - UseKB;
         int    AllMB  = AllKB / 1024;
         int    FreeMB = FreeKB / 1024;
         int    AllGB  = AllMB / 1024;
         int    FreeGB = FreeMB / 1024;
         string ID     = result.Pdu.VbList[0].Value.ToString();
         //委派傳出
         string[] text = new string[10];
         text[0] = Askitem[0];        //名稱
         text[1] = ID;                //代號
         text[2] = AllMB.ToString();  //總空間mb
         text[3] = AllGB.ToString();  //總空間gb
         text[4] = FreeMB.ToString(); //剩餘mb
         text[5] = FreeGB.ToString(); //剩餘gb
         text[6] = Askitem[2];        //警戒值
         text[7] = "Linux";           //檢察linux
         text[8] = Askitem[4];        //WindowsTargetID
         text[9] = Askitem[5];        //TargetWaning
         this.Invoke(new D_AddAlreadyGridView(AddAlreadyGridView), new object[] { text });
         //如果低於警戒值
         if (FreeMB <= Convert.ToInt32(Askitem[2]))
         {
             this.Invoke(new D_AddAlertGridView(AddAlertGridView), new object[] { text });
         }
     }
     catch (Exception x)
     {
         this.Invoke(new D_AddErrorList(AddErrorList), Askitem[0] + " >>> " + x.Message.ToString());
     }
     this.Invoke(new D_AddProgress(AddProgress), null);
 }
Example #18
0
        public string GetHersteller()
        {
            // SNMP community name
            OctetString community = new OctetString("public");
            // Define agent parameters class
            AgentParameters param = new AgentParameters(community);

            // Set SNMP version to 1 (or 2)
            param.Version = SnmpVersion.Ver1;
            // Construct the agent address object
            // IpAddress class is easy to use here because
            //  it will try to resolve constructor parameter if it doesn't
            //  parse to an IP address
            IpAddress agent = new IpAddress(Ip);

            // Construct target
            UdpTarget target = new UdpTarget((IPAddress)agent, 161, 250, 1);

            // Pdu class used for all requests
            Pdu pdu = new Pdu(PduType.Get);

            pdu.VbList.Add(".1.3.6.1.2.1.25.3.2.1.3.1"); //sysDescr

            // Make SNMP request
            try
            {
                SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);


                // If result is null then agent didn't reply or we couldn't parse the reply.
                if (result != null)
                {
                    // ErrorStatus other then 0 is an error returned by
                    // the Agent - see SnmpConstants for error definitions
                    if (result.Pdu.ErrorStatus != 0)
                    {
                        // agent reported an error with the request
                        return("Error in SNMP reply.");
                    }
                    else
                    {
                        // Reply variables are returned in the same order as they were added
                        //  to the VbList
                        target.Close();
                        return(result.Pdu.VbList[0].Value.ToString());
                    }
                }
                else
                {
                    target.Close();
                    return("No response received from SNMP agent.");
                }
            }
            catch { target.Close(); return("Fehler"); }
        }
Example #19
0
        private string getMessages(AgentParameters param, UdpTarget target, string OID_Message)
        {
            // Pdu class used for all requests
            Pdu pdu = new Pdu(PduType.Get);

            pdu.VbList.Add(OID_Message);

            // Make SNMP request
            SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);

            string value = result.Pdu.VbList[0].Value.ToString();

            return(value.ToString());
        }
Example #20
0
        public SnmpV1Packet GetNextRequest(string OID)
        {
            this.param.Version = SnmpVersion.Ver1;
            this.pdu           = new Pdu(PduType.GetNext);
            this.pdu.VbList.Add(OID);
            result = (SnmpV1Packet)target.Request(pdu, param);

            OidNumber = result.Pdu.VbList[0].Oid.ToString();
            type      = SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type);
            value     = result.Pdu.VbList[0].Value.ToString();
            ipPort    = address + ":161";

            return(result);
        }
Example #21
0
        public SNMPResultSet Get(string pOID)
        {
            SNMPResultSet snmpResult = null;

            // Define agent parameters class
            AgentParameters param = new AgentParameters(new OctetString(CommunityName));

            // Set SNMP version to 1 (or 2)
            param.Version = SnmpVersion.Ver1;

            // Construct target
            UdpTarget target = new UdpTarget((IPAddress)AgentIP, 161, 2000, 1);

            // Pdu class used for all requests
            Pdu pdu = new Pdu(PduType.Get);

            pdu.VbList.Add(pOID);

            // Make SNMP request
            SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);

            // If result is null then agent didn't reply or we couldn't parse the reply.
            if (result != null)
            {
                // ErrorStatus other then 0 is an error returned by
                // the Agent - see SnmpConstants for error definitions
                if (result.Pdu.ErrorStatus != 0)
                {
                    // agent reported an error with the request
                    Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
                                      result.Pdu.ErrorStatus,
                                      result.Pdu.ErrorIndex);
                }
                else
                {
                    // Reply variables are returned in the same order as they were added
                    //  to the VbList

                    snmpResult = new SNMPResultSet(result.Pdu.VbList[0].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type), result.Pdu.VbList[0].Value.ToString());
                }
            }
            else
            {
                Console.WriteLine("No response received from SNMP agent.");
            }
            target.Close();

            return(snmpResult);
        }
Example #22
0
        private void Get(int n)
        {
            OctetString     community = new OctetString(txtConStr.Text);
            AgentParameters param     = new AgentParameters(community);

            param.Version = SnmpVersion.Ver1;
            try
            {
                IpAddress agent  = new IpAddress(txtIPAgent.Text);
                UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);

                Pdu pdu = new Pdu(PduType.Get);
                pdu.VbList.Add("1.3.6.1.2.1.1.1." + n.ToString()); //sysDescr
                pdu.VbList.Add("1.3.6.1.2.1.1.2." + n.ToString()); //sysObjectID
                pdu.VbList.Add("1.3.6.1.2.1.1.3." + n.ToString()); //sysUpTime
                pdu.VbList.Add("1.3.6.1.2.1.1.4." + n.ToString()); //sysContact
                pdu.VbList.Add("1.3.6.1.2.1.1.5." + n.ToString()); //sysName
                pdu.VbList.Add("1.3.6.1.2.1.2.1." + n.ToString()); //ifNumbber
                SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);

                if (result != null)
                {
                    if (result.Pdu.ErrorStatus != 0)
                    {
                        rtbGuiGet.Text = "Error in SNMP reply. Error " + result.Pdu.ErrorStatus + " index " + result.Pdu.ErrorIndex;
                    }
                    else
                    {
                        rtbGuiGet.Text = "sysDescr(" + result.Pdu.VbList[0].Oid.ToString() + ") (" + SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type) + "): " + result.Pdu.VbList[0].Value.ToString();
                        rtbGuiGet.Text = rtbGuiGet.Text + "\n" + "sysObjectID(" + result.Pdu.VbList[1].Oid.ToString() + ") (" + SnmpConstants.GetTypeName(result.Pdu.VbList[1].Value.Type) + "): " + result.Pdu.VbList[1].Value.ToString();
                        rtbGuiGet.Text = rtbGuiGet.Text + "\n" + "sysUpTime(" + result.Pdu.VbList[2].Oid.ToString() + ") (" + SnmpConstants.GetTypeName(result.Pdu.VbList[2].Value.Type) + "): " + result.Pdu.VbList[2].Value.ToString();
                        rtbGuiGet.Text = rtbGuiGet.Text + "\n" + "sysContact(" + result.Pdu.VbList[3].Oid.ToString() + ") (" + SnmpConstants.GetTypeName(result.Pdu.VbList[3].Value.Type) + "): " + result.Pdu.VbList[3].Value.ToString();
                        rtbGuiGet.Text = rtbGuiGet.Text + "\n" + "sysName(" + result.Pdu.VbList[4].Oid.ToString() + ") (" + SnmpConstants.GetTypeName(result.Pdu.VbList[4].Value.Type) + "): " + result.Pdu.VbList[4].Value.ToString();
                        rtbGuiGet.Text = rtbGuiGet.Text + "\n" + "ifNumber(" + result.Pdu.VbList[5].Oid.ToString() + ") (" + SnmpConstants.GetTypeName(result.Pdu.VbList[5].Value.Type) + "): " + result.Pdu.VbList[5].Value.ToString();
                        GetMore(int.Parse(result.Pdu.VbList[5].Value.ToString()));
                    }
                }
                else
                {
                    rtbGuiGet.Text = "No response received from SNMP agent.";
                }
                target.Close();
            }
            catch (Exception ex)
            {
                rtbGuiGet.Text = "Đã xảy ra lỗi: " + ex.Message.ToString();
            }
        }
Example #23
0
        private void btnSet_Click(object sender, EventArgs e)
        {
            IpAddress agent  = new IpAddress(this.IP);
            UdpTarget target = new UdpTarget((IPAddress)agent, 162, 2000, 1);

            Pdu pdu = new Pdu(PduType.Set);
            //pdu.VbList.Add(this.OID, new Integer32());


            OctetString     community = new OctetString(this.comm);
            AgentParameters param     = new AgentParameters(community);

            param.Version = SnmpVersion.Ver1;



            SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);
        }
Example #24
0
        private int getStatus(AgentParameters param, UdpTarget target, string OID_Status)
        {
            // Pdu class used for all requests
            Pdu pdu = new Pdu(PduType.Get);

            pdu.VbList.Add(OID_Status);

            // Make SNMP request
            SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);

            string value = result.Pdu.VbList[0].Value.ToString();

            try
            {
                return(Int32.Parse(value));
            }
            catch (FormatException e)
            {
                throw new FormatException(e.Message);
            }
        }
        public void ParseSnmpResponse8()
        {
            var packetVersion = SnmpPacket.GetProtocolVersion(Packet8, Packet8.Length);

            Assert.Equal(ESnmpVersion.Ver1, packetVersion);

            var packet = new SnmpV1Packet();

            packet.Decode(Packet8, Packet8.Length);

            Assert.Equal("public", packet.Community.ToString());

            Assert.False(packet.IsRequest);
            Assert.True(packet.IsResponse);

            Assert.Equal(41, packet.Pdu.RequestId);
            Assert.Equal(EPduErrorStatus.NoError, packet.Pdu.ErrorStatus);
            Assert.Equal(0, packet.Pdu.ErrorIndex);

            Assert.Equal(3, packet.Pdu.VbCount);

            var vb = packet.Pdu.GetVb(0);

            Assert.NotNull(vb);
            Assert.Equal(new Oid("1.3.6.1.4.1.253.8.64.4.2.1.7.10.14130104"), vb.Oid);
            Assert.Equal((byte)EAsnType.OctetString, vb.Value.Type);
            Assert.Equal(new byte[] { 0x31, 0x37, 0x32, 0x2e, 0x33, 0x31, 0x2e, 0x31, 0x39, 0x2e, 0x32 }, vb.Value as OctetString);

            vb = packet.Pdu.GetVb(1);
            Assert.NotNull(vb);
            Assert.Equal(new Oid("1.3.6.1.4.1.253.8.64.4.2.1.7.10.14130102"), vb.Oid);
            Assert.Equal((byte)EAsnType.OctetString, vb.Value.Type);
            Assert.Equal(new byte[] { 0x32, 0x35, 0x35, 0x2e, 0x32, 0x35, 0x35, 0x2e, 0x32, 0x35, 0x35, 0x2e, 0x30 }, vb.Value as OctetString);

            vb = packet.Pdu.GetVb(2);
            Assert.NotNull(vb);
            Assert.Equal(new Oid("1.3.6.1.4.1.253.8.64.4.2.1.5.10.14130400"), vb.Oid);
            Assert.Equal((byte)EAsnType.Integer, vb.Value.Type);
            Assert.Equal(1, vb.Value as Integer32);
        }
        static void Main(string[] args)
        {
            // SNMP community name
            OctetString community = new OctetString("public");

            // Define agent parameters class
            AgentParameters param = new AgentParameters(community);

            // Set SNMP version to 1 (or 2)
            param.Version = SnmpVersion.Ver1;
            // Construct the agent address object
            //IpAddress agent = new IpAddress("127.0.0.1"); //Taylor: Change the 127.0.0.1 to the IP address of your own controller's
            IpAddress agent = new IpAddress("192.168.2.109");                 //Taylor: Change the 127.0.0.1 to the IP address of your own controller's
            // Construct target
            UdpTarget target = new UdpTarget((IPAddress)agent, 501, 2000, 0); //Taylor: 503 should be the same NTCIP port in your controller's network setting. You may change to other port numbers (but be consistent with your controller) Other numbers do not matter

            // Pdu class used for all relevant requests
            Pdu pdu1  = new Pdu(PduType.Get);
            Pdu pdu2  = new Pdu(PduType.Get);
            Pdu pdu3  = new Pdu(PduType.Get);
            Pdu pdu4  = new Pdu(PduType.Get);
            Pdu pdu5  = new Pdu(PduType.Get);
            Pdu pdu6  = new Pdu(PduType.Get);
            Pdu pdu7  = new Pdu(PduType.Get);
            Pdu pdu8  = new Pdu(PduType.Get);
            Pdu pdu9  = new Pdu(PduType.Get);
            Pdu pdu10 = new Pdu(PduType.Get);
            Pdu pdu11 = new Pdu(PduType.Get);
            Pdu pdu12 = new Pdu(PduType.Get);
            Pdu pdu13 = new Pdu(PduType.Get);
            Pdu pdu14 = new Pdu(PduType.Get);
            Pdu pdu15 = new Pdu(PduType.Get);
            Pdu pdu16 = new Pdu(PduType.Get);

            //pdu class for set //taylor: add in May 2019
            Pdu pdu17 = new Pdu(PduType.Set);
            Pdu pdu18 = new Pdu(PduType.Set);
            Pdu pdu19 = new Pdu(PduType.Set);
            Pdu pdu20 = new Pdu(PduType.Set);
            Pdu pdu21 = new Pdu(PduType.Set);
            Pdu pdu22 = new Pdu(PduType.Set);


            pdu1.VbList.Add("1.3.6.1.4.1.1206.4.2.1.1.4.1.4.1"); //Taylor: green status of Phase 1-8;it will be a decimal integer. you need to convert it a 9-bit binary. for each bit, 1 is current green 0 is not green
            pdu2.VbList.Add("1.3.6.1.4.1.1206.4.2.1.1.4.1.4.2"); //Taylor: Phase 9-16; if not activated, then the returned value is zero
            pdu3.VbList.Add("1.3.6.1.4.1.1206.4.2.1.1.4.1.3.1"); //Taylor: yellow status of Phase 1-8; convert the returned decimal integer to binary. if a bit is 1, then currently that phase is yellow. otherwise is not yellow
            pdu4.VbList.Add("1.3.6.1.4.1.1206.4.2.1.1.4.1.3.2"); //Taylor: yellow phase 9-16; if the phase is no activated, then return value is zero
            //Taylor: no need to poll red since if neither green nor yellow, then it's red

            /////////////////////Ped phase status
            pdu5.VbList.Add("1.3.6.1.4.1.1206.4.2.1.1.4.1.7.1"); //Taylor: walk status of Ped Phase 1-8; convert the returned decimal integer to binary. if a bit is 1, then currently that phase is yellow. otherwise is not yellow
            pdu6.VbList.Add("1.3.6.1.4.1.1206.4.2.1.1.4.1.7.2"); //Taylor: walk status of Ped Phase 9-16; convert the returned decimal integer to binary. if a bit is 1, then currently that phase is yellow. otherwise is not yellow
            pdu7.VbList.Add("1.3.6.1.4.1.1206.4.2.1.1.4.1.6.1"); //Taylor: ped clearance status of Ped Phase 1-8; convert the returned decimal integer to binary. if a bit is 1, then currently that phase is yellow. otherwise is not yellow
            pdu8.VbList.Add("1.3.6.1.4.1.1206.4.2.1.1.4.1.6.2"); //Taylor: ped clearance of Ped Phase 9-16; convert the returned decimal integer to binary. if a bit is 1, then currently that phase is yellow. otherwise is not yellow
            //if a ped phase is neither walk nor ped clearance. then it's solid "don't walk"

            //Poll dets status
            pdu9.VbList.Add("1.3.6.1.4.1.1206.4.2.1.2.4.1.2.1");  //Taylor: det status of channel 1-8; returned a decimal integer. converted to a binary first
            pdu10.VbList.Add("1.3.6.1.4.1.1206.4.2.1.2.4.1.2.2"); //Taylor: det status of channel 9-16; returned a decimal integer. converted to a binary first
            pdu11.VbList.Add("1.3.6.1.4.1.1206.4.2.1.2.4.1.2.3"); //Taylor: det status of channel 17-24; returned a decimal integer. converted to a binary first
            pdu12.VbList.Add("1.3.6.1.4.1.1206.4.2.1.2.4.1.2.4"); //Taylor: det status of channel 25-32; returned a decimal integer. converted to a binary first
            pdu13.VbList.Add("1.3.6.1.4.1.1206.4.2.1.2.4.1.2.5"); //Taylor: det status of channel 33-40; returned a decimal integer. converted to a binary first
            pdu14.VbList.Add("1.3.6.1.4.1.1206.4.2.1.2.4.1.2.6"); //Taylor: det status of channel 41-48; returned a decimal integer. converted to a binary first
            pdu15.VbList.Add("1.3.6.1.4.1.1206.4.2.1.2.4.1.2.7"); //Taylor: det status of channel 49-56; returned a decimal integer. converted to a binary first
            pdu16.VbList.Add("1.3.6.1.4.1.1206.4.2.1.2.4.1.2.8"); //Taylor: det status of channel 57-64; returned a decimal integer. converted to a binary first

            //Phase controls //
            pdu17.VbList.Add(new Oid("1.3.6.1.4.1.1206.4.2.1.1.5.1.2.1"), new Integer32(255)); //Taylor: phase 1~8 omit (if binary, from right to left: phase 1-8, but you will need to input the corresponding decimal). e.g., if you wish to omit 3 and 7. the binary value will be 01000100 and the decimal value is 68. replace the last digit in the 1.3.6.xxxx.2.1 with 2 if you wish to omit 9~16
            pdu18.VbList.Add(new Oid("1.3.6.1.4.1.1206.4.2.1.1.5.1.3.1"), new Integer32(68));  //Taylor: Ped 1-8 omits
            pdu19.VbList.Add(new Oid("1.3.6.1.4.1.1206.4.2.1.1.5.1.4.1"), new Integer32(68));  //Taylor: Phase 1-8 hold . 1.3....4."1". can be relaced with 2 for phase 9~16
            pdu20.VbList.Add(new Oid("1.3.6.1.4.1.1206.4.2.1.1.5.1.5.1"), new Integer32(68));  //taylor place or lift phase 1-8 force-off (if you wish to force phase 1 and 5, the binary will be 00010001 and the decmial is 17
            pdu21.VbList.Add(new Oid("1.3.6.1.4.1.1206.4.2.1.1.5.1.6.1"), new Integer32(68));  //Taylor: place (1) or lift (0)phase 1-8 vehicle call
            pdu21.VbList.Add(new Oid("1.3.6.1.4.1.1206.4.2.1.1.5.1.7.1"), new Integer32(68));  //Taylor: place or lift ped call


            // Taylor: Just use the green 1-8 and yellow 1-8 and det 1-8 as illustration. Please extend to others PDU as defined above if needed
            SnmpV1Packet result1 = (SnmpV1Packet)target.Request(pdu1, param);

            // green status of phase 1-8
            if (result1 != null)
            {
                // ErrorStatus other then 0 is an error returned by
                // the Agent - see SnmpConstants for error definitions
                if (result1.Pdu.ErrorStatus != 0)
                {
                    // agent reported an error with the request
                    Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
                                      result1.Pdu.ErrorStatus,
                                      result1.Pdu.ErrorIndex);
                }
                else
                {
                    // Reply variables are returned in the same order as they were added
                    //  to the VbList
                    String green1to8 = result1.Pdu.VbList[0].Value.ToString();//Taylor: Using https://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html  to convert decimal to binary (from right to left, phase 1 to 8). You may need a function to do this in the code
                }
            }
            // yellow status of phase 1-8
            SnmpV1Packet result3 = (SnmpV1Packet)target.Request(pdu3, param);

            if (result3 != null)
            {
                // ErrorStatus other then 0 is an error returned by
                // the Agent - see SnmpConstants for error definitions
                if (result3.Pdu.ErrorStatus != 0)
                {
                    // agent reported an error with the request
                    Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
                                      result3.Pdu.ErrorStatus,
                                      result3.Pdu.ErrorIndex);
                }
                else
                {
                    // Reply variables are returned in the same order as they were added
                    //  to the VbList
                    String yellow1to8 = result3.Pdu.VbList[0].Value.ToString();
                }
            }
            // Status of det 1-8
            SnmpV1Packet result9 = (SnmpV1Packet)target.Request(pdu9, param);

            if (result9 != null)
            {
                // ErrorStatus other then 0 is an error returned by
                // the Agent - see SnmpConstants for error definitions
                if (result9.Pdu.ErrorStatus != 0)
                {
                    // agent reported an error with the request
                    Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
                                      result9.Pdu.ErrorStatus,
                                      result9.Pdu.ErrorIndex);
                }
                else
                {
                    // Reply variables are returned in the same order as they were added
                    //  to the VbList
                    String det1to8 = result9.Pdu.VbList[0].Value.ToString();
                }
            }
            else
            {
                Console.WriteLine("No response received from SNMP agent.");
            }

            //Set (write) example: phase 1-8 omit
            //SnmpV1Packet result17 = (SnmpV1Packet)target.Request(pdu17, param);
            SnmpV1Packet result17 = target.Request(pdu17, param) as SnmpV1Packet;// Taylor: I set the omit value 255 (1111111). it means all 8 phases are omit and you should be able to see this status on your front panel

            //                                                                                                            ^ (from left to right means the status of phase 8, 7,...,2,1)
            //If you wish to
            if (result17 != null)
            {
                // ErrorStatus other then 0 is an error returned by
                // the Agent - see SnmpConstants for error definitions
                if (result17.Pdu.ErrorStatus != 0)
                {
                    // agent reported an error with the request
                    Console.WriteLine("Error in SNMP reply. Error {0} index {1}", result17.Pdu.ErrorStatus, result17.Pdu.ErrorIndex);
                }
                else
                {
                    // Reply variables are returned in the same order as they were added
                    //  to the VbList
                    String phaseomit1to8 = result17.Pdu.VbList[0].Value.ToString();
                }
            }
            else
            {
                Console.WriteLine("No response received from SNMP agent.");
            }

            target.Close();
        }
Example #27
0
        public void GetInfoOID(DataTable dtInterfaceGauge, out DataTable dt)
        {
            dt = dtInterfaceGauge.Copy();
            if (dtInterfaceGauge.Rows.Count > 0)
            {
                // SNMP community name
                OctetString community = new OctetString(_community);

                // Define agent parameters class
                AgentParameters param = new AgentParameters(community);
                // Set SNMP version to 1 (or 2)
                param.Version = SnmpVersion.Ver1;
                // Construct the agent address object
                // IpAddress class is easy to use here because
                //  it will try to resolve constructor parameter if it doesn't
                //  parse to an IP address
                IpAddress agent = new IpAddress(_ipHost);

                // Construct target
                UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);

                // Pdu class used for all requests
                Pdu pdu = new Pdu(PduType.Get);

                for (int i = 0; i < dtInterfaceGauge.Rows.Count; i++)
                {
                    pdu.VbList.Add(dtInterfaceGauge.Rows[i]["OIDIn"].ToString());
                    pdu.VbList.Add(dtInterfaceGauge.Rows[i]["OIDOut"].ToString());
                }
                // Make SNMP request
                SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);

                // If result is null then agent didn't reply or we couldn't parse the reply.
                if (result != null)
                {
                    // ErrorStatus other then 0 is an error returned by
                    // the Agent - see SnmpConstants for error definitions
                    if (result.Pdu.ErrorStatus != 0)
                    {
                        // agent reported an error with the request
                        Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
                                          result.Pdu.ErrorStatus,
                                          result.Pdu.ErrorIndex);
                    }
                    else
                    {
                        // Reply variables are returned in the same order as they were added
                        //  to the VbList
                        // MessageBox.Show(result.Pdu.VbList[0].Oid.ToString() + " (" + SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type) + ") " + result.Pdu.VbList[0].Value.ToString());

                        for (int i = 0; i < dtInterfaceGauge.Rows.Count; i++)
                        {
                            dtInterfaceGauge.Rows[i]["InBandwidth"]  = result.Pdu.VbList[i + i].Value.ToString();
                            dtInterfaceGauge.Rows[i]["OutBandwidth"] = result.Pdu.VbList[i + i + 1].Value.ToString();
                        }
                        dt = dtInterfaceGauge;
                    }
                }
                else
                {
                    Console.WriteLine("No response received from SNMP agent.");
                }
                target.Close();
            }
        }
Example #28
0
        private void GetDisk()
        {
            OctetString     community = new OctetString(txtConStr.Text);
            AgentParameters param     = new AgentParameters(community);

            param.Version = SnmpVersion.Ver1;
            try
            {
                IpAddress agent  = new IpAddress(txtIPAgent.Text);
                UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);

                Pdu pdu = new Pdu(PduType.Get);
                pdu.VbList.Add("1.3.6.1.2.1.1.1.0");    //sysDescr
                pdu.VbList.Add("1.3.6.1.2.1.1.2.0");    //sysObjectID
                pdu.VbList.Add("1.3.6.1.2.1.1.3.0");    //sysUpTime
                pdu.VbList.Add("1.3.6.1.2.1.1.4.0");    //sysContact
                pdu.VbList.Add("1.3.6.1.2.1.1.5.0");    //sysName
                pdu.VbList.Add("1.3.6.1.2.1.25.1.2.0"); //hrSystemDate
                pdu.VbList.Add("1.3.6.1.2.1.25.1.5.0"); //hrSystemNumUsers
                pdu.VbList.Add("1.3.6.1.2.1.25.1.6.0"); //hrSystemProcesses
                pdu.VbList.Add("1.3.6.1.2.1.25.2.2.0"); //hrMemorySize
                //pdu.VbList.Add("1.3.6.1.2.1.1.5." + n.ToString()); //sysName
                //pdu.VbList.Add("1.3.6.1.2.1.2.1." + n.ToString()); //ifNumbber
                SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);

                if (result != null)
                {
                    if (result.Pdu.ErrorStatus != 0)
                    {
                        rtbGuiGet.Text = "Error in SNMP reply. Error " + result.Pdu.ErrorStatus + " index " + result.Pdu.ErrorIndex;
                    }
                    else
                    {
                        // Reply variables are returned in the same order as they were added
                        //  to the VbList
                        ////////////////////////////////////
                        //rtbAddInfo.Text = "hrSystemDate(" + result.Pdu.VbList[0].Oid.ToString() + ") (" + SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type) + "):\n\t" + result.Pdu.VbList[0].Value.ToString();
                        //rtbAddInfo.Text += "\n\t-------------------\n\t-------------------\n" + "hrSystemNumUsers(" + result.Pdu.VbList[1].Oid.ToString() + ") (" + SnmpConstants.GetTypeName(result.Pdu.VbList[1].Value.Type) + "): " + result.Pdu.VbList[1].Value.ToString();
                        //rtbAddInfo.Text += "\n\t-------------------\n" + "hrSystemProcesses(" + result.Pdu.VbList[2].Oid.ToString() + ") (" + SnmpConstants.GetTypeName(result.Pdu.VbList[2].Value.Type) + "): " + result.Pdu.VbList[2].Value.ToString();
                        rtbAddInfo.Text += "" + "hrMemorySize(" + result.Pdu.VbList[3].Oid.ToString() + ") (" + SnmpConstants.GetTypeName(result.Pdu.VbList[3].Value.Type) + "): " + result.Pdu.VbList[3].Value.ToString();
                        for (int i = 1; i < 10; i++)
                        {
                            pdu.Reset();
                            pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.3." + i.ToString());
                            try
                            {
                                result = (SnmpV1Packet)target.Request(pdu, param);
                                if (result.Pdu.VbList[0].Value.ToString() != "Null")
                                {
                                    rtbAddInfo.Text += "\n\t" + "hrStorageDescr." + i.ToString() + "(" + result.Pdu.VbList[0].Oid.ToString() + ") (" + SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type) + "): " + result.Pdu.VbList[0].Value.ToString();
                                }
                            }
                            catch (Exception)
                            { }
                        }
                    }
                }
                else
                {
                    rtbAddInfo.Text = "No response received from SNMP agent.";
                }
                target.Close();
            }
            catch (Exception ex)
            {
                rtbGuiGet.Text = "Đã xảy ra lỗi: " + ex.Message.ToString();
            }
        }
Example #29
0
        private void GetOne(int n, RichTextBox rtb)
        {
            n++;
            // SNMP community name
            OctetString community = new OctetString(txtConStr.Text);

            // Define agent parameters class
            AgentParameters param = new AgentParameters(community);

            // Set SNMP version to 1 (or 2)
            param.Version = SnmpVersion.Ver1;
            // Construct the agent address object
            // IpAddress class is easy to use here because
            //  it will try to resolve constructor parameter if it doesn't
            //  parse to an IP address
            try
            {
                IpAddress agent = new IpAddress(txtIPAgent.Text);

                // Construct target
                UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);

                // Pdu class used for all requests
                Pdu pdu = new Pdu(PduType.Get);
                pdu.VbList.Add("1.3.6.1.2.1.2.2.1.2." + n.ToString()); //ifDescr
                pdu.VbList.Add("1.3.6.1.2.1.2.2.1.3." + n.ToString()); //ifType
                pdu.VbList.Add("1.3.6.1.2.1.2.2.1.5." + n.ToString()); //ifSpeed
                pdu.VbList.Add("1.3.6.1.2.1.2.2.1.6." + n.ToString()); //ifPhysAddress
                pdu.VbList.Add("1.3.6.1.2.1.2.2.1.7." + n.ToString()); //ifAdminStatus
                pdu.VbList.Add("1.3.6.1.2.1.2.2.1.8." + n.ToString()); //ifOperStatus
                // Make SNMP request
                SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);

                // If result is null then agent didn't reply or we couldn't parse the reply.
                if (result != null)
                {
                    // ErrorStatus other then 0 is an error returned by
                    // the Agent - see SnmpConstants for error definitions
                    if (result.Pdu.ErrorStatus != 0)
                    {
                        // agent reported an error with the request

                        rtb.Text = "Error in SNMP reply. Error " + result.Pdu.ErrorStatus + " index " + result.Pdu.ErrorIndex;
                    }
                    else
                    {
                        // Reply variables are returned in the same order as they were added to the VbList
                        rtb.Text = "ifDescr(" + result.Pdu.VbList[0].Oid.ToString() + ") (" + SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type) + "): " + "\n\t" + result.Pdu.VbList[0].Value.ToString();
                        rtb.Text = rtb.Text + "\n" + "ifType(" + result.Pdu.VbList[1].Oid.ToString() + ") (" + SnmpConstants.GetTypeName(result.Pdu.VbList[1].Value.Type) + "): " + "\n\t" + GetType(int.Parse(result.Pdu.VbList[1].Value.ToString()));
                        rtb.Text = rtb.Text + "\n" + "ifSpeed(" + result.Pdu.VbList[2].Oid.ToString() + ") (" + SnmpConstants.GetTypeName(result.Pdu.VbList[2].Value.Type) + "): " + "\n\t" + result.Pdu.VbList[2].Value.ToString();
                        rtb.Text = rtb.Text + "\n" + "ifPhysAddress(" + result.Pdu.VbList[3].Oid.ToString() + ") (" + SnmpConstants.GetTypeName(result.Pdu.VbList[3].Value.Type) + "): " + "\n\t" + result.Pdu.VbList[3].Value.ToString();
                        rtb.Text = rtb.Text + "\n" + "ifAdminStatus(" + result.Pdu.VbList[4].Oid.ToString() + ") (" + SnmpConstants.GetTypeName(result.Pdu.VbList[4].Value.Type) + "): " + "\n\t" + GetStatus(int.Parse(result.Pdu.VbList[4].Value.ToString()));
                        rtb.Text = rtb.Text + "\n" + "ifOperStatus(" + result.Pdu.VbList[5].Oid.ToString() + ") (" + SnmpConstants.GetTypeName(result.Pdu.VbList[5].Value.Type) + "): " + "\n\t" + GetStatus(int.Parse(result.Pdu.VbList[5].Value.ToString()));
                    }
                }
                else
                {
                    rtb.Text = "No response received from SNMP agent.";
                }
                target.Close();
            }
            catch (Exception ex)
            {
                rtb.Text = "Đã xảy ra lỗi: " + ex.Message.ToString();
            }
        }
Example #30
0
        // Method broadcast references the IPEndPoint class -
        // IPEndPoint contains host and local or remote port information needed by an application to connect to a service on a host.
        // It does this by combining the host's IP address and port number of a service, the IPEndPoint class forms a connection point to a service.
        // link to msft docs - https://docs.microsoft.com/en-us/dotnet/api/system.net.ipendpoint?view=netframework-4.8
        private static void broadcast(object interfaceIP)
        {
            Spinner spin = new Spinner();

            spin.Start();

            object[] str;
            Socket   socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000);
            NetworkInterface.GetAllNetworkInterfaces();
            socket.Bind(new IPEndPoint((IPAddress)interfaceIP, 161)); // 161 = snmp udp port
            IPEndPoint   pEndPoint    = new IPEndPoint(IPAddress.Broadcast, 161);
            SnmpV1Packet snmpV2Packet = new SnmpV1Packet("public");

            snmpV2Packet.Pdu.VbList.Add("1.3.6.1.2.1.1.1.0");
            socket.SendTo(snmpV2Packet.encode(), pEndPoint);
            IPEndPoint pEndPoint1 = new IPEndPoint(IPAddress.Any, 0);

            byte[]   numArray = new byte[32768];
            DateTime now      = DateTime.Now;

            while ((DateTime.Now - now).TotalSeconds < 20)
            {
                int num = 0;
                try
                {
                    EndPoint endPoint = pEndPoint1;

                    num = socket.ReceiveFrom(numArray, ref endPoint);

                    pEndPoint1 = (IPEndPoint)endPoint;
                }
                catch
                {
                    SocketException se = new SocketException();
                    num = -1;
                }
                if (num > 0)
                {
                    if ((pEndPoint1.Address.Equals(IPAddress.Broadcast) ? false : !pEndPoint1.Address.Equals((IPAddress)interfaceIP)))
                    {
                        try
                        {
                            snmpV2Packet.decode(numArray, num);
                            DeviceDiscovery.UpdateListViewCallback updateListViewCallback = new DeviceDiscovery.UpdateListViewCallback(UpdateListView);
                            if (snmpV2Packet.Pdu.VbList[0].Value.ToString() != "Null")
                            {
                                string truncatedName = trunc(snmpV2Packet.Pdu.VbList[0].Value.ToString(), 29);
                                ipList.Add(pEndPoint1.Address.ToString());
                                nameList.Add(truncatedName);
                            }
                        }
                        catch
                        {
                            continue;
                        }
                    }
                }
            }
            socket.Close();
            if (ipList.Count == 0)
            {
                Console.WriteLine("No devices found on network.");
            }
            else
            {
                spin.Stop();
                // clear current line
                int currLine = Console.CursorTop;
                Console.SetCursorPosition(0, Console.CursorTop);
                Console.Write(new string(' ', Console.WindowWidth));
                Console.SetCursorPosition(0, currLine);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.DarkMagenta;
                Console.WriteLine("{0,-33} {1,25}", "Name", "IP Address  ");
                Console.WriteLine("{0,-33} {1,25}", "---------------------------", "----------- ");

                for (int x = 0; x < nameList.Count; x++)
                {
                    Console.ForegroundColor = ConsoleColor.DarkCyan;
                    Console.Write("{0,-33}", (x + 1) + ". " + nameList[x]);

                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("{0,25}", ipList[x]);
                }

                displayMenu();
            }
        }