private void RestartController(object mSender, EventArgs e) { Task.Run(() => { string IP = ""; string Building = ""; string Room = ""; string Tag = ""; Task.Run(() => { Button sender = (Button)mSender; IP = RoomIPs[sender.Id]; Building = ((MainPage)Application.Current.MainPage)._Controllers.FindByIP(IP).Building; Room = ((MainPage)Application.Current.MainPage)._Controllers.FindByIP(IP).Room; Tag = ((MainPage)Application.Current.MainPage)._Controllers.FindByIP(IP).Tag; }); using (Renci.SshNet.SshClient sshClient = new Renci.SshNet.SshClient(IP, "Username", "Password")) { sshClient.HostKeyReceived += (_sender, _e) => { _e.CanTrust = true; }; sshClient.ConnectionInfo.Timeout = TimeSpan.FromSeconds(30); try { sshClient.Connect(); } catch { return; } var amxStream = sshClient.CreateShellStream("amxStream", 0, 0, 0, 0, 256); bool streamLockToken = true; var streamTTL = DateTime.Now.Add(TimeSpan.FromSeconds(30)); while (streamLockToken) { if (amxStream.DataAvailable) { streamLockToken = false; } else if (DateTime.Now >= streamTTL) { sshClient.Disconnect(); return; } else { System.Threading.Thread.Sleep(0); } } amxStream.WriteLine("reboot"); sshClient.Disconnect(); } }); }
// Protected implementation of Dispose pattern. protected virtual void Dispose(bool disposing) { if (disposed) { return; } if (disposing) { if (writer != null) { writer.Flush(); } if (reader != null) { reader.Dispose(); } if (stream != null) { stream.Dispose(); } if (client != null) { if (client.IsConnected) { client.Disconnect(); } client.Dispose(); } } disposed = true; }
public void Close() { try { ssh?.Disconnect(); } catch { } try { scp?.Disconnect(); } catch { } ssh = null; scp = null; }
private void cmdTest_Click(object sender, EventArgs e) { try { string machineName = ApplyConfigVarsOnField(txtMachineName.Text); string userName = ApplyConfigVarsOnField(txtUsername.Text); string password = ApplyConfigVarsOnField(txtPassword.Text); string privateKeyFile = ApplyConfigVarsOnField(txtPrivateKeyFile.Text); string passPhrase = ApplyConfigVarsOnField(txtPassPhrase.Text); using (Renci.SshNet.SshClient sshClient = SshClientTools.GetSSHConnection( optPrivateKey.Checked ? SSHSecurityOption.PrivateKey : optPassword.Checked?SSHSecurityOption.Password: SSHSecurityOption.KeyboardInteractive, machineName, (int)sshPortNumericUpDown.Value, userName, password, privateKeyFile, passPhrase)) { if (sshClient.IsConnected) { MessageBox.Show(string.Format("Success\r\n{0}", sshClient.RunCommand("cat /proc/version").Result), "Test", MessageBoxButtons.OK, MessageBoxIcon.Information); } sshClient.Disconnect(); } } catch (Exception ex) { MessageBox.Show(string.Format("Fail!\r\n{0}", ex.Message), "Test", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void CloseConnection(bool force = false) { if (currentConnection != null && (!Persistent || force)) { currentConnection.Disconnect(); currentConnection = null; } }
// runs multiple commands on the server public bool runCommands(string[] commands) { Renci.SshNet.SshCommand sshCommand; Renci.SshNet.SshClient sshClient = new Renci.SshNet.SshClient(this.ipAddress, 8888, this.username, this.password); sshClient.Connect(); foreach (string s in commands) { sshCommand = sshClient.RunCommand(s); Thread.Sleep(100); } sshClient.Disconnect(); return(true); }
// Brute SSH public static void SSH(string host, string username, string password) { var SSH = new Renci.SshNet.SshClient(host, username, password); try { SSH.Connect(); SSH.Disconnect(); core.Exit("[SSH] Logged in", output); } catch { output.error = true; core.Exit("[SSH] Failed to login", output); } finally { SSH.Dispose(); } }
public static void AdoptDevice(string IP, string ControllerURL = "http://unifi.scharsich.dev:8080/inform") { using (var client = new Renci.SshNet.SshClient(IP, "ubnt", "ubnt")) { client.Connect(); var cmd = client.CreateCommand("mca-cli-op set-inform " + ControllerURL); cmd.Execute(); if (cmd.ExitStatus != 0) { MessageBox.Show(cmd.Error); } else { MessageBox.Show("Device " + IP + " adopted."); } client.Disconnect(); } }
bool IProfileItem.Stop() { using (Renci.SshNet.SshClient client = new Renci.SshNet.SshClient(Value, Username, AES.DecryptString(Password))) { try { client.Connect(); var cmd = client.RunCommand(string.Format("{0}stop.sh", Path)); OnLog(new LogEventArgs(string.Format("Attempting to stop all serivices on {0}", Name))); client.Disconnect(); } catch (SocketException) { OnLog(new LogEventArgs(string.Format("Unable to connect to server {0}", Value))); } } return(true); }
public Task StartAsync(CancellationToken cancellationToken) { new Thread(() => { if (_options.SSHSettings != null) { Logs.Configuration.LogInformation($"SSH settings detected, testing connection to {_options.SSHSettings.Username}@{_options.SSHSettings.Server} on port {_options.SSHSettings.Port} ..."); var connection = new Renci.SshNet.SshClient(_options.SSHSettings.CreateConnectionInfo()); connection.HostKeyReceived += (object sender, Renci.SshNet.Common.HostKeyEventArgs e) => { e.CanTrust = true; if (!_options.IsTrustedFingerprint(e.FingerPrint, e.HostKey)) { Logs.Configuration.LogWarning($"SSH host fingerprint for {e.HostKeyName} is untrusted, start BTCPay with -sshtrustedfingerprints \"{Encoders.Hex.EncodeData(e.FingerPrint)}\""); } }; try { connection.Connect(); connection.Disconnect(); Logs.Configuration.LogInformation($"SSH connection succeeded"); } catch (Renci.SshNet.Common.SshAuthenticationException) { Logs.Configuration.LogWarning($"SSH invalid credentials"); } catch (Exception ex) { var message = ex.Message; if (ex is AggregateException aggrEx && aggrEx.InnerException?.Message != null) { message = aggrEx.InnerException.Message; } Logs.Configuration.LogWarning($"SSH connection issue: {message}"); } finally { connection.Dispose(); } } }) { IsBackground = true }.Start();
public void Dispose() { sshClient.Disconnect(); sshClient.Dispose(); }
private void RestartController(object mSender, RoutedEventArgs mE) { Task.Run(() => { string IP = ""; string Building = ""; string Room = ""; string Tag = ""; (mSender as Button).Dispatcher.Invoke(() => { IP = (mSender as Button).Tag.ToString(); Building = ((MainWindow)Application.Current.MainWindow)._1337.FindByIP(IP).Building; Room = ((MainWindow)Application.Current.MainWindow)._1337.FindByIP(IP).Room; Tag = ((MainWindow)Application.Current.MainWindow)._1337.FindByIP(IP).Tag; }); using (Renci.SshNet.SshClient sshClient = new Renci.SshNet.SshClient(IP, "username", "password")) { sshClient.HostKeyReceived += (_sender, _e) => { _e.CanTrust = true; }; Log("Attempting to connect to " + Building + " " + Room + " " + Tag + " controller"); sshClient.ConnectionInfo.Timeout = TimeSpan.FromSeconds(30); try { sshClient.Connect(); } catch { Log("Controller " + Building + " " + Room + " " + Tag + " connection timed out"); return; } Log("Connected to " + Building + " " + Room + " " + Tag + " controller"); Log("Attempting to reboot " + Building + " " + Room + " " + Tag + " controller"); var amxStream = sshClient.CreateShellStream("amxStream", 0, 0, 0, 0, 256); bool streamLockToken = true; var streamTTL = DateTime.Now.Add(TimeSpan.FromSeconds(30)); while (streamLockToken) { if (amxStream.DataAvailable) { streamLockToken = false; } else if (DateTime.Now >= streamTTL) { Log("Controller " + Building + " " + Room + " " + Tag + " connection nonresponsive"); sshClient.Disconnect(); return; } else { System.Threading.Thread.Sleep(0); } } amxStream.WriteLine("reboot"); Log("Reboot " + Building + " " + Room + " " + Tag + " command sent"); sshClient.Disconnect(); } }); }