From ab450135f908b93fd905bd486bf842d1585e027f Mon Sep 17 00:00:00 2001 From: popcorn Date: Mon, 28 Dec 2020 03:07:10 -0600 Subject: [PATCH] ssh cleanup/commenting --- ProcessAudiobooks-UI/ssh.cs | 71 +++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/ProcessAudiobooks-UI/ssh.cs b/ProcessAudiobooks-UI/ssh.cs index 74861da..94376f5 100644 --- a/ProcessAudiobooks-UI/ssh.cs +++ b/ProcessAudiobooks-UI/ssh.cs @@ -1,4 +1,4 @@ -using Renci.SshNet; +using Renci.SshNet; using System; using System.CodeDom; using System.Collections.Generic; @@ -9,6 +9,9 @@ namespace ProcessAudiobooks_UI { + //This is ment to be a easy object to handle ssh connections + //It will handle storing the data required to make ssh connections such as. Username, Password, ip and port. + // public class ssh { public string ip { get; set; } @@ -16,59 +19,83 @@ public class ssh public string username { get; set; } public string password { get; set; } - + //gather and save the user information and the port required to use this object and make a connection public ssh(string ip, int port, string username, string password) { this.ip = ip; this.port = port; this.username = username; this.password = password; - + } + //gather and save the user information to use this object and make a connection + public ssh(string ip, string username, string password) + { + this.ip = ip; + this.port = 22; //assume if not specified that the port is 22 + this.username = username; + this.password = password; + + } + + //Handle a self test on running a ssh connection public async Task sshStartTest() { + //try to make a connection to the ssh server. if it fails send up a message try { await RunCommand("echo 'SSH Connection Test Successful'"); - } catch - { + { //if a failure does end up occuring. Notify the user and give up System.Windows.MessageBox.Show("SSH Server down or The connection settings are wrong"); ConsoleWindow.WriteInfo("SSH Server down or The connection settings are wrong"); throw new SSHConnectionFailed(); + //Note. I really think i should rethink this when im not half asleep. } ConsoleWindow.WriteInfo("SSH connected!"); } + //run a ssh command as specified. + //if an error occurs this will throw public async Task RunCommand(string command) { - using (var client = new SshClient(this.ip, this.port, this.username, this.password)) + try //catch errors and throw a nicer looking exception forward. { - client.Connect(); - ConsoleWindow.WriteInfo(command); - var cmd = client.CreateCommand(command); - var result = cmd.BeginExecute(); - - using (var reader = - new StreamReader(cmd.OutputStream, Encoding.UTF8, true, -1, true)) + //create the ssh client object which we are using. + using (var client = new SshClient(this.ip, this.port, this.username, this.password)) { - while (!result.IsCompleted || !reader.EndOfStream) - { - string line = await reader.ReadLineAsync(); - if (line != null) + client.Connect(); + ConsoleWindow.WriteInfo(command); //debug write the command to the console window so we can find it easily if something goes wrong. + var cmd = client.CreateCommand(command); + var result = cmd.BeginExecute(); + + //read all the stdout from the ssh client until it has stopped sending new lines/end of file/completed. + using (var reader = + new StreamReader(cmd.OutputStream, Encoding.UTF8, true, -1, true)) + { + while (!result.IsCompleted || !reader.EndOfStream) { - ConsoleWindow.WriteInfo("[SSH] " + line); + string line = await reader.ReadLineAsync(); //read data from the reader + if (line != null) + { + ConsoleWindow.WriteInfo("[SSH] " + line); //print the data to the ConsoleWindow if its important + } + await Task.Delay(10); //add a small delay to prevent this loop from going out of control } - await Task.Delay(10); //add a small delay to prevent this loop from going out of control } + cmd.EndExecute(result); + ConsoleWindow.WriteInfo("Finished"); } - cmd.EndExecute(result); - ConsoleWindow.WriteInfo("Finished"); + } catch + { + throw new SSHConnectionFailed(); } } } - + + //ssh connection failed exception + //this is ment to be an exception thrown if the connection failed for whatever reason. This should be caught later in the code to potentially bring this to the users attention public class SSHConnectionFailed : Exception { public SSHConnectionFailed()