How To Run Commands On Multiple Remote Servers Simultaneously

WebsiteInternetWeb Services

OpenSSH is a widely used tool to access the remote servers, copy or transfer files, and perform all sorts of system administration tasks. One caution while using OpenSSH is that you can’t run the same command on multiple remote servers in one go. However, Don’t be disappointed! Meet PSSH, Parallel SSH, a simple, and useful alternative to OpenSSH that allows you to run commands on multiple servers from a single terminal. Sounds good? Read on to find more –

PSSH consists of the following commands, such as:

  • pssh : SSH to multiple servers at once
  • pscp : Transfer and copy files to multiple remote systems
  • prsync : Copy files to multiple hosts in parallel
  • pnuke : kills processes on multiple remote hosts
  • pslurp : Copies files from multiple remote hosts to a central host

In this article, we will discuss on – how to install and use Pssh in real time.

Install PSSH or Parallel SSH in Linux

The easiest way to install Pssh is to use pip. For those who don’t know already, pip is a package management system used to install and manage software packages written in Python. PSSH is supported on Python 2.4 and greater versions.

Pip is available in the default repositories of most Linux distributions. Run the following command to install python-pip in your Linux distribution.

Run the following command to install python-pip in your Linux distribution.

On Arch Linux and its derivatives:

sudo pacman -S python-pip

On RHEL, CentOS, Fedora:

sudo yum install python-pip

Or,

sudo dnf install python-pip

On Debian, Ubuntu and derivatives:

sudo apt-get install python-pip

Once you have installed pip, run the following command to install Pssh. This command is same for all Linux distributions.

sudo sudo pip install pssh

Sample Output

Downloading/unpacking pssh
Downloading pssh-2.3.1.tar.gz
Running setup.py (path:/tmp/pip_build_root/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/pslurp to 755
changing mode of /usr/local/bin/pscp to 755
changing mode of /usr/local/bin/pssh to 755
changing mode of /usr/local/bin/prsync to 755
changing mode of /usr/local/bin/pnuke to 755
Successfully installed pssh
Cleaning up...

Pssh has been installed on our system. Let us see some practical examples to learn how to use Pssh in real time.

Pssh Usage

Usage of Pssh is fairly easy! You need to create a host file and add your remote server’s IP address with SSH port number.

Let us now create a host file:

sudo nano hostfile.txt

You can use any name of your liking to this file. Then, add the remote server’s details one by one as shown below –

192.168.1.100:22
192.168.1.102:22

Here, 192.168.1.100 and 192.168.1.102 are the IP addresses of my remote Linux hosts. Save and close the file. It’s time to test some commands.

Let us check the uptime of both remote servers in one command. To do so, run:

pssh -h hostfile.txt -l sk -A -i "uptime"

Important note: Here, sk is the username of my remote servers. Please note that you need to have a user called sk with the same password in all remote servers. Otherwise, this command couldn’t help. So it is a must to have a common username with the same password in all your remote servers. Clear? Good.

Enter the password of the user “sk”.

Sample Output

Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 17:02:08 [SUCCESS] 192.168.1.100:22
17:02:23 up 5:10, 1 user, load average: 0.24, 0.38, 0.61
[2] 17:02:08 [SUCCESS] 192.168.1.150:22
17:02:22 up 50 min, 1 user, load average: 0.00, 0.01, 0.05

The command will display the uptime of both remote servers.

Warning: You need to be very careful while using Pssh. Since it is meant to be used to run single command on multiple servers at once, one bad command will harm multiple servers simultaneously. So be very cautious while using it.

Let us check kernel version in both the servers. To do so, run:

pssh -h hostfile.txt -l sk -A -i "uname -r"

Sample Output

Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 17:06:13 [SUCCESS] 192.168.1.100:22
4.8.13-1-ARCH
[2] 17:06:13 [SUCCESS] 192.168.1.150:22
3.10.0-327.22.2.el7.x86_64

Let us send a message to all remote servers.

pssh -h hostfile.txt -l sk -A echo "Welcome to Tutroialpoint!!"

Can I copy files to all my remote servers at once? Yes, of course, you can!

Let us copy a file called test.txt to all remote server with a single command.

pscp -h hostfile.txt -l sk -Av -r test.txt /tmp/

Sample Output

Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 17:36:59 [SUCCESS] 192.168.1.100:22
[2] 17:37:03 [SUCCESS] 192.168.1.150:22

Now, let us go to the remote server and check the test.txt file is copied in /tmp/ directory.

Great! test.txt is there.

The same can be applied to the directories. To copy a directory, run:

pscp -h hostfile.txt -l sk -Av -r tutorialspoint/ /tmp/

For more details, refer the man pages.

man pssh

The above command will copy the local directory called tutorialspoint to your remote system’s /tmp/ directory.

That’s all for now. Hope this guide helps. For more interesting updates about Linux, please keep reading our articles.

raja
Published on 23-Jan-2020 07:30:21
Advertisements