Linux: Execute commands in parallel without parallel-ssh or Ansible
I’ve decided to write this bite-sized article after reading Linux: Execute commands in parallel with parallel-ssh which I highly recommend, but I was confronted with a particular use case in which I didn’t have access to the internet and I faced constraints that prevented me from installing new packages.
Without much at hand, I’ve defined a server list, and scripted the following approach:
The script initiates multiple SSH connections and uses wait
and &
operator approach to achieve a rudimentary concurrency, and the usage is pretty straightforward:
./pssh.sh "hostname -A"
Also if you fancy a sequential one-liner, define hosts_file
that contains the servers, replace user
and run:
while read -u10 line;do ssh user@$line 'hostname -A';done 10<hosts_file
Or if you prefer to pass a script to the one-liner, just replace script.sh
with the desired script.
while read -u10 line;do echo $line;ssh user@$line 'bash -s' < script.sh;done 10< servers.txt