Example #1
0
        //Miktorik Button
        public void button5_Click(object sender, EventArgs e)
        {
            checkedListBox1.Enabled = true;
            checkedListBox2.Enabled = true;
            checkedListBox1.Items.Clear();
            checkedListBox2.Items.Clear();
            checkedListBox1.BackColor = Color.White;
            checkedListBox2.BackColor = Color.White;



            //DataTable table1 = new DataTable("IPAddress");
            //table1.Columns.Add("name");
            //table1.Columns.Add("ip");

            if (radio_api.Checked)
            {
                //--------------------------------------------------------------------------------------------------------------------------
                //API Method 1st as it's default
                //Using API Method



                MK mikrotik = new MK(txbsshserver.Text);
                if (!mikrotik.Login(txbsshuser.Text, txbsshpassword.Text))
                {
                    MessageBox.Show("Oooops: Unable To Connect");

                    //textBox2.AppendText("Could not log in");
                    mikrotik.Close();
                    return;
                }
                //mikrotik.Send("/system/identity/getall");
                mikrotik.Send("/ip/firewall/address-list/getall");
                //mikrotik.Send("?detail");
                mikrotik.Send(".tag=|", true);


                foreach (string h in mikrotik.Read())
                {
                    if (h.IndexOf("list") != -1)
                    {
                        //Get The IP List
                        var iplist = h.Substring(h.IndexOf("list") + 5, h.IndexOf("=address") - 18);
                        //Apply regex to get the IP
                        var arr = Regex.Matches(h, @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b").Cast <Match>().Select(m => m.Value).ToArray();

                        //var reg = @"^.*(get|GET).+(w00t|setup|phpMyAdmin|mysql|websql|muieblackcat|phpTest|vtigercrm).*$";

                        foreach (var address in arr)
                        {
                            table1.Rows.Add(iplist, address);
                        }
                    }
                }
            }

            //--------------------------------------------------------------------------------------------------------------------------



            //--------------------------------------------------------------------------------------------------------------------------
            //SSH Method

            if (radio_ssh.Checked)
            {
                try
                {
                    using (var client = new SshClient(txbsshserver.Text, txbsshuser.Text, txbsshpassword.Text))
                    {
                        client.Connect();


                        //SSH Command in here
                        var command = client.RunCommand("ip firewall address-list print");

                        var result = command.Result;
                        //result = result.Substring(0, result.Length - 1);    //  Remove \n character returned by command

                        string value = command.Result;
                        client.Disconnect();
                        //
                        // Split the string on line breaks.
                        // ... The return value from Split is a string array.
                        //
                        string[] lines = Regex.Split(value, "\r\n");

                        //foreach (string line in lines)
                        //{

                        //    // textBox2.AppendText(line +Environment.NewLine);
                        //}

                        //Apply regex to output from SSH
                        var arr = Regex.Matches(result, @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b")
                                  .Cast <Match>()
                                  .Select(m => m.Value)
                                  .ToArray();

                        //var iplist = result.Substring(result.IndexOf("list") + 5, result.IndexOf("=address") - 18);

                        //Add IP to listbox
                        foreach (var address in arr)
                        {
                            checkedListBox2.Items.Add(address, true);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Oooops " + Environment.NewLine + Environment.NewLine + ex.Message);
                }
            }
            //--------------------------------------------------------------------------------------------------------------------------



            //DataSet setz = new DataSet("mikrotik");
            //setz.Tables.Add(table1);

            var names = table1.AsEnumerable().Select(r => r.Field <string>("name")).ToArray().Distinct();

            foreach (var nam in names)
            {
                checkedListBox1.Items.Add(nam, true);
                //listBox1.Items.Add(nam);
            }

            var ipz = table1.AsEnumerable().Select(r => r.Field <string>("ip")).ToArray().Distinct();

            foreach (var nam in ipz)
            {
                checkedListBox2.Items.Add(nam, true);
                //listBox1.Items.Add(nam);
            }
        }//End Mikrotik Button
Example #2
0
        //Miktorik Button
        public void button5_Click(object sender, EventArgs e)
        {
            checkedListBox1.Enabled = true;
            checkedListBox2.Enabled = true;
            checkedListBox1.Items.Clear();
            checkedListBox2.Items.Clear();
            checkedListBox1.BackColor = Color.White;
            checkedListBox2.BackColor = Color.White;

            //DataTable table1 = new DataTable("IPAddress");
            //table1.Columns.Add("name");
            //table1.Columns.Add("ip");

            if (radio_api.Checked)
            {
                //--------------------------------------------------------------------------------------------------------------------------
                //API Method 1st as it's default
                //Using API Method

                MK mikrotik = new MK(txbsshserver.Text);
                if (!mikrotik.Login(txbsshuser.Text, txbsshpassword.Text))
                {
                    MessageBox.Show("Oooops: Unable To Connect");

                    //textBox2.AppendText("Could not log in");
                    mikrotik.Close();
                    return;
                }
                //mikrotik.Send("/system/identity/getall");
                mikrotik.Send("/ip/firewall/address-list/getall");
                //mikrotik.Send("?detail");
                mikrotik.Send(".tag=|", true);

                foreach (string h in mikrotik.Read())
                {
                    if (h.IndexOf("list") != -1)
                    {
                        //Get The IP List
                        var iplist = h.Substring(h.IndexOf("list") + 5, h.IndexOf("=address") - 18);
                        //Apply regex to get the IP
                        var arr = Regex.Matches(h, @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b").Cast<Match>().Select(m => m.Value).ToArray();

                        //var reg = @"^.*(get|GET).+(w00t|setup|phpMyAdmin|mysql|websql|muieblackcat|phpTest|vtigercrm).*$";

                        foreach (var address in arr)
                        {
                            table1.Rows.Add(iplist, address);

                        }
                    }
                }
            }

            //--------------------------------------------------------------------------------------------------------------------------

            //--------------------------------------------------------------------------------------------------------------------------
            //SSH Method

            if (radio_ssh.Checked)
            {
                try
                {
                    using (var client = new SshClient(txbsshserver.Text, txbsshuser.Text, txbsshpassword.Text))
                    {
                        client.Connect();

                        //SSH Command in here
                        var command = client.RunCommand("ip firewall address-list print");

                        var result = command.Result;
                        //result = result.Substring(0, result.Length - 1);    //  Remove \n character returned by command

                        string value = command.Result;
                        client.Disconnect();
                        //
                        // Split the string on line breaks.
                        // ... The return value from Split is a string array.
                        //
                        string[] lines = Regex.Split(value, "\r\n");

                        //foreach (string line in lines)
                        //{

                        //    // textBox2.AppendText(line +Environment.NewLine);
                        //}

                        //Apply regex to output from SSH
                        var arr = Regex.Matches(result, @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b")
                                .Cast<Match>()
                                .Select(m => m.Value)
                                .ToArray();

                        //var iplist = result.Substring(result.IndexOf("list") + 5, result.IndexOf("=address") - 18);

                        //Add IP to listbox
                        foreach (var address in arr)
                        {
                            checkedListBox2.Items.Add(address, true);
                        }

                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Oooops " + Environment.NewLine + Environment.NewLine + ex.Message);
                }

            }
            //--------------------------------------------------------------------------------------------------------------------------

            //DataSet setz = new DataSet("mikrotik");
            //setz.Tables.Add(table1);

            var names = table1.AsEnumerable().Select(r => r.Field<string>("name") ).ToArray().Distinct();

            foreach (var nam in names)
            {
                checkedListBox1.Items.Add(nam, true);
                //listBox1.Items.Add(nam);
            }

            var ipz = table1.AsEnumerable().Select(r => r.Field<string>("ip")).ToArray().Distinct();

            foreach (var nam in ipz)
            {
                checkedListBox2.Items.Add(nam, true);
                //listBox1.Items.Add(nam);
            }
        }