How to use DSH to run Linux Commands in Multiple Machines
To use DSH to run Linux Commands in Multiple Machines
DSH stands for “Distributed Shell”. It can be built easily from its source in any linux distributions. Usage of DSH (Distributed Shell) to run Linux commands across multiple machines are discussed in this tutorial.
To install DSH (Distributed Shell) In Linux
Run the following command to install the dependency packages.
On Debian / Ubuntu
For Debian based systems, dsh can be directly installed using apt-get command.
$ sudo apt-get install dsh
On RHEL / CentOS / Fedora
For Red-Hat based systems, download the tar package and extract it.
\[root@linuxhelp ~\]# wget http://www.netfort.gr.jp/~dancer/software/downloads/libdshconfig-0.20.10.cvs.1.tar.gz
--2016-06-28 09:54:26-- http://www.netfort.gr.jp/~dancer/software/downloads/libdshconfig-0.20.10.cvs.1.tar.gz
Resolving www.netfort.gr.jp (www.netfort.gr.jp)... 133.242.142.232, 2401:2500:102:1204:133:242:142:232
Connecting to www.netfort.gr.jp (www.netfort.gr.jp)|133.242.142.232|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://www.netfort.gr.jp/~dancer/software/downloads/libdshconfig-0.20.10.cvs.1.tar.gz \[following\]
--2016-06-28 09:54:26-- https://www.netfort.gr.jp/~dancer/software/downloads/libdshconfig-0.20.10.cvs.1.tar.gz
Connecting to www.netfort.gr.jp (www.netfort.gr.jp)|133.242.142.232|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 186195 (182K) \[application/x-gzip\]
Saving to: ‘libdshconfig-0.20.10.cvs.1.tar.gz’
100%\[===============================================================>\] 186,195 249KB/s in 0.7s
2016-06-28 09:54:27 (249 KB/s) - ‘libdshconfig-0.20.10.cvs.1.tar.gz’ saved \[186195/186195\]
Extract the downloaded tar file and install using configure and make commands.
\[root@linuxhelp ~\]# tar xfz libdshconfig-0.20.10.cvs.1.tar.gz
\[root@linuxhelp ~\]# cd libdshconfig-0.20.10.cvs.1/
\[root@linuxhelp libdshconfig-0.20.10.cvs.1\]# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
/root/libdshconfig-0.20.10.cvs.1/missing: Unknown \`--run' option
Try \`/root/libdshconfig-0.20.10.cvs.1/missing --help' for more information
configure: WARNING: \`missing' script is too old or missing
checking for gawk... gawk
checking whether make sets ${MAKE}... yes
checking whether to enable maintainer-specific portions of Makefiles... no
checking build system type... x86\_64-unknown-linux-gnu
checking host system type... x86\_64-unknown-linux-gnu
checking for style of include used by make... GNU
checking for gcc... gcc
.
.
config.status: creating config.h
config.status: executing default-1 commands
\[root@linuxhelp libdshconfig-0.20.10.cvs.1\]# make
cd .
&& CONFIG\_FILES= CONFIG\_HEADERS=config.h
/bin/sh ./config.status
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing default-1 commands
make all-am
make\[1\]: Entering directory \`/root/libdshconfig-0.20.10.cvs.1'
source='libdshconfig.c' object='libdshconfig.lo' libtool=yes
depfile='.deps/libdshconfig.Plo' tmpdepfile='.deps/libdshconfig.TPlo'
depmode=gcc3 /bin/sh ./depcomp
.
.
creating libdshconfig.la
(cd .libs && rm -f libdshconfig.la && ln -s ../libdshconfig.la libdshconfig.la)
make\[1\]: Leaving directory \`/root/libdshconfig-0.20.10.cvs.1'
Execute the below command for final installation.
\[root@linuxhelp libdshconfig-0.20.10.cvs.1\]# make install
make\[1\]: Entering directory \`/root/libdshconfig-0.20.10.cvs.1'
/bin/sh ./mkinstalldirs /usr/local/lib
/bin/sh ./libtool --mode=install /usr/bin/install -c libdshconfig.la /usr/local/lib/libdshconfig.la
/usr/bin/install -c .libs/libdshconfig.so.1.0.0 /usr/local/lib/libdshconfig.so.1.0.0
(cd /usr/local/lib && rm -f libdshconfig.so.1 && ln -s libdshconfig.so.1.0.0 libdshconfig.so.1)
(cd /usr/local/lib && rm -f libdshconfig.so && ln -s libdshconfig.so.1.0.0 libdshconfig.so)
/usr/bin/install -c .libs/libdshconfig.lai /usr/local/lib/libdshconfig.la
/usr/bin/install -c .libs/libdshconfig.a /usr/local/lib/libdshconfig.a
ranlib /usr/local/lib/libdshconfig.a
chmod 644 /usr/local/lib/libdshconfig.a
PATH="$PATH:/sbin" ldconfig -n /usr/local/lib
----------------------------------------------------------------------
Libraries have been installed in:
/usr/local/lib
.
.
/bin/sh ./mkinstalldirs /usr/local/include
/usr/bin/install -c -m 644 libdshconfig.h /usr/local/include/libdshconfig.h
make\[1\]: Leaving directory \`/root/libdshconfig-0.20.10.cvs.1'
Run the following command to install and compile DSH.
\[root@linuxhelp ~\]# wget http://www.netfort.gr.jp/~dancer/software/downloads/dsh-0.22.0.tar.gz
--2016-06-28 09:57:52-- http://www.netfort.gr.jp/~dancer/software/downloads/dsh-0.22.0.tar.gz
Resolving www.netfort.gr.jp (www.netfort.gr.jp)... 133.242.142.232, 2401:2500:102:1204:133:242:142:232
Connecting to www.netfort.gr.jp (www.netfort.gr.jp)|133.242.142.232|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://www.netfort.gr.jp/~dancer/software/downloads/dsh-0.22.0.tar.gz \[following\]
--2016-06-28 09:57:52-- https://www.netfort.gr.jp/~dancer/software/downloads/dsh-0.22.0.tar.gz
Connecting to www.netfort.gr.jp (www.netfort.gr.jp)|133.242.142.232|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 259258 (253K) \[application/x-gzip\]
Saving to: ‘dsh-0.22.0.tar.gz’
100%\[===============================================================>\] 259,258 152KB/s in 1.7s
2016-06-28 09:57:54 (152 KB/s) - ‘dsh-0.22.0.tar.gz’ saved \[259258/259258\]
Extract the downloaded DSH tar file and install using configure and make commands.
\[root@linuxhelp ~\]# tar xfz dsh-0.22.0.tar.gz
\[root@linuxhelp ~\]# cd dsh-0.22.0/
\[root@linuxhelp dsh-0.22.0\]# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... gawk
checking whether make sets ${MAKE}... yes
checking whether to enable maintainer-specific portions of Makefiles... no
checking for style of include used by make... GNU
checking for gcc... gcc
checking for C compiler default output... a.out
checking whether the C compiler works... yes
.
.
config.status: creating po/POTFILES
config.status: creating po/Makefile
\[root@linuxhelp dsh-0.22.0\]# make
make all-recursive
make\[1\]: Entering directory \`/root/dsh-0.22.0'
Making all in m4
make\[2\]: Entering directory \`/root/dsh-0.22.0/m4'
make\[2\]: Nothing to be done for \`all'.
make\[2\]: Leaving directory \`/root/dsh-0.22.0/m4'
Making all in po
make\[2\]: Entering directory \`/root/dsh-0.22.0/po'
make\[2\]: Nothing to be done for \`all'.
.
.
gcc -g -O2 -Wall -o dsh dsh-dsh.o dsh-linkedlist.o dsh-parameter.o /usr/local/lib/libdshconfig.so -Wl,--rpath -Wl,/usr/local/lib -Wl,--rpath -Wl,/usr/local/lib
make\[2\]: Leaving directory \`/root/dsh-0.22.0'
make\[1\]: Leaving directory \`/root/dsh-0.22.0'
Finally install DSH by running the "make install" command
\[root@linuxhelp dsh-0.22.0\]# make install
Making install in m4
make\[1\]: Entering directory \`/root/dsh-0.22.0/m4'
make\[2\]: Entering directory \`/root/dsh-0.22.0/m4'
make\[2\]: Nothing to be done for \`install-exec-am'.
make\[2\]: Nothing to be done for \`install-data-am'.
make\[2\]: Leaving directory \`/root/dsh-0.22.0/m4'
make\[1\]: Leaving directory \`/root/dsh-0.22.0/m4'
Making install in po
make\[1\]: Entering directory \`/root/dsh-0.22.0/po'
/bin/sh \`case "./mkinstalldirs" in /\*) echo "./mkinstalldirs" ;; \*) echo ".././mkinstalldirs" ;; esac\` /usr/local/share
mkdir /usr/local/share/locale
mkdir /usr/local/share/locale/ja
mkdir /usr/local/share/locale/ja/LC\_MESSAGES
.
. make\[2\]: Leaving directory \`/root/dsh-0.22.0'
make\[1\]: Leaving directory \`/root/dsh-0.22.0'
Open the configuration file "dsh.conf" located in the path “usr/local/etc/" and edit as shown below.
\[root@linuxhelp dsh-0.22.0\]# vim usr/local/etc/dsh.conf
remoteshell =ssh
waitshell=0
Now, save and exit the file.
Create a new file called “machines.list” in “/usr/local/etc/” and add the IP/ Host name of machines, for which you want to check the uptime. You can also use ssh password less option to view the uptime.
\[root@linuxhelp dsh-0.22.0\]# vim usr/local/etc/machines.list
######Remote machines #######
192.168.5.88
192.168.5.89
192.168.5.90
To view uptime using DSH command
Run the following command to view the uptime of all the machines.
\[root@linuxhelp ~\]# dsh -a -c uptime
######Remote machines #######:
192.168.5.90: 10:19:17 up 1:47, 1 user, load average: 0.00, 0.00, 0.00
192.168.5.89: 10:19:15 up 1:47, 2 users, load average: 0.14, 0.11, 0.08
192.168.5.88: 10:19:15 up 1:48, 2 users, load average: 0.00, 0.01, 0.05
Utilize '&&' symbol to view the uptime along with date.
\[root@linuxhelp ~\]# dsh -a -c "uptime && date"
######Remote machines #######:
192.168.5.90: 10:21:09 up 1:49, 1 user, load average: 0.00, 0.00, 0.00
192.168.5.90: Tue Jun 28 10:21:09 IST 2016
192.168.5.89: 10:21:07 up 1:49, 2 users, load average: 0.04, 0.09, 0.07
192.168.5.89: Tue Jun 28 10:21:07 IST 2016
192.168.5.88: 10:21:07 up 1:50, 2 users, load average: 0.00, 0.01, 0.05
192.168.5.88: Tue Jun 28 10:21:07 IST 2016
Command Explanation
-a option → to send the "uptime" command to all machines listed in “/etc/dsh/machines.list"
-c option → command to be executed
In the similar way we can check the uptime for group of machines. For that create a folder called "groups" in “/usr/local/etc/”.
\[root@linuxhelp ~\]# cd /usr/local/etc/
\[root@linuxhelp etc\]# mkdir group
And create a new file with the IP/ HOSTNAME of the remote machines.
\[root@linuxhelp etc\]# cd group
\[root@linuxhelp group\]# vim group1
192.168.5.88
192.168.5.89
192.168.5.90
Now execute the below command to view the uptime of remote machines in that newly created group.
\[root@linuxhelp etc\]# dsh -g group1 -c uptime
192.168.5.90: 10:38:21 up 2:06, 1 user, load average: 0.00, 0.00, 0.00
192.168.5.89: 10:38:19 up 2:06, 2 users, load average: 0.00, 0.01, 0.05
192.168.5.88: 10:38:20 up 2:07, 2 users, load average: 0.61, 0.17, 0.09
Command Explanation
-c option → command to be executed
-g option → to mention the group
Comments ( 0 )
No comments available