Using a SSH connection in a Unity project

Hello everyone,

I m trying to use SSH in a Unity project as i need a secured connection to an other server than the one hosting my project. This second server is not mine and this connection would allow me to call some bash scripts created and maintain by its owner.

I found two libraries : SharpSSH and SSH.NET

I didn’t manage to get the first one working so i wont talk about it.
The second one provides a 3.5 .NET dll (as Unity don’t work with 4.0), a good documentation and is easy to use.

It works perfectly in the development environment of Unity, but no longer after compilation. My goal is a web player client, but i also tried to compile it as a standalone build on windows, with the same result.

So I created a new project, with only a UI.Text in it and this script attached :

using UnityEngine;
using Renci.SshNet;

public class sshconnection : MonoBehaviour
{
	private UnityEngine.UI.Text text = null;
	
	private string _host = "host.h";
	private string _username = "login";
	private string _password = "password";

	void Start()
	{
		text = this.GetComponent<UnityEngine.UI.Text> ();

		try
		{
			var connectionInfo = new PasswordConnectionInfo(_host, 22, _username, _password);
			text.text += "connection infos : ok

";

			using (var client = new SshClient(connectionInfo ))
			{
				text.text += "Connecting...

";
client.Connect();
text.text += "OK
";

				var command = client.RunCommand("pwd");
				text.text += command.Result;
				
				text.text += "Disconnecting...

";
client.Disconnect();
text.text += "OK
";

				Debug.Log (text.text);
			}
		}
		catch(System.Exception e)
		{
			text.text = "Error

" + e;
Debug.Log(text.text + e);

		}
	}	
}

Of course the expected result would be an unknown host exception. In the following screenshot, the first error is in french even if i don’t know why, it’s written “Hôte inconnu” wich means “unknown host”.

Here’s the results :
http://imgur.com/a/leNqY

So i have two questions :

What am I missing to get this working ?

Should i reconsider using ssh for whatever reasons ?

Hi Tydero,
i used your code and it worked for me, maybe check that you can connect via something like putty first. the only error i get is "Line has invalid autocommand “pwd"Disconnecting…” wondering if you may know what this is because it seems to happen after the authentication.

maybe we can help each other to get it working as i need it for one of my projects as well

Hi Tydero,
its ok, maybe someone else will see this and be able to shed some light on it, when i use “var x = client.RunCommand(“known command”);” i get the below error. did some searching and someone said that it may be a part of unity’s security that is blocking me but i am unsure. basically i wand to use either telnet or ssh (preferably ssh) to view lets call them logs from a local ip address. i will then filter the data and only show the parts with the tags that i need. if anyone has any ideas i am open to them.

Error
System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine.

at System.Net.Sockets.Socket.Send (System.Byte buf, Int32 offset, Int32 size, SocketFlags flags) [0x00000] in :0
at Renci.SshNet.Session.SocketWrite (System.Byte data) [0x00000] in :0 System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine.

@Tydero @Packetstorm I was able to make it work by using .NET 2.0 instead of .NET 2.0 subset. I had some compilation when stripping was enabled , disable stripping also if you have any problem.