使用 Renci.SshNet 库。我正在尝试执行一些命令。在执行 "command 1" 后,我执行需要更多时间的 "command 2"。
我只得到输出的第一行。(
我还尝试了
我认为这是由于服务器响应的延迟造成的。当没有响应时,流不会读取任何内容并结束。
我找到了一个解决方案。
我只得到输出的第一行。(
reader.ReadToEnd()
没有正常工作)。我还尝试了
while (!reader.EndOfStream){ }
,但没有成功。我认为这是由于服务器响应的延迟造成的。当没有响应时,流不会读取任何内容并结束。
我找到了一个解决方案。
String tmp;
TimeSpan timeout = new TimeSpan(0, 0, 3);
while ((tmp = s.ReadLine()) != null)
{
Console.WriteLine(tmp);
}
但这不是专业的。我需要一种当流结束时就结束的方法。
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();
}