Execute one SSH command on multiple hosts with pssh

As I am working on Java benchmarks on a DL980 on the top of VMware and RHEL, I needed to execute the same script on 32 different virtual machines. I could have written an bash script to do so, but I have found a very nice suite of tools that does the job (and more) called parallel-ssh.

This Google Summer of Code project written in Python allows to execute a SSH command, to copy files, to rsync directories and to kill processes (among others) on multiple hosts

The install

First download the .tar.gz document
# wget http://parallel-ssh.googlecode.com/files/pssh-2.2.2.tar.gz

untar it
# tar -xzvf pssh-2.2.2.tar.gz
# cd pssh-2.2.2

To install (you need Python, it worked on RHEL 5.3):
# python setup.py install

The configuration

Then to execute parallel commands, create a file with all IP/hostnames you
need (one per line) like this
# cat hosts.txt
192.168.10.1
192.168.10.2

Create the directories “stderr” and “stdout”
# mkdir /tmp/stderr
# mkdir /tmp/stdout

and that’s it !

How to use it ?

If you want to execute the command hostname ?
# pssh -A -e errors -o /tmp/stdout -e /tmp/stderr -h hosts.txt -l root hostname

-A will ask the root password inline
-o will put the standard output in one file per host in the directory /tmp/stdout
-e will put the standard errors in one file per host in the directory /tmp/stderr
-l is the user that will execute the command

For my benchmark I nedded to change the starting script run.sh, so I edited it on one virtual machine and then used pscp to copy it on all the nodes in the directory /root/java/
# pscp -A -h hosts.txt -l root /root/java/run.sh /root/java/

And finally I executed the benchmark on my 32 nodes 🙂
# pssh -A -e errors -o results -h hosts.txt -l root /root/java/run.sh

Have you forgotten one JVM garbage collection parameter ? No worries, pnuke will make sure that all java processes are stopped on all servers
# pnuke -A -h hosts.txt -l root java

Leave a Reply

Your email address will not be published. Required fields are marked *