c# - 从 plink 获取重定向输出

标签 c# .net

我目前正在尝试编写一个函数来运行 plink 并捕获该进程的输出。当我尝试 StandardOutput 时,我什么也没得到,而当我尝试 StandardError 时,我得到“无法从标准输入读取:句柄无效。”我只能假设这意味着我在 plink 等待输入时尝试阅读。我还通过名为 apCommands.txt 的文本文件传递我的命令。当我手动运行此命令时,它会工作并为我提供所需的输出,但我不明白为什么我不能重定向该输出以便将其存储在结果文件中。这是我现在拥有的代码。

        string pArgs = "-m \"" + ABSPATH + "\\apCommands.txt\" -ssh myHost -l user -pw password";
        string output;

        if (File.Exists(ABSPATH + "\\apTest.txt"))
            File.Delete(ABSPATH + "\\apTest.txt");
        StreamWriter apTest = new StreamWriter(ABSPATH + "\\apTest.txt");

        Process runAP = new Process();
        ProcessStartInfo apInfoStart = new ProcessStartInfo(ABSPATH + "\\plink.exe");
        apInfoStart.Arguments = pArgs;
        apInfoStart.RedirectStandardOutput = true;
        apInfoStart.CreateNoWindow = true;
        apInfoStart.UseShellExecute = false;
        runAP.StartInfo = apInfoStart;
        runAP.Start();

        output = runAP.StandardOutput.ReadToEnd();
        runAP.WaitForExit();
        apTest.WriteLine("Standard Output");
        apTest.WriteLine(output);
        apTest.Close();
        runAP.Close();

谢谢,

最佳答案

要解决此错误,请使用 apInfoStart.RefirectStandardInput = true;

另请参阅:https://social.msdn.microsoft.com/Forums/vstudio/en-US/697324fa-6ce6-4324-971a-61f6eec075be/redirecting-output-from-plink

你可以对 StandardOutput 和 StandardError 做同样的事情

关于c# - 从 plink 获取重定向输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28386394/

相关文章:

c# - 嵌套的 using 语句 - 哪一个不会被处理掉

.net - Unity 2.0 通过 XML 注册泛型类型

C# 6 命名空间作为来自强类型命名空间引用的字符串

c# - 使用 ASP .NET MVC Identity 应用程序对外部 API (PostgreSQL) 中的用户进行身份验证

c# - 在 C# 中通过 id 在数据库中查找记录的最快方法是什么?

c# - MVVM 事件处理程序属于哪里

.net - Visual Studio CLR 项目

c# - 使用父级的 DataContext(WPF - 动态菜单命令绑定(bind))

c# - 等待来自 PowerShell 的异步 C# 方法

c# - 表中的枚举类值