[英]Sending input during command output with SSH.NET
使用 PuTTY,我连接到 SSH 服务器,然后执行一个不断输出日志的命令(每秒多行)。 在此过程中,我可以发送一些作为命令的击键,即。 把123
或Esc停止进程。 如果我输入了一个有效的击键,我可以在输出设备上看到它,所以我总是知道击键是否被发送。
我想以编程方式进行 - 连接到 SSH 服务器,执行主命令,然后将一些按键作为命令发送给它,同时监视日志,在那里我可以看到对我的命令的响应。
所以我想用 SSH.NET 来做这件事——它是一个简洁的 SSH 工具,它输出可靠,而且很好用。 但是我有一个问题 - 我可以连接到这个服务器,执行一个命令,但是当它转储日志时我无法向它发送任何击键。
我尝试使用CreateShellStream
:
shellStream.WriteLine('keystroke in ascii or just string')
sshClient.CreateCommand('keystroke in ascii or just string')
然后sshClient.Execute()
;StreamWriter(shellstream)
并写入它我尝试过 ascii 代码、UTF-16 代码、字符串,但不幸的是没有任何效果。
用 Plink(PuTTY 命令行工具)做的同样的事情工作得很好,但它更麻烦,我更喜欢使用 SSH.NET。 你知道用 SSH.NET 和 Plink 发送击键有什么区别吗? 为什么它与 Plink 完美配合? 我试过用 Wireshark 调试它,不幸的是 SSH 很难做到这一点。
使用 Plink 我基本上使用的是这样的:
ProcessStartInfo psi = new ProcessStartInfo("plink.exe", arguments);
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.UseShellExecute = false;
plink = Process.Start(psi);
StreamWriter input = plink.StandardInput;
StreamReader output = plink.StandardOutput;
input.WriteLine("startCommand"); //Starts the process responsible for logs
input.Write("950"); //It correctly sends the keystrokes
// Some code regarding reading logs from output, some other commands sent to ssh server...
Console.WriteLine("Exiting...");
input.Write('\u001b'); //ESC in UTF16
我认为 SSH.NET 在发送任何其他内容之前等待最后一个命令结束,但我不知道如何更改它。
我尝试使用 SSH.NET:
using (var client = new SshClient(host, user, new PrivateKeyFile(key)))
{
int timoutCounter = 0;
do
{
timoutCounter++;
try { client.Connect(); }
catch (Exception e)
{
Console.WriteLine("Connection error: " + e.Message);
}
} while (!client.IsConnected);
ShellStream stream = client.CreateShellStream("shell", 200, 24, 1920, 1080, 4096);
// it starts the process on remote ssh server that
stream.WriteLine("startCommand");
Console.Write("Waiting for any key...");
Console.ReadKey();
for (int i = 0; i < 30; i++)
{
// Just some quick way for me to know that the device fully loaded
stream.Expect("LogKeyWord");
}
Console.WriteLine("Device started");
Console.ReadKey();
// Different ways of entering commands during log output that I've tried
stream.Write("5");
Console.Write("Waiting for any key...");
Console.ReadKey();
stream.WriteLine("6");
Console.Write("Waiting for any key...");
Console.ReadKey();
stream.Write("\u001b");
Console.Write("Waiting for any key...");
Console.ReadKey();
var test97 = new StreamWriter(stream);
test97.Write("7");
Console.Write("Waiting for any key...");
Console.ReadKey();
test97.WriteLine("8");
Console.Write("Waiting for any key...");
Console.ReadKey();
client.RunCommand("11");
Console.Write("Waiting for any key...");
Console.ReadKey();
var test = client.CreateCommand("31");
test.Execute();
Console.Write("Waiting for any key...");
Console.ReadKey();
client.Disconnect();
}
只是一个猜测,但我会尝试冲洗流。
更新:
写了一个小实验,我无法重现该问题。 看看这里: https : //github.com/scottstephens-repros/SSHNetInterleavedIORepro
因此,这可能是特定于您的代码、您尝试远程运行的程序或远程机器的操作系统、sshd 或配置的问题。 我的远程测试机是 CentOS 7.8,OpenSSH 7.4p1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.