Originally published March 21, 2017 @ 11:18 am

This is just a quick note for me on pushing SSH keys using sshpass and xargs. The ~/hosts.txt file contains a list of remote hosts, one per line. And the ~/.passwd.txt contains your password. Make sure to chmod 400 ~/.passwd.txt and delete it after you’re done, to maintain some semblance of security. Another option is to read -s SSHPASS; export SSHPASS and then use sshpass -e to work with the environment variable. I don’t know if this is more secure, but it’s more of a hassle, so it must be.

And here’s the command. Pretty simple:

xargs -n 1 -P 10 -a ~/hosts.txt -I % sshpass -f ~/.passwd.txt ssh-copy-id '-o ConnectTimeout=5 -o StrictHostKeyChecking=no %' 2>/dev/null

You can adjust the number of parallel threads -P based on the number of CPU cores you have. Also, when running many parallel threads, you may want to up the ConnectionTimeout value.