tags:

views:

53

answers:

4

This is what I'm doing:

  ssh_ = new Process();
  ssh_.StartInfo.FileName = SshProvider;
  ssh_.StartInfo.Arguments = "-t -t " + Environment.UserName + "@" + serverName_;
  ssh_.StartInfo.UseShellExecute = false;
  ssh_.StartInfo.RedirectStandardInput = true;
  ssh_.StartInfo.RedirectStandardOutput = true;
  ssh_.StartInfo.RedirectStandardError = true;
  ssh_.StartInfo.CreateNoWindow = true;
  ssh_.Start();
  new Thread(new ThreadStart(ReadStdOut)).Start();
  new Thread(new ThreadStart(ReadStdErr)).Start();

I want to read the password prompt from stdout (or stderr) and write the password into stdin, but ssh is writing and reading from the console from which I started my mono app. I don't understand why that is, since I have redirected those streams in the code above.

+3  A: 

ssh does not read from stdin. This is very intentional behavior designed specifically to prevent people from trying to pass in passwords from scripts or other programs. They do not have a flag to disable this behavior; there is no way to change it.

The intended way to perform automated logins is to use the private/public key mechanism to bypass the password prompt. The high level steps to do that are:

  1. Run ssh-keygen on the client to create a public-private key pair.
  2. Add the contents of the public key file (e.g. ~/.ssh/id_rsa.pub) to the server's authorized hosts file (e.g. ~/.ssh/authorized_keys2).

By doing this you authorize the client to connect to the server using its keys so there is no password needed.

John Kugelman
A: 

Please have a look on python pexpect that solves this issue.

pexpect.sourceforge.net

http://linux.byexamples.com/archives/346/python-how-to-access-ssh-with-pexpect/

Taharqa

+2  A: 

I should be using a C# SSH library like SharpSSH or Granados, rather than trying to interact with another process.

Fantius
A: 

It is a little bit more complicated than that.

SSH and other applications expect a few more services for their "input" than a stream of bytes, they require a terminal to be active.

A terminal offers various services, like turning on and off the character "echo" (you turn it off to enter passwords, turn it on for regular operation). Be able to capture certain control sequences (selectively ignore interrupt or suspend signals), be notified of changes on the display screen (ssh needs this so it can in turn notify the remote end that the terminal size changed, and your remote editor can properly redisplay itself).

Unix provides a service called "pseudo terminals" (shortened as "pty") that provide this functionality and Mono has a .NET binding that lets you control processes using pseudo terminals, you can find the code here:

http://github.com/mono/pty-sharp

There is a sample included there.

miguel.de.icaza