views:

216

answers:

3

I've been trying very hard to automatically ssh into a Linux server. What's crazy, is that I can create a .bat script, that will do it, but I have to be there physically, to type in the password.

I've tried automating this using System.Diagnostics.Process object in c# to no end. There is no way, I've found, to make this object allow you to see the password prompt. It out puts every line up until that point, and then doesn't output any more lines.

cmd.exe, does really well at allowing you to jump from process to process in a script; It consolidates everything into one screen, and just prompts you for things such as passwords, and then you type them in.

Is there any simple way, in C#, to make cmd.exe think you are a human being typing in it, so I can simulated this programmatically? Otherwise, System.Diagnostics.Process, doesn't seem to offer a way to interacted with a process that outputs a password prompt. You never get to see the prompt that clearly happens when you run the same thing in cmd.exe

+1  A: 

You could use

Windows.Forms.SendKeys() to send password as keystrokes

Reference Material

Vivek Bernard
The problem is, I can't get C# to even make the password prompt available. The System.Diagnostics.Process object, outputs ever line of the command line process until I expect the prompted to be output, and then stops outputting lines.
LonnieBest
The process is setting their waiting for a password to be entered, and I have no way via System.Diagnostics.Process to access the prompt.
LonnieBest
+2  A: 

cmd.exe is not the SSH client; it's just a shell for one. Explore the capabilities of the actual client. In PuTTY (very popular!) there's an autologon feature, if you have an SSH key.

Seva Alekseyev
Agreed. You really need to look into ssh keys. They provide a secure way to login via SSH without having to type your password in each way.
GnomeCubed
I've implemented keys already. They work in cmd.exe, but in c#'s System.Diagnostics.Process object, all output (accessible through this process object) ceases right before (or during) ssh-key-authentication. In when I double-click a .bat file, though, output proceeds past authentication.
LonnieBest
If I use that same .bat file (that doesn't get hung up), as my System.Diagnostics.Process.Filename, the process object is unable to output lines after authentication.
LonnieBest
Why are you using a bat file? Why not call the SSH client directly with the relevant command line? With a bat file, your Process object is not the SSH client - it's the instance of CMD.exe that runs the bat file. I can envision I/O redirection becoming flakey because of this.
Seva Alekseyev
+1  A: 

Without knowing which commandline tool you are using to ssh to the server, isn't there a commandline parameter to provide a password.

C:\> ssh user@ip -p password

Or have you tried input redirection:

ssh user@ip <
Yourpassword
EOF

In looking into your earlier question, what is the trouble you are having in using the programmer interface?

Are you sure you need a full fledged ssh client, or do you just want to execute a command remotely. If it is the latter you might want to check out plink.

Yannick M.
No, ssh doesn't allow the password to be entered as a parameter. Yes, I've tried input redirection. Perhaps, the essence of the problem, is, I can't continue to interact after I authenticate.
LonnieBest
You see, even when I've prevented having to type the password (via ssh-key-authentication), as soon as authentication occurs, I can no longer monitor line output from the System.Diagnostics.Process object.
LonnieBest
It is like there is some kind of tricky security stuff going on at that point. I'm programmer, not a hacker. Perhaps I'm going to have to become hacker to write this program!
LonnieBest
What exactly are you trying to do?
Yannick M.
You can find further details here:http://www.lonniebest.com/Help/
LonnieBest
Yannick M.