为使用 SSH.NET SshClient.RunCommand 执行的命令 (cli) 提供输入/子命令 [英] Providing input/subcommands to a command (cli) executed with SSH.NET SshClient.RunCommand

查看:52
本文介绍了为使用 SSH.NET SshClient.RunCommand 执行的命令 (cli) 提供输入/子命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 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?

cli 是用于瞻博网络交换机/路由器的命令.

The cli is a command is used on Juniper switches/routers.

推荐答案

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

您必须提供cli 子命令";cli 命令的输入.但不幸的是,SSH.NET 不支持通过 SshClient.RunCommand/SshClient.CreateCommand 接口提供输入.请参阅允许写入 SshCommand.

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. See Allow writing to SshCommand.

有两种解决方案:

  • 使用服务器 shell 的适当语法在服务器上生成输入,例如:

  • 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).

    使用 SshClient.CreateShellStreamSshClient.CreateShell 并将命令发送到其输入:

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

      "cli
    " + "cli subcommand
    "
    

    有关示例代码,请参阅为使用 SSH.NET SshClient.CreateShellStream 执行的命令 (sudo/su) 提供子命令C# 通过 SSH.NET 发送 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) 提供输入/子命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆