问题描述
我使用Renci SSH.NET库创建了一个程序。它发送所有命令并正常读取结果。但是,当我发送以下命令时:
I created a program using Renci SSH.NET library. Its sending all the commands and reading the result normally. However, when I send the command below:
client.RunCommand("cli");
程序会无限期地挂在这行上。
The program hangs on this line indefinitely.
对正在发生的一切有何解释?
Any explanation of what is happening?
推荐答案
AFAIK, cli
是一种shell /交互程序。因此,我假设您已尝试执行以下操作:
AFAIK, cli
is a kind of a shell/interactive program. So I assume you have tried to do something like:
client.RunCommand("cli");
client.RunCommand("some cli subcommand");
那是错误的。 cli
将一直等待子命令并且永远不会退出,直到您使用相应的命令显式关闭它(例如 exit
)为止。并且在退出后,服务器将尝试执行 cli子命令
作为单独的顶级命令,也会失败。
That's wrong. cli
will keep waiting for subcommands and never exit, until you explicitly close it with a respective command (like exit
). And after it exits, the server will try to execute the cli subcommand
as a separate top-level command, failing too.
必须将 cli子命令提供给 cli
命令的输入。但是不幸的是,SSH.NET不支持提供 / 接口。
You have to feed the "cli subcommand" to the input of the cli
command. But SSH.NET unfortunately does not support providing an input with the SshClient.RunCommand
/SshClient.CreateCommand
interface.
有两种解决方案:
-
使用适当的语法服务器的外壳,以在服务器上生成输入,例如:
Use the appropriate syntax of the server's shell to generate the input on the server, like:
client.RunCommand("echo \"cli subcommand\" | cli");
或使用shell会话(否则不建议使用这种方法来自动执行命令)。
Or use a shell session (what is otherwise a not recommended approach for automating a command execution).
使用或并将命令发送到其输入: / p>
Use SshClient.CreateShellStream
or SshClient.CreateShell
and send the commands to its input:
"cli\n" + "cli subcommand\n"
有关示例代码,请参见或发送Ctrl + Y。
For a sample code see Providing subcommands to a command (sudo/su) executed with SSH.NET SshClient.CreateShellStream or C# send Ctrl+Y over SSH.NET.
这篇关于向使用SSH.NET SshClient.RunCommand执行的命令(cli)提供输入/子命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!