private void buttonRun_Click(object sender, EventArgs e) { if (buttonRun.Text == "Stop") { main_thread.Abort(); main_thread = null; buttonRun.Text = "Run"; this.Cursor = Cursors.Default; labelStatus.Text = "Successfully stopped"; return; } if (!File.Exists(textBoxIPsFile.Text)) { MessageBox.Show("Can't read IPs file.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //# Clearing workspace: listViewResults.Items.Clear(); labelStatus.Text = "Woho! Loading bullets..."; Application.DoEvents(); //# Target ip list: List<Router> list_of_target_routers = new List<Router>(); //# Read file looking for target ips: using (StreamReader sr = new StreamReader(textBoxIPsFile.Text, Encoding.Default)) { //# Possible improvement for next version: be able to read telnet user, password and enable password directly from IP list txt. var telnet_user = textBoxTelnetUser.Text; var telnet_password = textBoxTelnetPassword.Text; var enable_password = textBoxEnablePassword.Text; var router_ip = string.Empty; while ((router_ip = sr.ReadLine()) != null) { if (isIPv4(router_ip)) { Router router = new Router(router_ip, telnet_user, telnet_password, enable_password); list_of_target_routers.Add(router); ListViewItem viewitem = new ListViewItem(router_ip); viewitem.SubItems.Add(telnet_user); viewitem.SubItems.Add(telnet_password); viewitem.SubItems.Add(enable_password); viewitem.Tag = router; listViewResults.Items.Add(viewitem); } } } if (listViewResults.Items.Count > 0) { labelStatus.Text = "Bullets are ready! Shooting now..."; Application.DoEvents(); } else { labelStatus.Text = "No targets detected :-("; return; } //# Launching main and unique thread for all telnet conections. //# This could be upgraded to a multithreaded application, but this is a really simple app and performance is not the target. main_thread = new Thread(new ParameterizedThreadStart(mainThreadForTelnetConnections)); main_thread.Start(list_of_target_routers); GC.KeepAlive(main_thread); buttonRun.Text = "Stop"; this.Cursor = Cursors.WaitCursor; }
private void routerTelnetConnection(Router router, bool telnet, bool enable) { //# Select the router item in listview: ListViewItem router_item = null; foreach (ListViewItem item in listViewResults.Items) { if (item.Tag == router) { router_item = item; break; } } //# If there is not connection to telnet service, I mark it as red line on listview. if (!telnet) { router_item.BackColor = Color.Crimson; } //# If there is telnet connection, I mark it green telnet column and enable column if proceed. else { router.telnet_connection_available = true; router_item.BackColor = Color.PaleGreen; if (!enable) { router_item.UseItemStyleForSubItems = false; router_item.SubItems[0].BackColor = Color.PaleGreen; router_item.SubItems[1].BackColor = Color.PaleGreen; router_item.SubItems[2].BackColor = Color.PaleGreen; router_item.SubItems[3].BackColor = Color.Crimson; } else { router.enable_command_available = true; } } }