コード例 #1
0
ファイル: FormMain.cs プロジェクト: radtek/SwitchControl
        //--------------------------------------------------------------------------------------------------------------
        private void btFind_Click(object sender, EventArgs e)
        {
            NetController testSwitch = new NetController("10.10.14.101");

            testSwitch.GetMacsOnPorts();
            //testSwitch.DecMAC2Hex();

            using (Process process = new Process())
            {
                process.StartInfo.FileName               = @"d:\Temp\SnmpWalk.exe";
                process.StartInfo.UseShellExecute        = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.Arguments              = "-r:" + tbIP.Text + " -v:2c -c:public -os:.1.3.6.1.2.1.17.7.1.2.2.1.2.1 -op:.1.3.6.1.2.1.17.7.1.2.2.1.2.2";
                process.Start();

                /*   1.3.6.1.2.1.1.1 - sysDescr
                 *   1.3.6.1.2.1.1.2 - sysObjectID
                 *   1.3.6.1.2.1.1.3 - sysUpTime
                 *   1.3.6.1.2.1.1.4 - sysContact
                 *   1.3.6.1.2.1.1.5 - sysName
                 *   1.3.6.1.2.1.1.6 - sysLocation
                 *   1.3.6.1.2.1.1.7 - sysServices    */
                // Synchronously read the standard output of the spawned process.
                StreamReader reader = process.StandardOutput;
                string       output = reader.ReadToEnd();
                //richTextBox1.Text = output;
                string[]     words   = output.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
                ListViewItem l       = new ListViewItem();
                String       pattern = "(OID=.1.3.6.1.2.1.17.7.1.2.2.1.2.1.)|(Type=)|(Value=)";
                listView1.Items.Clear();
                string oct = "";
                string s;
                foreach (string str in words)
                {
                    l = new ListViewItem();
                    //l.Text = str;
                    foreach (string str2 in Regex.Split(str, ", "))
                    {
                        s = Regex.Replace(str2, pattern, String.Empty);
                        {
                            //if (s.Count('.') == 4)
                            {
                                string[] sw = s.Split('.');
                                string   ff = "";
                                if (sw.Count() == 6)
                                {
                                    foreach (string ss in sw)
                                    {
                                        oct = Convert.ToString(Convert.ToInt32(ss), 16);
                                        if (oct.Length == 1)
                                        {
                                            ff += "0";
                                        }
                                        ff += (oct);
                                        ff += tbSeparator.Text;
                                    }
                                    s = ff.Substring(0, ff.Length - 1);
                                }
                            }
                            l.SubItems.Add(s);
                        }
                    }



                    //  Если есть фильтр порта
                    //if (l.SubItems.Count > 2 && l.SubItems[3].Text != cbPort.Text)
                    if (l.SubItems.Count > 2 && !cbPort.Text.Contains(l.SubItems[3].Text) /* && !cbPortFilter.Checked*/)
                    {
                        //                        tbIP.Text = dgvDevicesList.Rows[dgvDevicesList.CurrentRow.Index].Cells["Router"].Value.ToString();


                        // Еще условие - если стоит чекбокс показывать только порт - смотрим только его!!!
                        if (!cbOnlyPort.Checked && !cbOnlyIP.Checked)
                        {
                            //добавим в конец мак - только надо чтобы до этого считался лист ARP!
                            string t = l.SubItems[1].Text;
                            l.SubItems.Add(GetIpFromMAC(l.SubItems[1].Text));
                            listView1.Items.Add(l);//добавим в лист}
                        }
                        else
                        if (l.SubItems[3].Text == tbPortOnly.Text)
                        {
                            //добавим в конец мак - только надо чтобы до этого считался лист ARP!
                            string t = l.SubItems[1].Text;
                            l.SubItems.Add(GetIpFromMAC(l.SubItems[1].Text));
                            listView1.Items.Add(l);        //добавим в лист}
                        }
                        else
                        {
                            if (GetIpFromMAC(l.SubItems[1].Text) == tbIPAddr.Text)
                            {
                                //добавим в конец мак - только надо чтобы до этого считался лист ARP!
                                string t = l.SubItems[1].Text;
                                l.SubItems.Add(GetIpFromMAC(l.SubItems[1].Text));
                                listView1.Items.Add(l);        //добавим в лист}
                            }
                        }
                    }
                }
                // Write the redirected output to this application's window.
                //Console.WriteLine(output);
                process.WaitForExit();
            }
        }