Using PSSH for executing parallel SSH commands
Tuesday, June 12th, 2012I recently needed to check the date across all of our servers, so instead of SSHing into all of them and executing a command manually, I learned about the tool pssh.
Installing pssh on Mac OS X
Installing pssh isn’t available through brew, because it is available through PyPi and it has dependencies on a homebrew built Python. Luckily it is easy to get PyPi running with easy_install.
[~]=> sudo easy_install pip ... (lots of output) [~]=> sudo pip install pssh
You now have installed pssh!
Using pssh!
First I create a file containing a list of servers:
[~]=> cat servers backend@server1.dk backend@server2.dk backend@server3.dk backend@server4.dk backend@server5.dk backend@server6.dk backend@server7.dk
And for running the command on the servers:
[~]=> pssh -h servers "date" [1] 13:33:00 [SUCCESS] backend@server1.dk [2] 13:33:00 [SUCCESS] backend@server2.dk [3] 13:33:00 [SUCCESS] backend@server3.dk [4] 13:33:00 [SUCCESS] backend@server4.dk [5] 13:33:00 [SUCCESS] backend@server5.dk [6] 13:33:00 [SUCCESS] backend@server6.dk [7] 13:33:00 [SUCCESS] backend@server7.dk
But to see the output of the server you need to use the -i argument:
[~]=> pssh -h benjamin-servers -i "date" [1] 13:34:11 [SUCCESS] backend@server1.dk Tue Jun 12 13:34:11 CEST 2012 [2] 13:34:11 [SUCCESS] backend@server2.dk Tue Jun 12 13:34:11 CEST 2012 [3] 13:34:11 [SUCCESS] backend@server3.dk Tue Jun 12 13:34:11 CEST 2012 [4] 13:34:11 [SUCCESS] backend@server4.dk Tue Jun 12 13:34:11 CEST 2012 [5] 13:34:11 [SUCCESS] backend@server5.dk Tue Jun 12 13:34:11 CEST 2012 [6] 13:34:11 [SUCCESS] backend@server6.dk Tue Jun 12 13:34:11 CEST 2012 [7] 13:34:11 [SUCCESS] backend@server7.dk Tue Jun 12 13:34:11 CEST 2012
If you are unfortunate enough to run a couple of servers, without having a centralized keystore, or manage the server with puppet, you can add a public key to multiple servers with pssh as well:
[~]=> pssh -h benjamin-servers -i “echo ‘ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDGcYH4Cg+PWnYFMUMFXDOAiC+hoN/N3frBstJC5+lMQoq5gJQ4nEb0zzS+5SyLaqwbD5SqByOAY/KynRYtrYLpyJwzRUKD7VoyL5wkahkeMeJvKs7b5tmom970mDMtRgyEuePUaVj0y4basdphmEbRjLwvuQSgNwQdf3g5pVoLXntk0VY4xtE9nkSftaB5EJkoSmzOJT/PwivGwMDNq31/yP4nkYZIUjOpOe2hF3aeFJCL+3xvpkI3tYVRCzqJZHLssiwQM5EK3HiOi4wWuvmOc1LJA5FATe04hyMCUopkwdZO2dGQVpI38IVrD1yKg6okwiq2C+B4HuKsbZ8seAeG5D+i9wh this@is.a.key’ >> /usr/home/backend/.ssh/authorized_keys”