private void btn_upload_Click(object sender, EventArgs e) { this.btn_upload.Enabled = false; string ip = this.tb_upload_ip.Text; string port = this.tb_upload_port.Text; string pwd = this.tb_upload_pwd.Text; string serverPath = this.tb_upload_serverPath.Text; string user = this.tb_upload_user.Text; string[] up_ip = ip.Split('\n'); this.lb_errorCount.Text = "0"; this.lb_successCount.Text = "0"; this.lb_runCount.Text = "0"; this.lb_count.Text = "0"; this.lb_count.Text = up_ip.Length + ""; new Thread(() => { foreach (string upload_ip in up_ip) { string _ip = upload_ip.Replace("\r", ""); foreach (string filePath in filePaths) { try { Renci.SshNet.ConnectionInfo connectionInfo = SshUtil.getSsh(_ip, Convert.ToInt32(port), user, pwd); string fileName = filePath.Substring(filePath.LastIndexOf("\\")).Substring(1); log("upload -> " + filePath); log("ip -> " + _ip); SshUtil.Upload(connectionInfo, filePath, fileName, serverPath); log(_ip + " upload is ok"); this.btn_upload.Enabled = true; this.lb_successCount.Text = Convert.ToInt32(this.lb_successCount.Text) + 1 + ""; } catch (Exception err) { this.lb_errorCount.Text = Convert.ToInt32(this.lb_errorCount.Text) + 1 + ""; this.tb_errorIp.Text += _ip + "\r\n"; this.tb_errorMessage.Text += err + "\r\n"; log("upload error ip ->" + _ip); log("upload error file ->:" + filePath); } this.lb_runCount.Text = Convert.ToInt32(this.lb_runCount.Text) + 1 + ""; } } }).Start(); }
private void btn_send_Click(object sender, EventArgs e) { string ips = this.tb_ip.Text; string[] vs = ips.Split('\n'); string port = this.tb_port.Text; string pwd = this.tb_pwd.Text; string script = this.tb_script.Text; string initPort = this.tb_initPort.Text; string user = this.tb_user.Text; bool @checked = this.cb_kill.Checked; this.lb_errorCount.Text = "0"; this.lb_successCount.Text = "0"; this.lb_runCount.Text = "0"; this.lb_count.Text = "0"; this.lb_count.Text = vs.Length + ""; foreach (var item in vs) { new Thread(() => { string ip = item.Replace("\r", ""); Renci.SshNet.ConnectionInfo connectionInfo = SshUtil.getSsh(ip, Convert.ToInt32(port), user, pwd); if (@checked) { try { string com = "netstat -lnp|grep " + initPort; log("execute -> " + com); string findPort = SshUtil.executeCommand(connectionInfo, com); log("resutl -> " + findPort); int index = findPort.LastIndexOf("/"); findPort = findPort.Substring(index - 10, 10).Trim(); findPort = Regex.Replace(findPort, "[a-zA-Z]", ""); string end = findPort.Substring(findPort.Length - 1); Convert.ToInt32(end); string kill = "kill -9 " + findPort; log("execute -> " + kill); string re = SshUtil.executeCommand(connectionInfo, kill); log(re); findPort = findPort.Substring(0, findPort.Length - 1); } catch (Exception err) { this.tb_errorIp.Text += item + "\r\n"; this.tb_errorMessage.Text += err + "\r\n"; } } log("execute -> " + script); string result = SshUtil.executeCommand(connectionInfo, script); if (!result.Equals("")) { this.lb_successCount.Text = Convert.ToInt32(this.lb_successCount.Text) + 1 + ""; string[] sp = result.Split('&'); try { this.tb_errorIp.Text += sp[0].Split(':')[1] + "\r\n"; this.tb_errorMessage.Text += result + "\r\n"; } catch (Exception) { } } else { this.lb_errorCount.Text = Convert.ToInt32(this.lb_errorCount.Text) + 1 + ""; } result = result.Replace("\n", "\r\n"); log("resutl -> " + result); log("执行完成!"); this.lb_runCount.Text = Convert.ToInt32(this.lb_runCount.Text) + 1 + ""; }).Start(); } }