Handle multiple Spawn process in expect script

坚强是说给别人听的谎言 提交于 2019-12-04 06:07:10

问题


Here is my use case for expect script ( one of few i have)

I want to run multiple sed command over ssh. Its like pre-build environment setup. I want to run something like this :-

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

But only last sed command will execute. 1st and 2nd will be skipped.

What could be the hidden gem i am missing?

Here is what i looked at so far but not helpful

Handle multiple statement in expect script

Expect script: How to handle two processes?

Handling multiple process simuntaneously - safari online book


回答1:


You don't really need multiple (means parallel) spawn here.

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


来源:https://stackoverflow.com/questions/42820661/handle-multiple-spawn-process-in-expect-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!