Home > front end >  How to execute a shell command synchronously on multiple client machines?
How to execute a shell command synchronously on multiple client machines?

Time:05-27

I am trying to run a simple bash command(ex: ssh user@client1 date) on several remote machines. I can do this by writing a bash script like this(consider there is no password for client machines):

ssh user@client1 date
ssh user@client2 date
ssh user@client3 date
ssh user@client4 date
ssh user@client5 date
.
.
.
ssh user@clientn date

However, what this does is simply iterate through the given machines and execute the date command one by one, like a for loop.
What I am willing to achieve is, executing these commands at the exact same time.
The first solution that came into my mind is fixing the clock on every client to the same time, then using the at command and setting the execution on a specific time, addressing every client to run their respective commands on that same clock tick. This solution makes the above script, morphed into this(consider this script started at 9:30 am, and finished in less than 5 minutes.):

at 9:35 <<< "ssh user@client1 date"
at 9:35 <<< "ssh user@client2 date"
at 9:35 <<< "ssh user@client3 date"
at 9:35 <<< "ssh user@client4 date"
at 9:35 <<< "ssh user@client5 date"
.
.
.
at 9:35 <<< "ssh user@clientn date"

I don't think this is a good solution and I can't stop thinking that I am approaching this completely wrong or rather lacking knowledge somewhere in the network architecture.

Also, running multiple threads and creating n amount of bash instances is not acceptable as well, because the number n can go up to thousands.

CodePudding user response:

Consider using & to send ssh task in background, so you can launch all your ssh thru the loop like this :

ssh user@client1 date &
ssh user@client2 date &
ssh user@client3 date &
ssh user@client4 date &
ssh user@client5 date &

CodePudding user response:

A few thoughts spring to mind:

  • Subscribe all of the clients to a common MQTT topic with a broker such as mosquitto, then publish a command GO on that channel whenever you want them to start

  • As above, but subscribe and publish via Redis

  • Start all clients waiting for a UDP broadcast on your network, e.g. simply with nectat and then UDP broadcast a GO message, also with netcat.

  • Related