【问题标题】:Chain together commands with SSH on ProCurve在 ProCurve 上使用 SSH 将命令链接在一起
【发布时间】:2012-06-18 12:54:55
【问题描述】:

我需要备份 HP ProCurve 的配置,并且需要通过 SSH 来完成。

ProCurve shell 是交互式的,我不能这样写:

$ ssh user@ip 'command1; command2'

...因为 shell 给了我一个错误——可能是因为机器实际上没有类似 Unix 的 shell。我需要连接,输入密码,执行两条命令。

【问题讨论】:

    标签: shell ssh interactive


    【解决方案1】:

    我不确定我是否完全理解您的问题。您是否尝试运行多个命令? 你试过sh -c 吗?

    ssh user@ip sh -c "command1; command2"
    

    来自man sh:

           -c               Read commands from the command_string operand instead of from the standard input.  Special parameter 0 will be set from the command_name operand
                            and the positional parameters ($1, $2, etc.)  set from the remaining argument operands.
    

    【讨论】:

      【解决方案2】:

      使用 Procurve/Cisco 设备,您不能简单地运行 ssh hostname command。您需要使用一些可以与交互式 ssh 会话一起使用的工具,即 expect、pexpect 或类似的东西。另一种解决方案是使用socat:

      (sleep 5; echo ${PASSWORD}; sleep 2; echo ; echo command1; sleep 2) \
      | socat - EXEC:"ssh ${SWITCH}",setsid,pty,ctty
      

      【讨论】:

        猜你喜欢
        • 2022-10-17
        • 1970-01-01
        • 1970-01-01
        • 2022-06-06
        • 1970-01-01
        • 1970-01-01
        • 2014-11-18
        • 2011-01-13
        • 1970-01-01
        相关资源
        最近更新 更多