diff --git a/CustomShell/CustomShell.csproj b/CustomShell/CustomShell.csproj index d36586e..f2243b5 100644 --- a/CustomShell/CustomShell.csproj +++ b/CustomShell/CustomShell.csproj @@ -41,6 +41,9 @@ + + ..\packages\SSH.NET.2020.0.1\lib\net40\Renci.SshNet.dll + @@ -73,6 +76,7 @@ + diff --git a/CustomShell/MainController.cs b/CustomShell/MainController.cs index 09fc6b0..de60d32 100644 --- a/CustomShell/MainController.cs +++ b/CustomShell/MainController.cs @@ -21,6 +21,7 @@ public partial class MainController : Form Compression comp; BatchInterpreter batch; FTPController ftpController; + SSHClient sshClient; LocalDirectory localDirectory; public static MainController controller { get; private set; } @@ -546,9 +547,10 @@ public void Shutdown() #endregion int historyIndex = 0; + public string[] tokens; + private void inputBox_KeyDown(object sender, KeyEventArgs e) { - string[] tokens; //When command is entered if (e.KeyCode == Keys.Enter) { @@ -790,6 +792,26 @@ private void inputBox_KeyDown(object sender, KeyEventArgs e) inputBox.Text = InputPrefix(); inputBox.SelectionStart = inputBox.Text.Length; break; + case true when cmds[i].StartsWith("ssh"): + if(sshClient == null) + sshClient = new SSHClient(); + + if (tokens[0] == "sshCom" || tokens[0] == "sshcom") + { + StringBuilder sb = new StringBuilder(); + for (int x = 1; x < tokens.Length; ++x) + { + sb.Append(tokens[x]); + if(x + 1 < tokens.Length) + sb.Append(" "); + } + sshClient.SendCommand(sb.ToString()); + } + else if (tokens[0] == "ssh" && tokens[1] == "close") + sshClient.TerminateConnection(); + else if(tokens[0] == "ssh" && tokens[1] == "connect") + sshClient.EstablishConnection(tokens[2], tokens[3], tokens[4]); + break; default: AddTextToConsole("Command does not exist"); break; diff --git a/CustomShell/SSHClient.cs b/CustomShell/SSHClient.cs new file mode 100644 index 0000000..7b9c4b3 --- /dev/null +++ b/CustomShell/SSHClient.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Renci.SshNet; + +namespace CustomShell +{ + class SSHClient + { + SshClient client; + public SSHClient() + { + + } + + public void EstablishConnection(string host, string username, string password) + { + try + { + ConnectionInfo connInfo = new ConnectionInfo(host, username, new PasswordAuthenticationMethod(username, password), new PrivateKeyAuthenticationMethod("rsa.key")); + client = new SshClient(connInfo); + client.Connect(); + MainController.controller.AddTextToConsole("Successfully connected to the host..."); + MainController.controller.inputBox.Text = MainController.controller.InputPrefix(); + MainController.controller.inputBox.SelectionStart = MainController.controller.inputBox.Text.Length; + } + catch (Exception) + { + MainController.controller.AddTextToConsole("Could not connect, did you enter the right login credentials?"); + return; + } + } + + public void SendCommand(string command) + { + try + { + SshCommand result = client.RunCommand(command); + MainController.controller.AddCommandToConsole(MainController.controller.tokens); + + //Add a red color for the ssh output + MainController.controller.AddTextToConsole(result.Result); + MainController.controller.outputBox.Select(MainController.controller.outputBox.Text.Length - result.Result.Length - 1, MainController.controller.outputBox.Text.Length); + MainController.controller.outputBox.SelectionColor = Color.Red; + MainController.controller.outputBox.SelectionStart = MainController.controller.outputBox.Text.Length; + + MainController.controller.inputBox.Text = MainController.controller.InputPrefix(); + MainController.controller.inputBox.SelectionStart = MainController.controller.inputBox.Text.Length; + } + catch (Exception) + { + MainController.controller.AddTextToConsole("Could not execute command..."); + return; + } + } + + public void TerminateConnection() + { + try + { + client.Disconnect(); + MainController.controller.AddTextToConsole("Successfully terminated the connection..."); + MainController.controller.inputBox.Text = MainController.controller.InputPrefix(); + MainController.controller.inputBox.SelectionStart = MainController.controller.inputBox.Text.Length; + } + catch (Exception) + { + MainController.controller.AddTextToConsole("Could not terminate..."); + return; + } + } + } +} diff --git a/CustomShell/packages.config b/CustomShell/packages.config index b0307dd..692c5ec 100644 --- a/CustomShell/packages.config +++ b/CustomShell/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file