执行命令后模拟回车键

simulate enter keypress after a command is executed

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

我这里用的是spawn expect。

当您发送报告命令时,它会要求您按 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 应该自动完成并获得命令的最终结果。

假设您在 "audrey" 机器上有 "report" 脚本:

#!/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