SSH.NET 没有在设备上执行命令

SSH.NET is not executing command on device

我正在尝试使用 SSH.NET 通过 SSH 发送一个简单的命令。即使是最简单的测试代码也会失败。这是一个片段。

using (var ssh = new SshClient("ip", port, "username", "password"))
{
    ssh.Connect();
    while (true)
    {
        var result = ssh.RunCommand("AT").Execute(); 
        Console.Out.WriteLine(result);
    }
}

AT 命令应该返回 OK 但没有。相反,我收到了一条由 SSH 目标发出的自定义超时消息。我在超时消息中看到设备名称,它对应于它使用的提示,从中我可以得出结论,登录有效(也使用各种 SSH 程序进行了测试),但命令本身没有执行。我尝试在命令中添加 \n\r\n 但没有结果。我做错了什么?

编辑 1: 结果的输出是 \r\nCommand Line Interface\r\nDeviceName> Custom idle timeout 我认为行尾由 Visual Studio.

转换为 Windows

编辑 2: 使用 Plink plink.exe username@ip -pw password "AT" > log.txt 会产生与 Visual Studio 相同的输出。 Plink 等待超时并终止并且 log.txt 包含 \r\nCommand Line Interface\r\nDeviceName> Custom idle timeout

使用 PuTTY 我看到

Using username "username".
username@host's password:

Entering character mode
Escape character is '^]'.

Command Line Interface
DeviceName> 

在您可以开始输入命令之前写入。可能是在主机准备好接收命令之前输入命令,结果命令挂起,直到出现一些反应?

您的服务器显然不支持 SSH.NET SshClient.RunCommand 方法和 PLink 的 plink.exe command 语法后面使用的 SSH“exec”通道。

或者它确实支持它,但不正确。它似乎启动了一个交互式会话(shell),而不是执行命令。

为了证实这个假设,用 PLink 试试这个:

echo AT| plink username@ip -pw password > log.txt

如果可行,您可能需要使用 SshClient.CreateShell(SSH“shell”通道)并将命令写入其输入流,而不是使用 SshClient.RunCommand . 虽然这通常是 ,但由于服务器损坏,您需要采用这种方法。


Python/Paramiko的类似问题:
.