Execute commands simultaneously on multiple servers Using PSSH/Cluster SSH/Multixterm

Sponsored Link
If you have multiple servers with similar or identical configurations (such as nodes in a cluster), it's often difficult to make sure the contents and configuration of those servers are identical. It's even more difficult when you need to make configuration modifications from the command line, knowing you'll have to execute the exact same command on a large number of systems .

In this tutorial we will see some tools to execute one command on multiple remote servers using ssh.First you need to make sure you have ssh installed in your machine or you can install using the following command

sudo aptitude install ssh

Now we are going to see the following tools which does the required job we are looking for

1) Pssh

2) cluster ssh

3) Multixterm

1) Pssh

pssh provides a number of commands for executing against a group of computers, using SSH. It's most useful for operating on clusters of homogenously-configured hosts.

The package contains:

-- Parallel ssh (parallel-ssh, upstream calls it pssh), executes commands on multiple hosts in parallel

-- Parallel scp (parallel-scp, upstream calls it pscp), copies files to multiple remote hosts in parallel

-- Parallel rsync (parallel-rsync, upstream calls it prsync), efficiently copies files to multiple hosts in parallel

-- Parallel nuke (parallel-nuke, upstream calls it pnuke), kills processes on multiple remote hosts in parallel

-- Parallel slurp (parallel-slurp, upstream calls it pslurp), copies files from multiple remote hosts to a central host in parallel

These tools are good for controlling large collections of nodes, where faster alternatives

Install pssh in Ubuntu

sudo aptitude install pssh

pssh Syntax

pssh command -h hosts-file options

-h --hosts   hosts file (each line "host[:port] [user]")
-l --user    username (OPTIONAL)
-p --par     max number of parallel threads (OPTIONAL)
-o --outdir  output directory for stdout files (OPTIONAL)
-t --timeout timeout in seconds to do ssh to a host (OPTIONAL)
-v --verbose turn on warning and diagnostic messages (OPTIONAL)
-O --options SSH options (OPTIONAL)

where the hosts-file contains a list of all the hosts that you want to have the command executed on.

pssh Examples

The following example runs hostname on three machines (IPs or hostnames) specified in the file ips.txt using login irb2 and saves the output in /tmp/foo.

sudo cat ips.txt
128.112.152.122
18.31.0.190
128.232.103.201

sudo pssh -h ips.txt -l irb2 -o /tmp/foo hostname

Success on 128.112.152.122:22
Success on 18.31.0.190:22
Success on 128.232.103.201:22

sudo ls /tmp/foo

128.112.152.122  128.232.103.201  18.31.0.190

sudo cat /tmp/foo/*

planetlab-1.cs.princeton.edu
planetlab1.xeno.cl.cam.ac.uk
planetlab1.lcs.mit.edu

By default, pssh uses at most 32 ssh processes in parallel to ssh to the various nodes. (This is somewhat important if you're controlling hundreds or thousands of machines.) By default, it also uses a timeout of one minute to ssh to a node and obtain a result. For ssh commands that take longer than this (e.g., sleep 61), the -t option can be used. Note that pssh and pnuke have a default timeout of one minute. pscp and prsync have no default timeout, but one can be specified using the -t option.

pscp

Here's an example of using pscp to copy files in parallel to a set of machines.

sudo  pscp -h ips.txt -l irb2 /etc/hosts /tmp/hosts

Success on 128.112.152.122:22
Success on 18.31.0.190:22
Success on 128.232.103.201:22

Using the -r option will perform a recursive copy for copying entire directories.

2) cluster ssh

administer multiple ssh or rsh shells simultaneouslyClusterSSH allows you to control multiple ssh or rsh sessions from a single input window. You can also configure clusters of machines for easy invocation and interact with individual terminal windows during a session.

Install Cluster ssh in Ubuntu

sudo aptitude install clusterssh

Configuration Files

This /etc/clusters file is cluster tag database. Contains a list of tags and hostnames, in the form

<tag> [<username>@]hostname [...]

Newlines and comments (delimited with a #) are ignored.

Cluster definitions can also be added to the $HOME/.csshrc file

This /etc/csshrc file is Global configuration file, sourced by all users. Can be generated by:

sudo cssh -u > /etc/csshrc

$HOME/.csshrc

Per user configuration file. Can be generated by:

cssh -u > /etc/csshrc </tt>

To add a cluster definition to this file, use the following format

cluster_tag1 = server1 server2 user@server3
cluster_tag2 = server4 server5
clusters = cluster_tag1 cluster_tag2

Be aware of using “reserved names” and no check is currently performed for them.

Using Cluster SSH

If you want to perform the same command on the three servers one, two, & three use the following command

sudo cssh one two three

This will open three consoles, one for each server, over an ssh connection, and one little console to type your command.

3) Multixterm

Expect is a program that talks to other interactive programs according to a script. Expect is useful for running any program which requires interaction between the program and the user.In Ubuntu multixterm included in expect package.

Install expect in Ubuntu

sudo aptitude install expect

Multixterm Syntax

multixterm -[ args ] "command arg1 arg2″ server1 server2...

Arguments

-xa -- The optional -xa argument indicates arguments to pass to xterm.

-xc -- The optional -xc argument indicates a command to be run in each named xterm (see -xn). With no -xc argument, the command is the current shell.

-xd -- The optional -xd argument indicates a directory to search for files that will appear in the Files menu. By default, the directory is: ~/lib/multixterm

-xf -- The optional -xf argument indicates a file to be read at startup. See FILES below for more info.

-xn -- The optional -xn argument indicates a name for each xterm. This name will also be substituted for any %n in the command argument (see -xc).

-xv -- The optional -xv flag puts multixterm into a verbose mode where it will describe some of the things it is doing internally. The verbose output is not intended to be understandable to anyone but the author.

Multixterm Examples

The following command line starts up two xterms using ssh to the hosts 192.168.2.100 and 192.168.2.101

sudo multixterm -xc "ssh root@%n" 192.168.2.100 192.168.2.101

-xc -- The optional -xc argument indicates a command to be run in each named xterm (see -xn). With no -xc argument, the command is the current shell.

ssh root@%n : ssh is a command to connect remote server with root user. This name will also be substituted for any %n in the command argument.

Sponsored Link

Incoming search terms:

Related posts

6 thoughts on “Execute commands simultaneously on multiple servers Using PSSH/Cluster SSH/Multixterm

  1. Nice post (as allways) you forget “dsh” the famous dancer’s shell, or distributed shell I used it on openmosix cluster very usefull ;)

    keep going

    max

    [Reply]

  2. Wow this is crazy. I’m a user of iTerm on Mac OS X and there’s an option to send keyboard input to all tabs. So I load 5 or 6 tabs and ssh in each tab to my target machine. I then send input to all tabs and whatever I type on one tab, goes to the others. I can use command+right to switch tabs quickly so I can make sure every machine is ‘on the same page’. This allows for interactive programs like vi and top and allows you to edit files on all the tabbed servers simultaneously. I’m looking for something similar in Linux. clusterit, dsh, multixterm, cssh, pssh all seem geared toward doing it all from the GUI and being blind to whats going on in the remote machines. What if one doesn’t respond quite like the others or gets stuck somewhere? I need a GUI solution like iTerm for Linux workstations unfortunately.

    [Reply]

    Stuart Reply:

    I use iTerm on Mac also.

    Try Terminator for Linux:
    http://www.tenshu.net/p/terminator.html

    [Reply]

  3. Great post. Appreciate if you can modify the commands, as pssh has been renamed to parallel-ssh and pscp has been renamed to parallel-scp.
    The rename has been done on ubuntu to avoid confusion with putty packages.

    [Reply]

Leave a comment

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