Skip to content

Commit

Permalink
Added support for SSH connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Abbin44 committed Jul 16, 2021
1 parent 5410e17 commit 0e0761f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CustomShell/CustomShell.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
</Reference>
<Reference Include="Microsoft.VisualStudio.DebuggerVisualizers, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="PresentationCore" />
<Reference Include="Renci.SshNet, Version=2020.0.1.0, Culture=neutral, PublicKeyToken=1cee9f8bde3db106, processorArchitecture=MSIL">
<HintPath>..\packages\SSH.NET.2020.0.1\lib\net40\Renci.SshNet.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression" />
Expand Down Expand Up @@ -73,6 +76,7 @@
<Compile Include="Processes.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SSHClient.cs" />
<Compile Include="TreeGenerator.cs" />
<Compile Include="WandEditor.cs" />
<EmbeddedResource Include="MainController.resx">
Expand Down
24 changes: 23 additions & 1 deletion CustomShell/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down
76 changes: 76 additions & 0 deletions CustomShell/SSHClient.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
1 change: 1 addition & 0 deletions CustomShell/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FluentFTP" version="34.0.0" targetFramework="net472" />
<package id="SSH.NET" version="2020.0.1" targetFramework="net472" />
</packages>

0 comments on commit 0e0761f

Please sign in to comment.