0

I would like to connect use ssh to connect to an other computer. Following you can see my code.

        ProcessStartInfo startinfo = new ProcessStartInfo();
        startinfo.FileName = @"C:\Program Files (x86)\PuTTY\putty.exe";
        startinfo.Arguments = "-ssh <user>@<domain> -pw <password>";
        Process process = new Process();
        process.StartInfo = startinfo;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardInput = true;
        process.Start();
        process.StandardInput.WriteLine("<command>");
        process.WaitForExit();
        Console.ReadKey();

With that I have two problems. Firstly, the command should wait until the connection is successful established. Secondly, the command is not shown in the cmd. I don't know if don't see the commands because it's not waiting until the connection is successful or it is just not working.

I really appreciate any help.

etienne84
  • 1
  • 2

1 Answers1

1

Using the GUI application "Putty" to initiate a ssh connection is not optimal. First of all, Putty just gives you a messagebox with an error in it on an unsuccesfull connection attempt, e.g. putty_error

In your program-logic, you have no way of reacting to this event, since everything is handled by Putty and nothing is written to stdout or stderr. Also, the program hangs until the user clicks a button when such an error occures, thus requiring user interaction to even get the program to exit. I'm not sure wether Putty even gives you a meaningfull exitcode after the user closes the window, which you can use after the execution of the program.

So, all you could do is hope for the connection to be successfull. But you get no reaction on when the connection has been established. The best shot you have is doing a Thread.Sleep() for a while, then writing into stdin. I'm not sure wether you can control what Putty shows in the GUI through stdin (it doesn't seem to show up according to your information) or stdout.

Overall, starting a Putty process to establish an ssh connection is the wrong approach. You should look at external libraries such as https://sshnet.codeplex.com/ or the C# WinSCP library and the Session.ExecuteCommand() method. It should be as easy as shown in the code here. Using C# libraries, you also have a much, much better control over handling connection errors, timeout errors, command execution errors and reacting on command outputs.

Community
  • 1
  • 1
Maximilian Gerhardt
  • 5,188
  • 3
  • 28
  • 61
  • thanks for your answer. I took the code for getting a file from [here](http://www.dart.com/sftp-get-code-example.aspx). But unfurtunatlly I receive now the error "The name 'sftp1' does not exist in the current context". I don't know if I need to add a refrence or what is missing. I apprecaite any hint Thanks alot – etienne84 Mar 27 '16 at 11:21
  • `sftp1` is a special GUI `Control`, you have to pull it from your toolbox into your GUI. (Like a `SaveFileDialog`). You must have added a reference to that library's .dll first in order to see that SFTP element in your toolbox ofcourse. For further questions, open a new question. – Maximilian Gerhardt Mar 27 '16 at 16:58