【问题标题】:SSH.NET is not executing command on deviceSSH.NET 未在设备上执行命令
【发布时间】:2016-10-12 06:58:42
【问题描述】:

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

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.txtcontains \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> 

是在您开始输入命令之前编写的。可能是在主机准备好接收命令之前输入命令,导致命令挂起,直到有反应出现?

【问题讨论】:

    标签: c# ssh ssh.net


    【解决方案1】:

    您的服务器显然不支持在 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。虽然这通常是wrong approach,由于您的服务器损坏,您需要采用这种方法。


    关于 Python/Paramiko 的类似问题:
    Executing command using Paramiko exec_command on device is not working

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      • 2019-01-09
      • 1970-01-01
      相关资源
      最近更新 更多