I have been trying to parallelize the following script, specifically the for loop. How can I do that?
#!/bin/bash
for i in `cat /root/vms`;
do
/usr/bin/sshpass -p 'test' /usr/bin/ssh -o StrictHostKeyChecking=no \
-l testuser $i -t 'echo test | sudo -S yum update -y'
done
Replace
with
You can do it quite succinctly with GNU Parallel like this:
So, if your
/root/vms
contains:and you add the
--dry-run
option to see what it would do, without actually doing anything:Sample Output
Rather than repeating all your
ssh
options, consider putting them into a file at$HOME/.ssh/config
like this:GNU Parallel has a
--nonall
option and an environment variable to set thessh
-command to use:If the things you want to run is a bit more complex, you can make a function and have GNU Parallel transfer that: