【问题标题】:expect scripting with detached screen期望带有分离屏幕的脚本
【发布时间】:2015-05-28 03:07:41
【问题描述】:

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

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

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

#!/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 not open。

我能做些什么来解决这个问题?

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

提前致谢。

【问题讨论】:

    标签: linux scripting expect


    【解决方案1】:

    对于面临同样问题的其他人,我设法解决了它,我认为问题在于: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.
    

    希望有用。

    【讨论】:

      猜你喜欢
      • 2014-11-13
      • 2014-01-16
      • 2017-01-22
      • 1970-01-01
      • 2023-03-19
      • 2016-04-13
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多