Providing input/subcommands to a command (cli) executed with SSH.NET SshClient.RunCommand

一世执手 提交于 2019-12-04 01:46:56

问题


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?


回答1:


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("cli subcommand");

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.

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.

You have to open a shell session (what is otherwise a not recommended approach for automating a command execution).

Use SshClient.CreateShellStream or SshClient.CreateShell and send the commands to its input:

"cli\n" + "cli subcommand\n"

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.



来源:https://stackoverflow.com/questions/57666090/providing-input-subcommands-to-a-command-cli-executed-with-ssh-net-sshclient-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!