从C#SSH.NET的ShellStream获取完整的命令输出

T4m*_*mer 3 c# ssh ssh.net

使用Renci.SshNet库。我正在尝试执行一些命令。执行“命令1”后,我将执行“命令2”,这需要更多时间。

我只得到输出的第一行。(reader.ReadToEnd()无法正常工作)。

我也while (!reader.EndOfStream){ }没有运气尝试过。

我认为这是因为服务器响应的延迟。如果没有响应,则流什么也不会读取并结束。

我找到了解决方案

String tmp;
TimeSpan timeout = new TimeSpan(0, 0, 3);
while ((tmp = s.ReadLine()) != null)
{
    Console.WriteLine(tmp);
}
Run Code Online (Sandbox Code Playgroud)

但这不是专业的。我需要某种方式在流结束时结束流。

using (var vclient = new SshClient("host", "username", "password"))
{
    vclient.Connect();
    using (ShellStream shell = vclient.CreateShellStream("dumb", 80, 24, 800, 600, 1024))
    {
        Console.WriteLine(SendCommand("comand 1", shell));
        Console.WriteLine(SendCommand("comand 2", shell));
        shell.Close();
    }
    vclient.Disconnect();
}

public static string SendCommand(string cmd, ShellStream sh)
{
    StreamReader reader = null;
    try
    {
        reader = new StreamReader(sh);
        StreamWriter writer = new StreamWriter(sh);
        writer.AutoFlush = true;
        writer.WriteLine(cmd);
        while (sh.Length == 0)
        {
            Thread.Sleep(500);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("exception: " + ex.ToString());
    }
    return reader.ReadToEnd();
}               
Run Code Online (Sandbox Code Playgroud)

Mar*_*ryl 5

外壳是无尽的流。没有发送命令-接收输出序列。在ReadToEnd不能知道的一个命令结束的输出。您所要做的就是阅读,直到您自己可以知道输出结束为止。如果您不能告诉您,可以通过添加某种输出结束标记来帮助自己,例如:

command 1 ; echo this-is-the-end-of-the-output
Run Code Online (Sandbox Code Playgroud)

并阅读,直到你得到"this-is-the-end-of-the-output"线。


通常,“外壳”通道不是自动化的理想解决方案。它旨在用于交互式会话。

您最好通过使用“ exec”通道SshClient.CreateCommand。与CreateCommand一旦命令完成的信道关闭。因此,显而易见的是“流程的尽头”,这ReadToEnd()是您按预期工作的原因。SSH.NET甚至还提供了完整的命令输出SshCommand.Result(内部使用ReadToEnd())。