How to execute commands on Multiple Remote Server - pssh
Executable Commands on Multiple Remote Linux Servers Using Single Terminal &ndash pssh
This article gives information about the installation of pssh. PSSH tool comes in handy, python based application that allow us to execute commands on multiple hosts in parallel at the same time.
Some of the PSSH tools
pscp &ndash Copy/Transfer Files Two or More Remote Linux Servers
pslurp &ndash copies files from multiple remote hosts to a central host in parallel.
prsync &ndash is a program for efficiently copying files to multiple hosts in parallel.
pssh &ndash is a program for running ssh in parallel on a multiple remote hosts.
pscp &ndash is a program for copying files in parallel to a number of hosts.
pnuke &ndash kills processes on multiple remote hosts in parallel.
It can be work with large collections of nodes on a network.
To Install PSSH or Parallel SSH on Linux
Use pip command which is a small program to install and manage the Python software packages index.c. Before installing the packages, update your repositories.
root@linuxhelp:~# apt-get update
Get:1 http://security.ubuntu.com wily-security InRelease [65.9 kB]
Hit http://in.archive.ubuntu.com wily InRelease
Get:2 http://in.archive.ubuntu.com wily-updates InRelease [65.9 kB]
Get:3 http://in.archive.ubuntu.com wily-backports InRelease [65.9 kB]
Get:4 http://security.ubuntu.com wily-security/main Sources [48.9 kB]
.
.
.
Get:50 http://in.archive.ubuntu.com wily-backports/restricted Translation-en [28 B]
Get:51 http://in.archive.ubuntu.com wily-backports/universe Translation-en [1,390 B]
Fetched 1,789 kB in 1min 4s (27.8 kB/s)
Reading package lists... Done
Now install the python-pip package.
root@linuxhelp:~# apt-get install python-pip -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libavahi-client-dev libavahi-common-dev libavahi-compat-libdnssd1
.
.
.
Setting up python-requests (2.7.0-3) ...
Setting up python-setuptools (18.4-1) ...
Setting up python-pip (1.5.6-7ubuntu1.2) ...
Setting up python-wheel (0.26.0-1) ...
Then install PSSH using pip command.
root@linuxhelp:~# pip install pssh
Downloading/unpacking pssh
Downloading pssh-2.3.1.tar.gz
Running setup.py (path:/tmp/pip-build-lmMWO5/pssh/setup.py) egg_info for package pssh
Installing collected packages: pssh
Running setup.py install for pssh
changing mode of build/scripts-2.7/pssh from 644 to 755
changing mode of build/scripts-2.7/pnuke from 644 to 755
changing mode of build/scripts-2.7/prsync from 644 to 755
changing mode of build/scripts-2.7/pslurp from 644 to 755
changing mode of build/scripts-2.7/pscp from 644 to 755
changing mode of build/scripts-2.7/pssh-askpass from 644 to 755
changing mode of /usr/local/bin/pssh-askpass to 755
changing mode of /usr/local/bin/pnuke to 755
changing mode of /usr/local/bin/pssh to 755
changing mode of /usr/local/bin/pscp to 755
changing mode of /usr/local/bin/pslurp to 755
changing mode of /usr/local/bin/prsync to 755
Successfully installed pssh
Cleaning up...
How to use pssh
Create a host “ node.txt” file with the number of hosts along with IP address and port number which is used to connect to remote systems using pssh
root@linuxhelp:~# vim node.txt
Now the node.txt file is created. Add the Following entries in the file.
Entry: 192.168.5.88:22
To execute single command on multiple server using pssh
There are many options to execute single command on multiple server using pssh. Some of them are,
- To include a default username on all hosts that do not define a specific user, use the -l username or &ndash user username option.
- We can also display standard output and standard error as each host completes. By using the -i or &ndash inline option.
- To ask for a password and send to ssh, use the -A option.
- To read hosts file, include the -h host_file-name or &ndash hosts host_file_name option.
- We may wish to make connections time out after the given number of seconds by including the -t number_of_seconds option.
- To save standard output to a given directory, you can use the -o /directory/path option.
Some examples and usage of pssh commands
Execute the ifconfig command by using PSSH over remote machines.
root@linuxhelp:~# pssh -h node.txt -l root -A -i " ifconfig"
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 09:40:05 [SUCCESS] 192.168.5.88:22
eno16777736: flags=4163< UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.5.88 netmask 255.255.255.0 broadcast 192.168.5.255
inet6 fe80::20c:29ff:fe83:a038 prefixlen 64 scopeid 0x20
ether 00:0c:29:83:a0:38 txqueuelen 1000 (Ethernet)
RX packets 1926 bytes 171341 (167.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 157 bytes 20285 (19.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73< UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 0 (Local Loopback)
RX packets 10 bytes 870 (870.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10 bytes 870 (870.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Run the below command to check the kernel version on remote machine.
root@linuxhelp:~# pssh -h node.txt -l root -A -i " uname -r"
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 09:51:06 [SUCCESS] 192.168.5.88:22
3.10.0-123.el7.x86_64
pssh help
root@linuxhelp:~# pssh --help
Usage: pssh [OPTIONS] command [...]
Options:
--version show program' s version number and exit
--help show this help message and exit
-h HOST_FILE, --hosts=HOST_FILE
hosts file (each line " [user@]host[:port]" )
-H HOST_STRING, --host=HOST_STRING
additional host entries (" [user@]host[:port]" )
-l USER, --user=USER username (OPTIONAL)
-p PAR, --par=PAR max number of parallel threads (OPTIONAL)
-o OUTDIR, --outdir=OUTDIR
output directory for stdout files (OPTIONAL)
-e ERRDIR, --errdir=ERRDIR
output directory for stderr files (OPTIONAL)
-t TIMEOUT, --timeout=TIMEOUT
timeout (secs) (0 = no timeout) per host (OPTIONAL)
-O OPTION, --option=OPTION
SSH option (OPTIONAL)
-v, --verbose turn on warning and diagnostic messages (OPTIONAL)
-A, --askpass Ask for a password (OPTIONAL)
-x ARGS, --extra-args=ARGS
Extra command-line arguments, with processing for
spaces, quotes, and backslashes
-X ARG, --extra-arg=ARG
Extra command-line argument
-i, --inline inline aggregated output and error for each server
--inline-stdout inline standard output for each server
-I, --send-input read from standard input and send as input to ssh
-P, --print print output as we get it