67

I'm a young a student and received my homework for this week. It's pretty difficult for me because I have to create a program, that is able to connect to an SSH server and send the command "etc/init.d/networking restart".

I should program this application in C# and I'm pretty new to it (Just have learned from some school lessons). I also should create a GUI.

I understand the basics of C# (loop, if etc...).

I've already created GUI, menus, buttons and a log listbox. GUI = 3 textboxes (ip, username, password) and a button to connect.

screenshot of ui

screenshot of code

I'm coding with Microsoft Vistual Studio.

My question is: How can I establish an SSH connection to my server?

Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91
lucbas
  • 847
  • 1
  • 9
  • 16
  • Have you seen [this](http://www.dart.com/sftp-ssh-code-examples-samples.aspx) – Jacob Seleznev Jun 23 '12 at 12:16
  • 3
    It would also be amazing for us if you share what you have actually tried for that.... – Akash KC Jun 23 '12 at 12:18
  • Hi, thx for your fast answers JacobSeleznev Thanks for this website. But I just can't afford this product. @LolCoder For sure :D I've created a GUI [Click Me](http://www.bilder-hochladen.net/files/haru-4-a87f-png-nb.html) Here a picture of the code (well^^) [Click Me](http://www.bilder-hochladen.net/files/haru-5-e4da-png.html) – lucbas Jun 23 '12 at 12:42
  • @justinb138 Thanks for your solution. I've downloaded the library from the download page. Now a little question: How can I implement this library in my program? – lucbas Jun 23 '12 at 13:11
  • Start Here: http://msdn.microsoft.com/en-us/library/f3st0d45.aspx – justinb138 Jun 23 '12 at 13:16
  • @justinb138 I'm pretty sure he's not writing an ASP.NET web site based on the screenshot. – Dan Bechard Dec 04 '14 at 15:41
  • ssh.net doesn't support sha-2 and you should expect issues when connecting to new OS like Fedora 35 – TGN12 Feb 01 '22 at 05:21

5 Answers5

119

I used SSH.Net in a project a while ago and was very happy with it. It also comes with a good documentation with lots of samples on how to use it.

The original package website can be still found here, including the documentation (which currently isn't available on GitHub).

For your case the code would be something like this.

using (var client = new SshClient("hostnameOrIp", "username", "password"))
{
    client.Connect();
    client.RunCommand("etc/init.d/networking restart");
    client.Disconnect();
}
Karl-Johan Sjögren
  • 16,544
  • 7
  • 59
  • 68
  • 1
    where did you find SSH.NET samples? Thanks – Frank Schwieterman Jun 17 '15 at 01:54
  • 3
    It's available under Downnloads on their codeplex page as "SshNet Help": http://sshnet.codeplex.com/releases/view/120565. Not the most common way to supply documentation but it's there at least. – Karl-Johan Sjögren Jun 17 '15 at 07:46
  • @Karl-JohanSjögren, I have downloaded the SSH.NET and if I want to call this particular Unix file `anly_load_int.sh` from .net.. what would the RunCommand look like..? – MethodMan Mar 08 '16 at 03:54
  • 1
    Well depending on where on the target machine the file is located it should be something like `sh /path/to/anly_load_int.sh`. – Karl-Johan Sjögren Mar 08 '16 at 06:30
  • 1
    @Karl-JohanSjögren, thanks. For others, if you don't see content when viewing the .chm file, remember to rt click on it in your file manager and in properties tick the "unblock" checkbox. – Kent Kruckeberg Mar 27 '17 at 20:45
  • Out of curiosity, do you need to explicitly disconnect if you are using your client in a `using` block? – Jay Sep 13 '17 at 14:58
  • I'm pretty sure that it will do a clean disconnect when disposing the client, but it doesn't hurt to disconnect yourself. I think that example was from their documentation however so maybe it was needed back then? – Karl-Johan Sjögren Sep 13 '17 at 15:01
  • I think this is unsecured. How is the certification handled? – Boy Feb 27 '18 at 22:43
  • Well using username and password isn't the best way to handle SSH connections but that was what the OP asked for. If you want to use certificates to authenticate then SSH.Net supports that as well. – Karl-Johan Sjögren Feb 28 '18 at 06:14
  • Sadly, CodePlex's shutdown means documentation isn't available any more. – NetMage Jul 11 '18 at 23:24
  • But documentation appears to be available at [Nudoq](http://www.nudoq.org/#!/Projects/SSH.NET). – NetMage Jul 11 '18 at 23:49
  • The docs at nudoc seems to be simply generated from the assembly and doesn't contain the samples that the old file had. They also seem to have a new chm file on github now but it seems to be the same as the Nudoq ones. The original one can be found at https://github.com/ch4/ch4zilla/blob/master/Renci.SshNet/Documentation/Help/SshNet.Help.chm though it might be a bit dated but it check the SshClient class documentation for a sample of how to really use it. – Karl-Johan Sjögren Jul 12 '18 at 09:13
  • Disconnect() call is required? Dispose() call Disconnect()? – Ivan Montilla Sep 20 '19 at 09:35
  • I haven't worked with this in quite a while but generally disposing of something should close its underlying connection. So it probably isn't required. – Karl-Johan Sjögren Sep 20 '19 at 13:25
9
SshClient cSSH = new SshClient("192.168.10.144", 22, "root", "pacaritambo");
cSSH.Connect();
SshCommand x = cSSH.RunCommand("exec \"/var/lib/asterisk/bin/retrieve_conf\"");
cSSH.Disconnect();
cSSH.Dispose();

//using SSH.Net

Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
  • this worked for me as i needed to use a port that wasnt the default. – Dan Hastings Mar 25 '16 at 11:34
  • 1
    You should actually put a using at the beginning of line 1. This keeps you from having to remember to Dispose on line 5 and will dispose even in the event of an error which prevents memory leaks. – PRMan May 05 '21 at 21:32
3

SharpSSH should do the job. http://www.codeproject.com/Articles/11966/sharpSsh-A-Secure-Shell-SSH-library-for-NET

NickD
  • 2,672
  • 7
  • 37
  • 58
2

Late answer, but for me none of the solution worked unfortunately. Actually, If you need to execute multiple commands, and also the commands depend on each other (first command modified the environment, e.g. variables, that are used by the latter commands), you have to execute them within one channel. Use a shell syntax for that, like && or ;:

using (var client = new SshClient("hostnameOrIp", "username", "password"))
{
    client.Connect();

    using (var command = client.CreateCommand("command1 && command2"))
    {
        Console.Write(command.Execute()); //Don't forget to Execute the command
    }

    client.Disconnect();
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
0

For .Net core i had many problems using SSH.net and also its deprecated. I tried a few other libraries, even for other programming languages. But i found a very good alternative. https://stackoverflow.com/a/64443701/8529170

Michael Santos
  • 466
  • 7
  • 15