问题描述
我正在尝试使用SSH.NET通过SSH发送一个简单的命令.即使是最简单的测试代码也会失败.这是一个片段.
I'm trying to send a simple command over SSH using SSH.NET. Even the simplest test code fails. Here is a snippet.
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
添加到命令中,但没有结果.我在做什么错了?
The AT
command should echo back OK but doesn't. Instead I receive a custom timeout message issued by the SSH target. I see the device name in the timeout message which corresponds to the prompt it uses and from that i can conclude that the login works (also tested with various SSH programs) but the command itself is not executed. I tried adding \n
and \r\n
to the commands but to no results. What am I doing wrong?
结果输出为 \ r \ n命令行界面\ r \ nDeviceName>自定义空闲超时
,我认为Visual Studio会将行尾转换为Windows.
Edit 1:output from result is \r\nCommand Line Interface\r\nDeviceName> Custom idle timeout
I think the line endings are converted to Windows ones by Visual Studio.
使用Plink plink.exe username @ ip -pw密码"AT">log.txt
的输出结果与Visual Studio相同.Plink等待直到超时并终止,并且 log.txt
包含 \ r \ nCommand Line Interface \ r \ nDeviceName>.自定义空闲超时
.
Edit 2:Using Plink plink.exe username@ip -pw password "AT" > log.txt
results in the same output as Visual Studio. Plink waits till timeout and terminates and log.txt
contains \r\nCommand Line Interface\r\nDeviceName> Custom idle timeout
.
我看到腻子
Using username "username".
username@host's password:
Entering character mode
Escape character is '^]'.
Command Line Interface
DeviceName>
在开始输入命令之前先写
.可能是在主机准备好接收命令之前输入了该命令,结果该命令挂起直到出现某种反应?
is written before you can start entering commands. Might it be that the command is entered before the host is ready to receive it and as a result the command hangs until some reaction comes?
推荐答案
您的服务器显然不支持SSH"exec".SSH.NET SshClient.RunCommand
方法和PLink的 plink.exe命令
语法后面使用的通道.
Your server obviously does not support the SSH "exec" channel that is used behind the SSH.NET SshClient.RunCommand
method and PLink's plink.exe command
syntax.
或者实际上它确实支持它,但是不正确.似乎启动了一个交互式会话(一个shell),而不是执行命令.
Or actually it does support it, but incorrectly. It seems to start an interactive session (a shell), instead of executing the command.
要确认此假设,请尝试使用PLink:
To confirm this assumption, try this with PLink:
echo AT| plink username@ip -pw password > log.txt
如果可行,则可能需要使用 SshClient.CreateShell
(SSH"shell"通道")而不是 SshClient.RunCommand
.虽然这通常是错误的方法,但是由于服务器损坏,您需要采用这种方法.
If that works, you may need to use the SshClient.CreateShell
(SSH "shell" channel") instead of the SshClient.RunCommand
. While this is generally a wrong approach, you need to take this approach due to your broken server.
Python/Paramiko的类似问题:
在设备上使用Paramiko exec_command执行命令不起作用 .
这篇关于SSH.NET未在设备上执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!