如何并行而不是顺序运行期望脚本?

如何并行而不是顺序运行期望脚本?
#!/bin/expect --

set vm_num [lindex $argv 0]

puts "``````````````````````````````````````````````````````````````"
puts "``````````````````````````````````````````````````````````````"
puts "Registering Stations on VM"
puts "Tarts to be Registered: $vm_num"
puts "``````````````````````````````````````````````````````````````"
puts "``````````````````````````````````````````````````````````````"

for { set index 0 } { $index < [llength $vm_num] } { incr index } {

puts "Registering Tart Num: [lindex $vm_num $index]"

switch -- [lindex $vm_num $index]\
1 {
        spawn telnet 10.171.0.10 6187
        set timeout -1
        expect {
                puts "``````````````````````````````````````````````````````````````"
                "*traffic*" { puts "Registering Group1, Group2"
                send "traffic map rate reg_group1 30000\r"
                send "traffic map rate reg_group2 30000\r"

                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                puts "Registering Group3, Group4"
                send "traffic map rate reg_group3 30000\r"
                send "traffic map rate reg_group4 30000\r"
                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                puts "Registering Group5, Group6"
                send "traffic map rate reg_group5 30000\r"
                send "traffic map rate reg_group6 30000\r"
                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                puts "Registering Group7, Group8"
                send "traffic map rate reg_group7 30000\r"
                send "traffic map rate reg_group8 30000\r"
                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                }


                puts "wait 3 seconds"
                sleep 3

                send -- "^]"
                expect -exact "^\]\rtelnet> "
                send -- "close\r"
                expect eof
                wait
        }

} 2 {
        spawn telnet 10.171.0.11 6187
        set timeout -1
        expect {
                "*traffic*" {
                puts "``````````````````````````````````````````````````````````````"
                puts "Registering Group1, Group2"
                send "traffic map rate reg_group1 30000\r"
                send "traffic map rate reg_group2 30000\r"

                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                puts "Registering Group3, Group4"
                send "traffic map rate reg_group3 30000\r"
                send "traffic map rate reg_group4 30000\r"
                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                puts "Registering Group5, Group6"
                send "traffic map rate reg_group5 30000\r"
                send "traffic map rate reg_group6 30000\r"
                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                puts "Registering Group7, Group8"
                send "traffic map rate reg_group7 30000\r"
                send "traffic map rate reg_group8 30000\r"
                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                }

                puts "wait 3 seconds"
                sleep 3


                send -- "^]"
                expect -exact "^\]\rtelnet> "
                send -- "close\r"
                expect eof
                wait
        }
} 3 {
        spawn telnet 10.171.0.12 6187
        set timeout -1
        expect {
                "*traffic*" {
                puts "``````````````````````````````````````````````````````````````"
                puts "Registering Group1, Group2"
                send "traffic map rate reg_group1 50000\r"
                puts "wait 220 seconds .."
                send "traffic go\r"
                sleep 220

                puts "Registering Group2"
                send "traffic map rate reg_group2 50000\r"
                puts "wait 220 seconds .."
                send "traffic go\r"
                sleep 220

                }

                puts "wait 3 seconds"
                sleep 3

                send -- "^]"
                expect -exact "^\]\rtelnet> "
                send -- "close\r"
                expect eof
                wait
        }
}
default {
puts "$vm_num is not a valid tart"
}
}

有人可以指导我吗?我有上面的期望脚本,当我们运行例如 ./vmSetup.ex "1 2 3" 的脚本时会执行该脚本,但这是按顺序执行的。我已经创建了这个 Expect 脚本,但现在我想并行运行脚本中的案例,即 1、2 和 3。有人可以告诉我如何并行运行例如 8 个案例吗?此外,我也使用类似的案例 1 到 3,唯一的区别是 IP。我身边就有25个这样的案例。有人还可以告诉我如何仅使用一个案例来容纳所有 25 个虚拟机吗?

答案1

最简单的方法是重写您的 except 脚本,以便它仅连接到机器,从命令行获取机器名称或 IP 地址。

然后使用以下任一方式运行该脚本GNU 并行或与xargs -P. xargs更容易用于更简单的并行化任务,parallel更灵活,更适合更复杂的并行化要求。

例如

echo 10.171.0.{10..17} | xargs -P 8 -n 1 /path/to/your/expect/script

这将同时运行最多 8 个作业 ( -P 8),每个作业恰好指定一个 ( -n 1) IP 地址。

如果您期望脚本产生大量输出,我建议编写脚本,以便它们不会打印到标准输出,而是打印到它们自己的单独日志文件(例如使用 IP 地址expect-10.171.0.10.log),否则所有作业的输出将混合在一起。

至少,每条输出行应该识别它来自哪台机器,例如,通过在每行前面加上 IP 地址和冒号作为前缀。


额外评论:

  1. 如果可能的话,您应该使用ssh而不是telnet,并将其配置为使用基于密钥的身份验证而不是密码身份验证,然后:

    • 你不需要expect
    • 你可以使用并行分布式ShellPDSH连接到远程计算机(pdsh 默认情况下并行连接)

    再仔细考虑一下您的expect脚本后,ssh 可能不合适。

  2. 或者,您似乎正在尝试从某些路由器收集数据?

    在这种情况下,简单网络管理协议可能更合适 - 并且已经有许多现有工具可以收集、存储和绘制通过 SNMP(和其他来源)收集的数据。例如,仙人掌,蟋蟀,穆宁以及其他使用的工具RRD工具

相关内容