Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# &引用;StandardOut未被重定向或流程尚未';“还没有开始”;在C中读取控制台命令输出时#_C#_Ssh_Plink - Fatal编程技术网

C# &引用;StandardOut未被重定向或流程尚未';“还没有开始”;在C中读取控制台命令输出时#

C# &引用;StandardOut未被重定向或流程尚未';“还没有开始”;在C中读取控制台命令输出时#,c#,ssh,plink,C#,Ssh,Plink,感谢@user2526830的代码。基于该代码,我在程序中添加了几行代码,因为我想读取SSH命令的输出。下面是我的代码,它在行给出了一个错误,而 StandardOut尚未重定向或该过程尚未启动 我想要实现的是将ls的输出读入字符串 ProcessStartInfo startinfo = new ProcessStartInfo(); startinfo.FileName = @"f:\plink.exe"; startinfo.Arguments = "-ssh abc@x.x.x.x -p

感谢@user2526830的代码。基于该代码,我在程序中添加了几行代码,因为我想读取SSH命令的输出。下面是我的代码,它在
行给出了一个错误,而

StandardOut尚未重定向或该过程尚未启动

我想要实现的是将ls的输出读入字符串

ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = @"f:\plink.exe";
startinfo.Arguments = "-ssh abc@x.x.x.x -pw abc123";
Process process = new Process();
process.StartInfo = startinfo;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.Start();
process.StandardInput.WriteLine("ls -ltr /opt/*.tmp");
process.StandardInput.WriteLine("exit");

process.StartInfo.RedirectStandardOutput = true;

while (!process.StandardOutput.EndOfStream)
{
    string line = process.StandardOutput.ReadLine();
}

process.WaitForExit();
Console.ReadKey();

在启动进程之前,请尝试设置标准输出重定向

process.StartInfo.RedirectStandardOutput = true;
process.Start();

当您尝试读取输出时,进程可能已经终止(由于您的“exit”命令)。尝试下面稍微修改的版本,我将while循环移动到“ls”命令之后,但在“exit”命令之前

它应该可以很好地读取“ls”命令的输出,但不幸的是,它很可能会在某个时候挂起,因为您永远无法在标准输出上获得EndOfStream。当没有更多的内容可读取时,ReadLine将阻塞,直到可以读取另一行

因此,除非您知道如何检测命令生成的输出的最后一行,并在读取后中断循环,否则您可能需要使用单独的线程进行读取或写入

ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = @"f:\plink.exe";
startinfo.Arguments = "-ssh abc@x.x.x.x -pw abc123";
Process process = new Process();
process.StartInfo = startinfo;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.StandardInput.WriteLine("ls -ltr /opt/*.tmp");

while (!process.StandardOutput.EndOfStream)
{
    string line = process.StandardOutput.ReadLine();
}

process.StandardInput.WriteLine("exit");
process.WaitForExit();
Console.ReadKey();