Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run commands on SSH server in C#?

Tags:

c#

.net

ssh

ssh.net

I need to execute this action using a C# code:

  1. open putty.exe in the background (this is like a cmd window)
  2. login to a remote host using its IP address
  3. enter a user name and password
  4. execute several commands one after the other.
  5. run another command that gets a response telling me that the commands I ran before that where executed successfully

So I'm trying to do it like this:

ProcessStartInfo proc = new ProcessStartInfo() 
{
     FileName = @"C:\putty.exe",
     UseShellExecute = true, //I think I need to use shell execute ?
     RedirectStandardInput = false,
     RedirectStandardOutput = false,
     Arguments = string.Format("-ssh {0}@{1} 22 -pw {2}", userName, hostIP, password)
     ... //How do I send commands to be executed here ?
};
Process.Start(proc);
like image 526
Liran Friedman Avatar asked Jun 17 '15 05:06

Liran Friedman


People also ask

Can I use CMD for SSH?

You can start an SSH session in your command prompt by executing ssh user@machine and you will be prompted to enter your password. You can create a Windows Terminal profile that does this on startup by adding the commandline setting to a profile in your settings.

What is SSH command in CMD?

In this article OpenSSH (Secure Shell) is a standard protocol for secure terminal connections. You can use SSH to connect to all Factory OS images.

What is the syntax for SSH?

The ssh syntax is ssh username@destation and then hitting enter and supplying your password. In this case your username will be your hawkID and your password.


1 Answers

You could try https://sshnet.codeplex.com/. With this you wouldn't need putty or a window at all. You can get the responses too. It would look sth. like this.

SshClient sshclient = new SshClient("172.0.0.1", userName, password);    
sshclient.Connect();
SshCommand sc= sshclient .CreateCommand("Your Commands here");
sc.Execute();
string answer = sc.Result;

Edit: Another approach would be to use a shellstream.

Create a ShellStream once like:

ShellStream stream = sshclient.CreateShellStream("customCommand", 80, 24, 800, 600, 1024);

Then you can use a command like this:

  public StringBuilder sendCommand(string customCMD)
    {
        StringBuilder answer;

        var reader = new StreamReader(stream);
        var writer = new StreamWriter(stream);
        writer.AutoFlush = true; 
        WriteStream(customCMD, writer, stream);
        answer = ReadStream(reader);
        return answer;
    }

private void WriteStream(string cmd, StreamWriter writer, ShellStream stream)
    {
        writer.WriteLine(cmd);
        while (stream.Length == 0)
        {
            Thread.Sleep(500);
        }
    }

private StringBuilder ReadStream(StreamReader reader)
    {
        StringBuilder result = new StringBuilder();

        string line;
        while ((line = reader.ReadLine()) != null)
        {
            result.AppendLine(line);
        }
        return result;
    }
like image 65
LzyPanda Avatar answered Oct 01 '22 03:10

LzyPanda