DSH – Run/Execute Shell commands on multiple Linux servers at once.

We knows most of the IT infrastructure has been migrated to Linux and recent past the count is more since its more secure and less investment (Few distro comes with license and remaining are freeware) compared with other operating systems. Everyone knows about openSSH is one of the best tool to connect remote Linux machine securely to perform activity.

When you have a requirement to run the same command on multiple hosts, openSSH doesn’t work and you may need to write a script. Developing a script is not a small thing for administrator that to it’s not our scope to do but still we need a alternate solution to achieve it.

Suggested Read : PSSH – Execute Commands on Multiple Linux Servers in Parallel

I did some google search and found the utility called DSH, which is working natively as I expect.

DSH stands for Dancer’s Shell or Distributed Shell, It allows users to run shell commands on multiple Linux servers at once.

A while ago we wrote about PSSH is a another utility which is used for same purpose but both has their own unique features and working in different style.

Install DSH in Linux through Package Manager

All the major Linux distributions has included the DSH package in default repository, we can easily install DSH with help of distribution package manager.

For Debian : Debian based users can easily install via APT package manager or APT-GET package manager.

$ sudo apt-get install dsh

Enable yaourt or packer in order to install DSH to Arch Linux based systems.

$ yaourt -S dsh
or
$ packer -S dsh

For other Distro : For other distribution such as RHEL, CentOS, Fedora, openSUSE We can compile from source when distribution doesn’t offer official package. Make sure you have to installed libdshconfig package for dependency.

# wget http://www.netfort.gr.jp/~dancer/software/downloads/libdshconfig-0.20.10.cvs.1.tar.gz
# tar xfz libdshconfig*.tar.gz 
# cd libdshconfig-*
# ./configure ; make
# make install

Compile and install dsh.

# wget http://www.netfort.gr.jp/~dancer/software/downloads/dsh-0.22.0.tar.gz
# tar xfz dsh-0.22.0.tar.gz
# cd dsh-*
# ./configure ; make 
# make install

Configure dsh

Open the configuration file and modify the remote shell protocol to ssh instead of rsh, since rsh is an unencrypted protocol.

For Debian based systems:

$ sudo vi /etc/dsh/dsh.conf
remoteshell =ssh

For RHEL based systems:

$ sudo vi /usr/local/etc/dsh.conf
remoteshell =ssh

Configure remote host

Open/create a machines.list file and add your remote hosts IP or Hostname one by one to run the command at once. Make sure you have to replace your IP’s instead of us.

For Debian based systems:

$ sudo vi /etc/dsh/machines.list
192.168.1.150
192.168.1.151
192.168.1.152

For RHEL based systems we won’t get a machines.list file by default, so create a new one.

# touch /usr/local/etc/machines.list
# vi /usr/local/etc/machines.list
192.168.1.150
192.168.1.151
192.168.1.152

How to use dsh

Once everything is ready, you can use dsh to perform the activity. It will ask remote hosts password every time, to avoide this set up SSH password-less login.

To get uptime using DSH command

$ dsh -a -c uptime
 13:12:23 up 99 days, 19:12,  1 user,  load average: 1.83, 1.36, 0.82

 19:11:44 up 320 days,  4:09,  1 user,  load average: 0.14, 0.19, 0.14

 13:10:22 up 22 days, 19:13,  0 users,  load average: 0.00, 0.12, 0.17

How to create groups on dsh

When you have more than one activity, I would advise you to create a groups and perform the activity smoothly.

For Debian based systems:

$ sudo touch /etc/dsh/groups/week1
$ sudo vi /etc/dsh/groups/week1
192.168.1.150
192.168.1.151
192.168.1.152

For RHEL based systems we won’t get groups folder as well files by default, so create a new one.

# mkdir /usr/local/etc/groups
# touch /usr/local/etc/groups/week2
# vi /usr/local/etc/groups/week2
192.168.1.150
192.168.1.151
192.168.1.152

To get list of user logged in system using DSH command.

$ dsh -g week1 -c w
 13:39:46 up 197 days,  8:12,  1 user,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    103.5.134.17      12:55    0.00s  0.02s  0.00s w

 13:28:18 up 22 days, 19:29,  1 user,  load average: 0.13, 0.04, 0.05
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    103.5.134.17      Thu11    2.00s  0.50s  0.00s w

 13:27:10 up 22 days, 19:31,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    103.5.134.17      Tue11    4days  0.19s  0.19s -bash

Leave a Reply

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