Saturday, January 31, 2009

Save time managing multiple systems with Parallel SSH

OpenSSH is perhaps one of the most powerful and versatile tools available to any Linux user. It allows you to securely connect to a remote system via a shell or encrypted FTP and also allows you to copy files securely to and from remote systems.

For a user caring for multiple systems, OpenSSH is extremely useful, but being able to execute OpenSSH commands in parallel is even more so. This is where Parallel SSH, or pssh, comes in. Pssh provides parallel versions of the OpenSSH tools, meaning you can execute commands on various hosts in parallel, copy files in parallel, and so forth. Pssh is essentially a frontend to OpenSSH written in Python. It includes pssh, pscp, and prsync, as well as pslurp (the opposite of pscp in that it downloads rather than uploads) and pnuke (a frontend to the kill command).

Using pssh is extremely easy. There are no manpages, but calling the command with no arguments will bring up the help, which describes each option.

Every command uses a plaintext “hosts” file that is a simple text file containing the hosts to act upon, one per line. As an example, assume you wanted to make sure that the time on each server was identical. This could be done using the date command, but to do it with regular SSH, you would have to execute the command at the same time on each host using screen or multiple terminals. With pssh, this is one simple command:

$ pssh -h hosts -P date

hades: Wed Nov 12 10:21:11 MST 2008

hades: [1] 10:21:11 [SUCCESS] hades 22

odin: Wed Nov 12 10:21:11 MST 2008

odin: [2] 10:21:11 [SUCCESS] odin 22

$ cat hosts

hades

odin

Contrast that to using ssh directly:

$ for host in hades odin; do ssh ${host} "date"; done

Wed Nov 12 10:24:02 MST 2008

Wed Nov 12 10:24:02 MST 2008

Not much difference, other than the one command is easier and executes faster. Calling the date command itself won’t take up much time, but imagine for a moment that you were performing a database dump or some other time-intensive task. Using ssh directly in a serial mode, if each dump took one hour, would cost you two hours of ssh execution on the host. In parallel, it would only take one hour. If there are 10 systems to execute the command on…well, you get the idea.

You can use pscp to copy files to the hosts:

$ pscp -h hosts example.txt /tmp/example.txt

Or you can use pslurp to copy files from the hosts:

$ pslurp -h hosts -L ~/tmp /tmp/example.txt example.txt

The above will download /tmp/example.txt as example.txt, and will store the file in ~/tmp/[host]/, where [host] is the corresponding hostname to that found in your hosts file.

Finally, prsync will also upload to the hosts using rsync rather than scp.

The Parallel SSH tools can be great time savers if you work with multiple systems and need to be able to copy files to or from these servers and/or execute similar commands on each host. This can be great for updating servers with the latest security fixes, performing automated maintenance such as database backups and retrieval of dump files, and so forth.