- Home /
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\n";
using (var client = new SshClient(connectionInfo ))
{
text.text += "Connecting...\n";
client.Connect();
text.text += "OK\n";
var command = client.RunCommand("pwd");
text.text += command.Result;
text.text += "Disconnecting...\n";
client.Disconnect();
text.text += "OK\n";
Debug.Log (text.text);
}
}
catch(System.Exception e)
{
text.text = "Error\n" + 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 ?
Drag & drop into /Assets/Plugins
edit : use directly the 3.5 .NET dll
HI,
I tried to test your script on android device and I have same problem. Script working perfectly in Unity Editor but on android device I have same error message. I don't have idea to how to solve this problem ;/
I am trying to install this library in Unity on my mac. I am deploying onto iOS. I would like more info on how this library is installed. Where do i get the 3.5 .NET dll?
Answer by Packetstorm · Jul 13, 2015 at 05:48 AM
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.
Hello,
Putty was working fine. I stopped using ssh since i didn't manage to get it working soon enough. I m sorry, but i cant really help you.
Did you try another simple command like "ls" (UNIX) / "dir" (WINDOWS) ? $$anonymous$$aybe put a ending-line symbol at the end ("\n" or "\r\n") ?
If you find a way to get it working, it would be great if you post it there, because if remember well, i was on the first page of google not long after i asked this ...
Good luck on get it working !
Answer by gfxguru · May 12, 2016 at 02:49 PM
@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.