【问题标题】:Handle multiple Spawn process in expect script在期望脚本中处理多个 Spawn 进程
【发布时间】:2017-03-15 21:06:04
【问题描述】:

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

我想通过 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

【问题讨论】:

    标签: bash sed expect spawn


    【解决方案1】:

    你真的不需要多个(意思是平行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)\r"
                exp_continue
            }
            eof { wait } ; # at this time the last spawn'ed process has exited
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2021-01-13
      相关资源
      最近更新 更多