Home > Tools > pssh – Running command in parallel on multiple servers

pssh – Running command in parallel on multiple servers

Print Friendly, PDF & Email

When the number of server you are administrating is getting higher, you need to find a way to get the job done easier. This is when “pssh” come in handy. In this article, I will demonstrate by example, the utilization of the “pssh” tools. I will on purpose, not cover the “prsync” and the “pnuke” command, in an effort to keep this article interesting and not to lenghtly.

To begin with, we need to automate your ssh login session to your servers from one management server. This is require before using “pssh” to it full extend. If you do not know how to automate ssh login there is an excellent article available at “The Geek Stuff” site, that explain how to do it.

First, let’s install the package

# rpm -ivh pssh-1.2.2-1.i386.rpm
Preparing...                ########################################### [100%]
   1:pssh                   ########################################### [100%]
#

After installing the package, there are several parallel commands that are available to us. We have the ‘pssh’ that allow us to run the same command on multiple servers, the “pscp” to copy file(s) to multiple servers and finally “pslurp” used to get directories or files from multiples host and copy them to the local server.

Normally, when the command included in the “pssh” package are run, the output from the remote hosts is not shown to us. The -P option, displays the output from both remote hosts as well as the exit status. The -h option specify the file containing the name of your servers that you want the command to be executed. In our example, this file only contain the name of two servers, so that the output is shortened. On each line, we have the name of the server, follow by ‘:’ , the ssh port used and finally the name of the user you wish to use when connecting to the servers.

# cat /sysadmin/cfg/dns_servers.txt
dns_server2.home.ca:22 root
dns_server1.home.ca:22 root

# pssh -h /sysadmin/cfg/dns_servers.txt "date"    (Output is hidden)
dns_server2.home.ca: Success on dns_server2.home.ca:22
dns_server1.home.ca: Success on dns_server1.home.ca:22

# pssh -h /sysadmin/cfg/dns_servers.txt -P "date"  (Output is displayed)
dns_server2.home.ca: Wed May  6 16:28:57 EDT 2009
dns_server2.home.ca: Success on dns_server2.home.ca:22
dns_server1.home.ca: Wed May  6 16:28:57 EDT 2009
dns_server1.home.ca: Success on dns_server1.home.ca:22

If you do not want to specify the file containing your servers list, there is an environment variable named ‘PSSH_HOSTS‘ that you can declare and ‘pssh’ will use it by default. You will notice that since the command is issued in parallel, the output does not necessary comes back in the same order that you have specify in the host file. This is normal, some servers may be more occupied than other and may take more time to run the command you specified.

We can use the -o option, the specify directory path used to save the output from each remote host. Each remote host output is output to a seperate file named  with the remote machine’s host name. The -t option can be use to specify the maximum time a command have to execute on the remote server. It defaults to 60 seconds.

# pssh -P -o /tmp/pssh "date"
dns_server2.home.ca: Tue May 12 10:12:30 EDT 2009
dns_server2.home.ca: dns_server2.home.ca: Success on dns_server2.home.ca:22
dns_server1.home.ca: Tue May 12 10:12:30 EDT 2009
dns_server1.home.ca: dns_server1.home.ca: Success on dns_server1.home.ca:22

# ls -l /tmp/pssh
total 8
-rw-r--r-- 1 root root 29 May 12 10:07 dns_server1.home.ca
-rw-r--r-- 1 root root 29 May 12 10:07 dns_server2.home.ca

# cat /tmp/pssh/dns_server1.home.ca
Tue May 12 10:12:30 EDT 2009
# cat /tmp/pssh/dns_server2.home.ca
Tue May 12 10:12:30 EDT 2009
#

In the next example, we will show an example using the “pscp” command to copy a directory and all its sub directories to servers listed in our /sysadmin/cfg/dns_servers.txtfile.  In /tmp, I created a directory named “coco”  with two files in it and a sub-directory  named “coco1” with one file in it.  We then issue the “pscp” command and to verify that it worked, we list the “coco” directory on one of the server were we copied the directory content.

# cd /tmp
(/tmp)# mkdir coco
(/tmp)# date > coco/date1.txt
(/tmp)# date > coco/date2.txt
(/tmp)# mkdir  coco/coco1
(/tmp)# date > coco/coco1/date3.txt

(/tmp)# ls -lR coco
coco:
total 12
drwxr-xr-x 2 root root 4096 May 12 10:34 coco1
-rw-r--r-- 1 root root   29 May 12 10:33 date1.txt
-rw-r--r-- 1 root root   29 May 12 10:33 date2.txt
coco/coco1:
total 4
-rw-r--r-- 1 root root 29 May 12 10:34 date3.txt
(/tmp)#

(/tmp)# pscp -r coco /tmp
Success on dns_server2.home.ca:22
Success on dns_server1.home.ca:22

(/tmp)# ssh dns_server1.home.ca "ls -lR /tmp/coco"
/tmp/coco:
total 12
drwxr-xr-x    2 root     root         4096 May 12 10:51 coco1
-rw-r--r--    1 root     root           29 May 12 10:52 date1.txt
-rw-r--r--    1 root     root           29 May 12 10:52 date2.txt
/tmp/coco/coco1:
total 4
-rw-r--r--    1 root     root           29 May 12 10:52 date3.txt
(/tmp)#

The “pslurp” command grabs a file or directory of all the remote servers and copies it to the local machine. The example below grabs the “coco” directory from both dns_server1.home.ca and dns_server2.home.ca and copy them into “/tmp/outdir” on our local server. The -r option is shorthand for --recursive.  For each remote host, a new directory is created with the name of the host, and inside that directory a copy of copy is made using the local directory name supplied as the last argument to pslurp.

(/tmp)# mkdir /tmp/outdir
(/tmp)# pslurp -L /tmp/outdir -r /tmp/coco coco
Success on dns_server1.home.ca:22
Success on dns_server2.home.ca:22
(/tmp)# 

(/tmp)# cd /tmp/outdir
(/tmp/outdir)# ls -lR
total 8
drwxr-xr-x 3 root root 4096 May 12 11:08 dns_server1.home.ca
drwxr-xr-x 3 root root 4096 May 12 11:08 dns_server2.home.ca
./dns_server1.home.ca:
total 4
drwxr-xr-x 3 root root 4096 May 12 11:08 coco
./dns_server1.home.ca/coco:
total 12
drwxr-xr-x 2 root root 4096 May 12 11:08 coco1
-rw-r--r-- 1 root root   29 May 12 11:08 date1.txt
-rw-r--r-- 1 root root   29 May 12 11:08 date2.txt
./dns_server1.home.ca/coco/coco1:
total 4
-rw-r--r-- 1 root root 29 May 12 11:08 date3.txt
./dns_server2.home.ca:
total 4
drwxr-xr-x 3 root root 4096 May 12 11:08 coco
./dns_server2.home.ca/coco:
total 12
drwxr-xr-x 2 root root 4096 May 12 11:08 coco1
-rw-r--r-- 1 root root   29 May 12 11:08 date1.txt
-rw-r--r-- 1 root root   29 May 12 11:08 date2.txt
./dns_server2.home.ca/coco/coco1:
total 4
-rw-r--r-- 1 root root 29 May 12 11:08 date3.txt
(/tmp/outdir)#

I sincerely hope that with these examples, will give you a good overview and a good feel of power of  the pssh package. So have fun !

There is a problem with some requirement of the latest version if you are running with Redhat/Centos 4 5 or Fedora.  I suggest using the version 1.2.2-1 instead of the latest 1.4.3.1 version

Download the version 1.2.2.1 (pssh-1.2.2-1.i386.rpm)  or the latest version (pssh-1.4.3-1.noarch.rpm)
Visit the pssh website for more information.

Categories: Tools