gpt4 book ai didi

c# - 使用 SSH.NET 在命令输出期间发送输入

转载 作者:行者123 更新时间:2023-12-04 17:48:16 31 4
gpt4 key购买 nike

使用 PuTTY,我连接到 SSH 服务器,然后执行一个不断输出日志(每秒多行)的命令。在此过程中,我可以发送一些击键,它们是命令,即。输入 123Esc 停止进程。如果我输入有效的击键,我可以在输出设备上看到它,所以我总是知道是否发送了击键。

我想以编程方式执行此操作 - 连接到 SSH 服务器,执行主要命令,然后将一些击键作为命令发送给它,同时监视日志,我可以在日志中看到对我的命令的响应。

所以我想用 SSH.NET 来完成它 - 它是一个简洁的 SSH 工具,它输出可靠并且非常好用。但是我有一个问题 - 我可以连接到这个服务器,执行命令,但是当它转储日志时我无法向它发送任何击键。

我尝试使用 CreateShellStream 的方法:

  • shellStream.WriteLine('ascii 中的击键或只是字符串')
  • 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。

关于c# - 使用 SSH.NET 在命令输出期间发送输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47263654/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com