5

I am trying to execute action in multiple servers in my loop but I would like to launch my actions in the first server and go directly to the second one without waiting for the first one to complite .

HOSTS="host1 host2"
    For hostname in ${HOST} ;
do ssh -tt ${USERNAME}@${hostname} << EOF
actions
exit
EOF
Done 

Anyone has an idea hos to do that in linux sh bash ?

Thank you in advance.

steve
  • 21,582
  • 5
  • 48
  • 75
Issam
  • 103
  • 1
  • 5
  • I see that if I need to launch command in background i need to put the "&" in the and of my command. If I have multiple command I need to do it for all ? or can I do it after "EOF &" – Issam Oct 27 '16 at 10:10
  • @user236012 lmgtfy.com links [are not allowed](http://meta.stackexchange.com/q/15650/203101) here since they are condescending and unhelpful. On top of which, yours was completely useless since it searches for something unrelated to what the OP wants to do. – terdon Oct 27 '16 at 13:20

2 Answers2

1

pdsh would appear to meet your needs. Runs ssh connections in parallel, in a multi threaded manner.

Example:

$ pdsh -w 192.168.1.4,192.168.1.250 uname -r
192.168.1.4: 2.6.32-431.17.1.el6.x86_64
192.168.1.250: 2.6.32-431.11.2.el6.x86_64
$

Parallel shell with pdsh

How can I send single file to multiple remote sites at the same time?

steve
  • 21,582
  • 5
  • 48
  • 75
  • I don't have the pdsh in our machine. – Issam Oct 27 '16 at 13:54
  • Do you know other alternative . – Issam Oct 27 '16 at 13:58
  • Multithreaded code in Python, if you have that installed, is fairly easy to write. You could write your own Python script which essentially does what pdsh does. – steve Oct 27 '16 at 14:03
  • I have Python installed , but I never used it , I will try it to change a little, but I will be grateful if you can specify which command I need to use with Python or if you have an example. Thanks in advance. – Issam Oct 27 '16 at 15:12
  • I see there is a lot of module that we can import and use but is there any solution based on the default Python modules ? – Issam Oct 27 '16 at 15:24
  • try https://gist.github.com/boopathi/4046754 for starters – steve Oct 27 '16 at 15:33
1

Use GNU Parallel:

parallel -S host1,host2 --nonall uname -a

If the task is more complex, make a function:

do_actions() {
   action
   packed
   function
}
export -f do_actions
HOSTS="host1,host2"
parallel -S $HOSTS --ssh 'ssh -l '$USERNAME --nonall --env do_actions do_actions

If the username is the same as whoami then this is enough:

parallel -S $HOSTS --nonall --env do_actions do_actions

GNU Parallel does not have to be installed globally: If it is not install you can do a personal installation.

wget pi.dk/3
bash 3
Ole Tange
  • 33,591
  • 31
  • 102
  • 198