How to run several commands with SSH.Net?

Liran Friedman picture Liran Friedman · Jun 17, 2015 · Viewed 11.3k times · Source

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

Answer