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

标签 linux scripting expect

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

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

我使用了这个脚本,看起来一切正常:

#!/usr/bin/expect -f

spawn screen -S server ssh <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dba9b4b4af9beae2eff5eaeaedf5eef5e3eb" rel="noreferrer noopener nofollow">[email protected]</a>

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

当我尝试在分离模式下运行屏幕时,它不起作用......

我做到了:

spawn screen -d -m -S server ssh <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef9d80809bafded6dbc1deded9c1dac1d7df" rel="noreferrer noopener nofollow">[email protected]</a>

但它显示了一个错误:spawn id exp6 not open。

我该如何解决这个问题?

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

提前致谢。

最佳答案

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

所以我设法首先完成我想做的所有事情,然后脱离屏幕以在另一个屏幕上执行另一项工作。

脚本变成这样:

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

希望这有用。

关于linux - 期望使用分离屏幕编写脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29238834/

相关文章:

linux - 访问以单行而不是多行显示的变量

linux - Sun Grid Engine 暂停而不是重新启 Action 业

mysql - PowerShell MySQL 转储脚本未正确转储

shell - 如何让expect -c在单行而不是脚本中工作

linux - 如何在恢复实例时删除丢失的 dbf

linux - G-WAN 和 Amazon EC2 错误

Java/Groovy - 字符串 : replace characters on matched regex

shell - KornShell bool 条件逻辑

java - 期待冒险

linux - 两个命令或命令管道命令 - Spawn Expect