执行命令后模拟Enter键

时间:2019-08-07 09:11:59

标签: linux shell expect spawn

我正在对主机执行SSH并执行命令,并且该命令要求您按Enter键。 (它两次询问不同的内容。)

我在这里使用生成期望。

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

{
  "plugins": [
-   ["@babel/plugin-transform-runtime"],
+   ["@babel/plugin-transform-runtime", {
+     "corejs": 2,
+   }],
  ]
}

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

1 个答案:

答案 0 :(得分: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。期望输出:

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