【问题标题】:How to open a bash/ssh session in shell script and run commands in the remote session?如何在 shell 脚本中打开 bash/ssh 会话并在远程会话中运行命令?
【发布时间】:2013-06-07 09:31:11
【问题描述】:

我尝试在 shell 脚本中执行以下操作:

bash;

在 bash 上下文中:

  • 运行./a.out

./a.out 上下文中我需要模拟keystorkes:

yes
3292
no

我该怎么做?我所有的尝试都失败了,因为 &&&; 在主 shell 上下文中而不是在 bash 中执行后续命令。

bash && echo "yes" > /dev/console

我已经看到use expect in shell script 使用本机 shell 命令没有其他选择吗?我不想依赖其他工具。

【问题讨论】:

    标签: linux bash shell ssh


    【解决方案1】:

    查看expect,它在用户提供的脚本的帮助下与交互式程序“对话”。

    用法

    expect ./interact
    

    或使interact 可执行(chmod a+x interact):

    ./interact
    

    interact 是以下脚本:

    #!/usr/bin/expect
    spawn ./a.out
    send -- "yes\r"
    expect "3292\r"
    send -- "no\r"
    

    这只是一个简单的示例,手册页中充满了深入的解释,并且还有安装附带的示例脚本。

    参考文献

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 2021-03-01
      • 1970-01-01
      • 2015-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多