C# send a simple SSH command

C#Ssh

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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();
}

Solution 2 - C#

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

Solution 3 - C#

Solution 4 - C#

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();
}

Solution 5 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlucbasView Question on Stackoverflow
Solution 1 - C#Karl-Johan SjögrenView Answer on Stackoverflow
Solution 2 - C#Mario PalaciosView Answer on Stackoverflow
Solution 3 - C#NickDView Answer on Stackoverflow
Solution 4 - C#Salah AkbariView Answer on Stackoverflow
Solution 5 - C#Michael SantosView Answer on Stackoverflow