【问题标题】:simulate enter keypress after a command is executed执行命令后模拟回车键
【发布时间】:2019-08-07 09:11:59
【问题描述】:

我正在对主机进行 SSH 并执行命令,该命令要求您按 Enter 键。 (它要求两次不同的东西。)

我在这里使用 spawn expect。

当您发送报告命令时,它会要求您按 ENTER 键。完成后,它再次要求您按 Enter。我想自动发送回车键。

#!/usr/bin/expect
spawn ssh user@host report
expect "Press ENTER to continue, or CTRL-C to quit."
send " \r"
expect "Press enter for inputing"
send "\r"

ENTER 应该自动完成并获得命令的最终结果。

【问题讨论】:

  • 这里有问题吗?
  • 第一个send中的\r之前有一个空格:send "<SPC>\r"

标签: linux shell expect spawn


【解决方案1】:

假设您在机器“audrey”上有“报告”脚本:

#!/bin/bash
echo -n "Press ENTER to continue, or CTRL-C to quit."
read
echo -n "Press enter for inputing"
read
read s
echo "You sent: $s"

和本地期望脚本:

#!/usr/bin/expect
spawn ssh audrey ./report
expect "Press ENTER to continue, or CTRL-C to quit."
send "\n"
expect "Press enter for inputing"
send "\n"
send "OK\n"                                                                           
expect "You sent: OK"
close

./a.expect 输出:

spawn ssh audrey ./report
Press ENTER to continue, or CTRL-C to quit.
Press enter for inputing
OK
You sent: OK

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    • 2013-11-18
    • 2018-04-12
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 2019-04-04
    相关资源
    最近更新 更多