4

I want to execute a process on multiple servers. I am using the following wrapper script. The script moves to servers one at a time. I would like to change it so it can execute on multiple servers. Is there way to do it?

#!/bin/bash
for ip in $(<ALL_SERVERS_IP); do
    # Tell the remote server to start bash, but since its
    # standard input is not a TTY it will start bash in
    # noninteractive mode.
    ssh -q "$ip" bash <<-'EOF'
        printf "%s\n" ================== "$(hostname) ::: Current date : $(date)" \
                            ==================
        ./remote 
    EOF
done
countermode
  • 7,533
  • 5
  • 31
  • 58
user67186
  • 497
  • There are a bunch of options. This blog lists 10: http://blog.crdlo.com/2010/07/parallel-ssh-tool-roundup.html – depquid Aug 21 '14 at 17:26
  • related: http://unix.stackexchange.com/questions/83571/changing-password-in-multiple-boxes-using-script/83583#83583 & http://unix.stackexchange.com/questions/84936/how-to-add-line-in-a-etc-sudoers-file-with-the-help-of-shell-script-50servers/84943#84943 – slm Aug 21 '14 at 17:38
  • These are related too: http://unix.stackexchange.com/questions/19008/automatically-run-commands-over-ssh-on-many-servers/19009#19009 & http://unix.stackexchange.com/questions/3239/how-can-i-reproduce-commands-run-on-one-machine-on-another-machine – slm Aug 21 '14 at 17:42

3 Answers3

2

Yes. Try pdsh, which will execute a command on multiple remote hosts simultaneously. Your task could be written like so:

pdsh -w ssh:$(echo $(<ALL_SERVERS) | sed -e 's/ /,/g') 'printf "%s\n" ...'
dg99
  • 2,642
2

Sounds like you're looking for GNU Parallel. That's pretty much exactly what it was written for. In your case, you would do:

parallel ./remote --sshloginfile ALL_SERVERS_IP

The above assumes that ALL_SERVERS_IP is a text file with one IP or hostname per line. If they can have different logins, make the list like so:

user@server.example.com
foo@server2.example.com
bar@server3.example.com
terdon
  • 242,166
  • I checked on the central server and couldn't find parallel on it. – user67186 Aug 21 '14 at 17:53
  • @user67186 you only need to install it on your machine, the one you will launch the job from. – terdon Aug 21 '14 at 17:55
  • Use a bash function to give the 'printf/hostname' stuff to run. Then use --env to export that function to the remote server: parallel --env myfunc --slf ALL_SERVERS_IP --nonall myfunc – Ole Tange Aug 22 '14 at 07:08
  • @terdon How do you have to install parallel? Can't find anything useful. – Jenson Jan 27 '16 at 07:34
  • @Jenson see the link in my answer. Most Linux distributions (probably all) will have it in their repositories as parallel. For others, the link provides download packages. – terdon Jan 27 '16 at 10:32
0

I know some tools for that, but the easiest is Fabric. Take a look and tell me what do you think.