期待用独立屏幕编写脚本

时间:2015-03-24 17:10:35

标签: linux scripting expect

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

我发现期望脚本对自动化过程很有用,而屏幕命令对于创建许多窗口并同时与它们进行交互非常有用。

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

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

当我尝试在deatached模式下运行屏幕时,它没有工作......

我做了:

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

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

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

作为底线,我需要在deatached不可见屏幕上使用expect。

提前致谢。

1 个答案:

答案 0 :(得分:2)

对于面临同样问题的其他人,我设法解决了这个问题,我认为问题是:期望无法与“它看不到的东西”互动,即分离屏幕。

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

脚本变成这样:

#!/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 "\01d"    # detach from the screen (ctrl-a + d)

interact     # cause the screen not to close.

希望这很有用。

相关问题