goe
goe

Reputation: 337

Execute certain commands one after other two all set of servers simultaneously

I need to execute command 1 and command 2 on 6 servers simultaneously and after that I need to run command 3 and command 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" &

Is there any better way of doing it, as I have around 10 servers and 10 commands each.

Please help an easy and a robosut way.

Upvotes: 1

Views: 142

Answers (1)

glenn jackman
glenn jackman

Reputation: 247210

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

Upvotes: 1

Related Questions