期望使用分离屏幕编写脚本

expect scripting with detached screen

我有一个脚本需要与 2 个不同的主机(客户端和服务器)交互,登录到它们并同时对它们执行一些不同的命令。

我发现 expect 脚本对于自动化过程很有用,而 screen 命令对于创建许多 windows 并同时与它们交互很有用。

我使用了这个脚本,似乎一切正常:

#!/usr/bin/expect -f

spawn screen -S server ssh root@194.116.5.80

set timeout 30
expect "assword" { send "toor\r" }
expect -re "(\$ |# )"
send "iperf3 -s\r"
interact

当我尝试 运行 屏幕处于分离模式时它不起作用...

我做到了:

spawn screen -d -m -S server ssh root@194.116.5.80

但它向我显示错误:spawn id exp6 未打开。

我该怎么做才能解决这个问题?

作为底线,我需要将 expect 与分离的不可见屏幕一起使用。

提前致谢。

只是为了其他面临同样问题的人,我设法解决了它,我认为问题是:expect 无法与 "what it doesn't see" 交互,即与分离的屏幕交互。

所以我设法先做我想做的一切,然后从屏幕上分离,在另一个屏幕上执行另一项工作。

脚本变成了这样:

#!/usr/bin/expect -f

set timeout 30

spawn screen -S server ssh <USER_NAME>@<IPADDRESS>

expect "assword" { send "<YOUR_PASSWORD>\r" }

expect -re "(\$ |# )"

send "<YOUR_COMMAND>\r"

send "d"    # detach from the screen (ctrl-a + d)

interact     # cause the screen not to close.

希望有用。