public static Renci.SshNet.SshClient GetSSHConnection(SSHSecurityOption sshSecurityOption, string machineName, int sshPort, string userName, string password, string privateKeyFile, string passCodeOrPhrase) { Renci.SshNet.SshClient sshClient; if (sshSecurityOption == SSHSecurityOption.Password) { byte[] b = System.Text.UTF8Encoding.UTF8.GetBytes(password.ToCharArray()); Renci.SshNet.AuthenticationMethod am = new Renci.SshNet.PasswordAuthenticationMethod(userName, b); Renci.SshNet.ConnectionInfo ci = new Renci.SshNet.ConnectionInfo(machineName, sshPort, userName, am); sshClient = new Renci.SshNet.SshClient(ci); } else if (sshSecurityOption == SSHSecurityOption.PrivateKey) { Renci.SshNet.PrivateKeyFile[] pkf = new Renci.SshNet.PrivateKeyFile[1]; pkf[0] = new Renci.SshNet.PrivateKeyFile(privateKeyFile, passCodeOrPhrase); Renci.SshNet.PrivateKeyAuthenticationMethod pm = new Renci.SshNet.PrivateKeyAuthenticationMethod(userName, pkf); Renci.SshNet.ConnectionInfo ci = new Renci.SshNet.ConnectionInfo(machineName, sshPort, userName, pm); sshClient = new Renci.SshNet.SshClient(ci); } else { Renci.SshNet.KeyboardInteractiveAuthenticationMethod kauth = new Renci.SshNet.KeyboardInteractiveAuthenticationMethod(userName); Renci.SshNet.PasswordAuthenticationMethod pauth = new Renci.SshNet.PasswordAuthenticationMethod(userName, password); keyBoardPassword = password; kauth.AuthenticationPrompt += new EventHandler <Renci.SshNet.Common.AuthenticationPromptEventArgs>(HandleKeyEvent); sshClient = new Renci.SshNet.SshClient(new Renci.SshNet.ConnectionInfo(machineName, sshPort, userName, pauth, kauth)); } if (machineName.Trim().Length > 0) { sshClient.Connect(); } return(sshClient); }
public LiveHostClient(string sHostName, string sRemoteSupportUser, string sRemoteSupportPassphrase) { RemoteSupportPassphrase oReportSupportPassphrase = new RemoteSupportPassphrase(); String sUserPassword = oReportSupportPassphrase.Decode(sRemoteSupportPassphrase); Renci.SshNet.AuthenticationMethod authMethod = new Renci.SshNet.PasswordAuthenticationMethod(sRemoteSupportUser, sUserPassword); scpConnInfo = new Renci.SshNet.ConnectionInfo(sHostName, sRemoteSupportUser, new[] { authMethod }); scpClient = new Renci.SshNet.ScpClient(scpConnInfo); MemoryStream xmlStream = new MemoryStream(); scpClient.Connect(); System.Threading.Thread.Sleep(3000); try { scpClient.Download(@"/usr/local/platform/conf/platformConfig.xml", xmlStream); } catch (Exception ex) { string bob = ex.Message; } scpClient.Disconnect(); byte[] xmlDataBytes = new byte[xmlStream.Length]; xmlStream.Seek(0, SeekOrigin.Begin); xmlStream.Read(xmlDataBytes, 0, (int)xmlStream.Length); Console.Write("{0}\n", Functions.encoding.GetString(xmlDataBytes)); }
public Client(string sHostName, string sRemoteSupportUser, string sRemoteSupportPassphrase) { RemoteSupportPassphrase oReportSupportPassphrase = new RemoteSupportPassphrase(); String sUserPassword = oReportSupportPassphrase.Decode(sRemoteSupportPassphrase); Renci.SshNet.AuthenticationMethod authMethod = new Renci.SshNet.PasswordAuthenticationMethod(sRemoteSupportUser, sUserPassword); scpConnInfo = new Renci.SshNet.ConnectionInfo(sHostName, sRemoteSupportUser, new[] { authMethod }); scpClient = new Renci.SshNet.ScpClient(scpConnInfo); }
public SftpClient(object options) { BlendedJSEngine.Clients.Value.Add(this); var host = options.GetProperty("host").ToStringOrDefault(); var port = options.GetProperty("port").ToIntOrDefault(); var user = options.GetProperty("user").ToStringOrDefault(); var password = options.GetProperty("password").ToStringOrDefault(); var connectionInfo = new Renci.SshNet.ConnectionInfo(host, user, new Renci.SshNet.PasswordAuthenticationMethod(user, password)); //new Renci.SshNet.PrivateKeyAuthenticationMethod("rsa.key")); _client = new Renci.SshNet.SftpClient(connectionInfo); }
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 static void OpenSftpConnection() { try { Utils.CLogger.WriteLog("Opening SFTP CONNECTION START"); connectioninfo = new Renci.SshNet.ConnectionInfo(SFTP_HOST, SFTP_USER, new Renci.SshNet.PasswordAuthenticationMethod(SFTP_USER, SFTP_PWD), new Renci.SshNet.PrivateKeyAuthenticationMethod("rsa.key")); SFTPClient = new Renci.SshNet.SftpClient(connectioninfo); SFTPClient.Connect(); Utils.CLogger.WriteLog("SFTP CONNECTION Opened with HOST " + SFTP_HOST); Utils.CLogger.WriteLog("Waiting for commands "); } catch (Exception ex) { Utils.CLogger.WriteLog(ex); } }
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(); } }