2

我想知道如何从 putty.exe 控制台窗口读取输出。

关于如何获取 plink.exe/cmd.exe 的输出(以及如何输入)的答案有很多,但它们都不适用于 putty.exe。

仅供参考,这就是我现在所拥有的;将字符串 'putty.exe' 更改为 'cmd.exe' 使事情起作用:

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

编辑:这段代码的目的是自动处理登录过程:提供用户名/密码后,输入“sudo”并输入相应的审计信息。我需要一种方法来捕获 putty.exe 的输出,以便我可以将文本输入到此终端。

4

1 回答 1

2

plink.exe 的全部意义在于为 PuTTY 提供命令行界面。那是你应该使用的。

于 2013-08-05T10:56:19.907 回答