在expect脚本中处理多个Spawn进程

时间:2017-03-15 21:06:05

标签: bash sed expect spawn

这是我的期望脚本的用例(我拥有的少数几个)

我想在ssh上运行多个sed命令。它就像预构建环境设置一样。 我想运行这样的东西: -

#!/usr/bin/expect
set timeout -1

spawn -noecho bash -c "ssh -t user@host 'sed -i <some_stuff1> <file1>'"
spawn -noecho bash -c "ssh -t user@host 'sed -i <some_stuff2> <file2>'"
spawn -noecho bash -c "ssh -t user@host 'sed -i <some_stuff3> <file3>'"

expect {
  -re ".*sword.*" {
     exp_send "$env(PASS_WORD)\n"
     exp_continue
  }
}

但只有最后sed命令才会执行。第1和第2将被跳过。

我错过的隐藏宝石是什么?

这是我到目前为止所看到的但没有帮助的

Handle multiple statement in expect script

Expect script: How to handle two processes?

Handling multiple process simuntaneously - safari online book

1 个答案:

答案 0 :(得分:0)

你真的不需要多个(意思是 parallel spawn

#!/usr/bin/expect

set timeout 60

set cmds [list "ssh host1 ..." "ssh host2 ..." "ssh host3 ..."]

foreach cmd $cmds {
    spawn -noecho bash -c $cmd
    expect {
        -nocase "password" {
            exp_send "$env(PASS_WORD)\"
            exp_continue
        }
        eof { wait } ; # at this time the last spawn'ed process has exited
    }
}