Run a shell script on all 15 servers at the same time?

We have 15 servers. Hostnames for these 15 servers are stored in a text files and loop through each server to connect to the remote server and run a command, but this loop process runs the command one after another. However, the requirement is to run the same command on all 15 servers at the same time. We are connecting the remote servers using public/private key files.

textfile="/home/laknar/hostfile"

while read r
do
	ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no laknar@$r 'run a shell script'

done < $textfile

Any idea would be great.

putting the whole 'ssh' in the background (&)?

That depends heavily on how you define "same time". If in above you run the remote command in background, you'll achieve a start of all scripts within a few seconds, as each ssh returns immediately after submitting the respective script. If that is not acceptable, you might consider using a cron job on each server, having synchronized the servers' clocks meticulously. But this would still carry a few seconds risk...

textfile="/home/laknar/hostfile"

while read r
do
	ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no laknar@$r 'myscript.ksh &'

done < $textfile

is this correct?

Why not giving it a shot and reporting back the results?

I'd suggest not, but more of a concern, how do you handle a server not being available for some reason?

Robin

Hi All,

Any one can tell me how to avoid passphrase or is there any way to login to multiple servers and run one command with out looking for passphrase

Thanks,

Do you mean avoiding the SSH passphrase when you open the connection? You would need to create a key-pair that does not have one. You then have to carefully consider if anything or anyone else could get at the private key, because without a passphrase, it could be used more easily.

Can you secure the account that will be performing this work?

Robin

1 Like

You should prevent ssh from reading the while loop's input.
Either by redirecting ssh's stdin

  < /dev/null ssh ...

or by a -n option

  ssh -n ...

If the backgrounding works, it is safer to add a little sleep to the loop

while ...
do
   command &
   sleep 1
done

After enter passphrase , i am able to do ssha directly to the host but not from the script..

Hi.

For the parallel connection to numerous servers, we use:

pdsh - Efficient rsh-like utility, for using hosts in parallel

with the ssh (not rsh) option after the keys have been distributed to the servers.

There may be other even better solutions, but pdsh has worked well for us. It seems to be on most Linux variants.

Best wishes ... cheers, drl