同时执行其他两个服务器之后的某些命令

时间:2018-03-31 20:33:55

标签: python linux bash

我需要同时在6台服务器上执行命令1和命令2,之后我需要运行命令3和命令4。

#!/bin/sh
#Instance details
Server1="<Server1>"
Server2="<Server2>"
Server3="<Server3>"
Server4="<Server4>"
Server5="<Server5>"
Server6="<Server6>"
ssh root@$Server1 "command1" &
ssh root@$Server1 "command2" &
ssh root@$Server2 "command1" &
ssh root@$Server2 "command2" &
ssh root@$Server3 "command1" &
ssh root@$Server3 "command2" &
ssh root@$Server4 "command1" &
ssh root@$Server4 "command2" &
ssh root@$Server5 "command1" &
ssh root@$Server5 "command2" &
ssh root@$Server6 "command1" &
ssh root@$Server6 "command2" &
wait
ssh root@$Server1 "command3" &
ssh root@$Server1 "command4" &
ssh root@$Server2 "command3" &
ssh root@$Server2 "command4" &
ssh root@$Server3 "command3" &
ssh root@$Server3 "command4" &
ssh root@$Server4 "command3" &
ssh root@$Server4 "command4" &
ssh root@$Server5 "command3" &
ssh root@$Server5 "command4" &
ssh root@$Server6 "command3" &
ssh root@$Server6 "command4" &

有没有更好的方法,因为我有大约10个服务器和10个命令。

请帮助一个简单和robosut方式。

1 个答案:

答案 0 :(得分:1)

#!/bin/bash
cmds=( 
    'command1; command2'
    'command3; command4'
)
servers=( Server1 Server2 Server3 Server4 Server5 Server6 )

for c in "${cmds[@]}"; do
    for s in "${servers[@]}"; do 
        ssh root@"$s" "$c" &
    done
    wait
done