捕获执行plink的cmd的所有(stdout,stderr和CON)输出

问题描述:

大家好,



这是我第一次发布编码问题..我对以下情况感到疯狂(在互联网上搜索了几天) :

我想通过打开一个进程并运行plink来打开来自c#的SSH连接。应收集所有输出,并根据结果,程序将触发对ssh的操作。

我的大问题是,如果使用不同的脚本我需要用户交互。因此我必须捕获所有输出数据(标准输出,标准错误和控制台)。



查看以下测试批次应该使案例更清楚:

1:@ECHO OFF

2:ECHO StdOut

3:ECHO StdErr 1>& 2

4:ECHO缺点> CON



我能够捕获2 + 3行,但不能捕获4行(某些程序使用)。

我也有尝试使用powershell而不是cmd.exe作为起点,但结果相同。



c#中是否有任何方法可以捕获控制台,或者你知道任何第三个派对命令行能够将CON重定向到标准输出或类似的东西吗?



任何帮助都非常感谢。非常感谢!

Michael

Hi all,

its my first time posting a coding question.. I am getting crazy with the following case (searching the internet for a couple of days):
I want to open SSH connections from c# via opening a process and running plink. All output should be collected and depending on the results the program will fire actions to the ssh.
My big problem is, that i am using a couple if different scripts and i need user interaction. therefore i have to capture ALL output data (standard output, standard error AND CONSOLE).

Looking at the following test batch should make the case more clear:
1: @ECHO OFF
2: ECHO StdOut
3: ECHO StdErr 1>&2
4: ECHO Cons>CON

I am able to capture lines 2+3, but not 4 (used by some programs).
I have also tried powershell instead of cmd.exe as starting point, but same result.

Is there any way in c# to capture the console out as well or do you know any third party command line being able to redirect CON out to stdout or something like this?

any help is really appreciated. Thanks a lot!
Michael

您可以使用 System.Diagnostics.Process.Start 带有重定向输出流 StandardOutput StandardError ,请参阅http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx [ ^ ]。



您可以在此处找到带重定向的代码示例: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx [ ^ ]。



-SA
You can use System.Diagnostics.Process.Start with re-directed output streams StandardOutput and StandardError, please see http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^].

You can find a code sample with redirection here: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^].

—SA






感谢您的建议。不幸的是,我对我的解释并不准确:我使用的是一个带有StandardOutput和StandardError异步重定向的进程,但我不知道如何捕获其他输出发送到cmd进程。



谢谢!
Hi,

thanks for the suggestion. Unfortunately I was not precise with my explanation: I am using a Process with asynchronous redirection of StandardOutput and StandardError, but I have no idea how to capture the other output send to the cmd process.

thanks!


I've encountered the same issue. You can capture ALL of plink's command-line output by executing it through a windows shell script. eg.

Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("cmd /c C:\tools\plink -load "& WScript.Arguments(0) &" "& WScript.Arguments(1) )

Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadLine()
WScript.StdOut.WriteLine strText
Loop

Then call the windows shell script via cscript. eg.

cscript C:\\tools\\executePlink.vbs argument1 argument2