Running Command on Multiple Servers at Once

Home / Running Command on Multiple Servers at Once

Running Command on Multiple Servers at Once

December 9, 2015 | Article | No Comments

Maintaining multiple servers is not an easy job. There are cases where you want to run the same command(s) on all the servers. For example, you may want to install/upgrade packages, patch the kernel, update configuration, etc. It would be tedious if you have to log in to each server and run the same routine manually.

In this article, we will discuss a way to log in to some server and run the same command on many different machines at once.

In this article, I use:

  1. Slackware64 14.0 as client machine
  2. Ubuntu 12.10 as client machine
  3. Fedora 17 as client machine
  4. ClusterSSH

ClusterSSH

The administrative tool we have is ClusterSSH. It provides a special console interface where anything you type into the console is automatically sent to as many hosts as you want.

You might visit the official ClusterSSH site for more information.

Installation

Package Manager Way

To install ClusterSSH on Ubuntu, Debian or Linux Mint you can use following command:

sudo apt-get install clusterssh

To install ClusterSSH on CentOS or RHEL, first you need to set up EPEL repository, and then run the following:

sudo yum install clusterssh

Generic Way

Installing ClusterSSH from source is really simple. We will cover how to install ClusterSSH in generic way so we can apply it to various OS.

Clone the source code.

git clone git://github.com/duncs/clusterssh

Or you can download the source code from sourceforge: here

I will focus on the source code cloned from github.

Go to the source code root directory.

First, build the ClusterSSH. ClusterSSH use a perl-based script to configure itself.

perl Build.PL

At some points, ClusterSSH need dependencies that might not installed on your system. To install the dependencies, do following:

./Build installdeps

Once the dependencies are fulfilled, next invoke following commands:

./Build
./Build test
./Build install

Configure ClusterSSH

Once installation finished, the first step is to define a cluster of hosts that you want to run commands on. To do that, create a system-wide ClusterSSH configuration as /etc/clusters and write following:

clusters = my_cluster my_cluster2
my_cluster = host1 host2 host3 host4
my_cluster2 = host5 host6

A cluster is a group of hosts which you want to log in to, and run commands on. Here we specify two clusters: my_cluster and my_cluster2 with respective machine there. Of course the host1 is the host in hostname or IP address format.

If you want a user-specific ClusterSSH configuration, simply use ~/.csshrc instead of /etc/clusters.

When you launch ClusterSSH with any user-defined cluster, it will use ssh to log in to individual hosts in the cluster, and run any user-typed commands on the hosts.

Launch ClusterSSH

To launch ClusterSSH, run cssh command as follows:

cssh -l userid my_cluster

Where “userid” is a login ID for all the hosts in the cluster, and “my_cluster” is the cluster name.

If you want, you can specify individual hostnames instead of the cluster name.

cssh -l userid host1 host2 host3

Once cssh command is executed, it will pop up XTerm windows for individual hosts, as well as a small window labeled “CSSH [2]“, which is ClusterSSH console window. Whatever you type in the console window will simultaneously appear in the XTerm windows of individual hosts. Essentially, you control all XTerm windows via the single console window.

If you want to run some commands to a specific XTerm window, you can simply switch focus to the Xterm window, and type the commands as you usually would.

The following screenshot shows ClusterSSH in action, where there are five hosts in the cluster, and the console window in the upper left corner is where you are supposed to type the commands to run on all five hosts.

Troubleshoot

This is the troubleshoot which come to me, so I won’t cover all possible troubleshoot. You can conform on ClusterSSH page for more information.

No X11-Protocol Perl Module

If you have following error message when execute cssh:

Can't locate X11/Protocol.pm in @INC (@INC contains: /usr/local/bin/../lib/perl5 /usr/local/bin/../lib /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/local/share/perl5/App/ClusterSSH.pm line 29.
BEGIN failed--compilation aborted at /usr/local/share/perl5/App/ClusterSSH.pm line 29.
Compilation failed in require at /usr/local/bin/cssh line 11.
BEGIN failed--compilation aborted at /usr/local/bin/cssh line 11.

Then you possibly don’t install X11-Protocol perl module.

Download the packages on http://search.cpan.org/CPAN/authors/id/S/SM/SMCCAM/X11-Protocol-0.56.tar.gz

You can follow guide on how to install perl modules by:

tar -xzvf X11-Protocol-0.56.tar.gz
cd X11-Protocol-0.56
perl Makefile.PL
make
make test
make install

, , ,

About Author

about author

xathrya

A man who is obsessed to low level technology.

Leave a Reply

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

Social media & sharing icons powered by UltimatelySocial