2

I wish to know how to read the output from putty.exe console window.

There are plenty of answers about how to get the output (and how to input) for plink.exe/cmd.exe, yet none of them work for putty.exe.

FYI this is what i have now; changing the string 'putty.exe' to 'cmd.exe' make things work:

            var process = new Process();
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.FileName = "putty.exe";
            process.OutputDataReceived +=
                (s, e) =>
                {
                    Console.WriteLine(e.Data);
                    switch (e.Data)
                    {
                        case "test1":
                            {
                                process.StandardInput.WriteLine("echo test2");
                            }
                            break;

                        case "test2":
                            {
                                process.StandardInput.WriteLine("exit");
                            }
                            break;
                    }
                };

            if (process.Start())
            {
                process.BeginErrorReadLine();
                process.BeginOutputReadLine();
                process.StandardInput.WriteLine("echo test1");
                process.WaitForExit();
            }

Edited: The purpose to the piece of code is to automatically handle the login process: after username/password are provided, enter 'sudo' and input corresponding auditing messages. I need a way to capture the output of putty.exe so that I could input texts to this terminal.

ender
  • 480
  • 6
  • 23

1 Answers1

2

The entire point of plink.exe is to give a command-line interface to PuTTY. That is what you're supposed to use.

hometoast
  • 11,522
  • 5
  • 41
  • 58
  • putty seems more powerful to use interactively as we are not using it for automatic jobs. besides the team decides to use putty.exe but not plink.exe via cmd.exe – ender Aug 06 '13 at 02:56
  • So, are you automating or not? Perhaps if you add exactly what you're trying to accomplish, we could better help you. – hometoast Aug 06 '13 at 13:02
  • hi, basically i wish to automate the login and auditing process only; end user will then go to the terminal and do any further actions. – ender Aug 07 '13 at 01:56
  • 1
    Yeah, you're trying to fit a round AND square peg in a round hole. I'd suggest taking care of the automation using private keys for authentication and auditing on the server end. That, though, is the topic for another question. – hometoast Aug 07 '13 at 20:00
  • Hi Hometoast , Can you help me in automating the unix commands in C# using plink , i have tried plink and i was not able to proceed please refer my question in this link http://stackoverflow.com/questions/27645631/execute-unix-commands-using-putty-in-c-sharp – Mahi Dec 28 '14 at 16:20