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?

7
  • Have you seen this Commented Jun 23, 2012 at 12:16
  • 3
    It would also be amazing for us if you share what you have actually tried for that.... Commented Jun 23, 2012 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 Here a picture of the code (well^^) Click Me Commented Jun 23, 2012 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? Commented Jun 23, 2012 at 13:11
  • 1
    @justinb138 I'm pretty sure he's not writing an ASP.NET web site based on the screenshot. Commented Dec 4, 2014 at 15:41

5 Answers 5

123

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();
}
Sign up to request clarification or add additional context in comments.

14 Comments

where did you find SSH.NET samples? Thanks
It's available under Downnloads on their codeplex page as "SshNet Help": sshnet.codeplex.com/releases/view/120565. Not the most common way to supply documentation but it's there at least.
@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..?
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-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.
|
10
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

2 Comments

this worked for me as i needed to use a port that wasnt the default.
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.
3

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

2 Comments

Dont support multiple connections :(
Tamir SSH is deprecated. It uses old easy-hacking protocols. SSH.net is a newer and more secure solution.
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();
}

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.