Friday, February 3, 2017

Quick Tips: run single command to multiple systems[]: How To Run Single Command On Multiple Remote Systems At Once

How To Run Single Command On Multiple Remote Systems At Once


Today, we are going to see to how to run a single command on multiple remote systems at once in Unix-like operating systems. As you already know, we can access and communicate with remote system using ssh. openSSH allows us to do all sorts of administration tasks in a remote system. One limitation with openSSH is we can’t run the single command on multiple remote systems at once. No problem. Here comes PSSH in help.

PSSH, or Parallel SSH, is a command line suite that helps you to ssh in parallel on a number of hosts. PSSH suite consists of  the following commands:
  • pssh – SSH to multiple remote systems in parallel
  • pscp – Copy files in parallel to a number of hosts
  • prsync : Copy files in parallel to a number of hosts
  • pnuke : Kill processes in parallel on a number of hosts
  • pslurp : Copy files in parallel from a number of hosts
In this tutorial, we will see how to execute a single command on multiple hosts at once using PSSH.

Run Single Command On Multiple Remote Systems At Once

We can easily install PSSH using PIP, a python package manager.
To install PIP on Arch Linux and its derivatives, run:
sudo pacman -S python-pip
On RHEL, Fedora, CentOS:
sudo yum install epel-release
sudo yum install python-pip
Or,
sudo dnf install epel-release
sudo dnf install python-pip
On Debian, Ubuntu, Linux Mint:
sudo apt-get install python-pip
Once PIP installed, run the following command to install PSSH.
sudo pip install pssh

Usage

Important: In order to use PSSH (for the purpose of this tutorial only), all your remote systems must have a common username with same password. Otherwise, this method won’t help. Say for example, I have already created an user called sk with password ostechnix on all my remote hosts.
Now, let us see how to run a single command on multiple remote hosts using PSSH. Go to your local system where you want to run the command and create a text file called remotehosts.txt. You can name it as you wish.
vi remotehosts.txt
Add IP addresses of your remote hosts with port numbers one by one as exactly shown below.
192.168.1.103:22
192.168.1.104:22
Where, 192,168.1.103 and 192.168.1.104 are the IP addresses of my remote systems. 22 is the ssh port number. You need to mention the correct port number if you changed it already. Also, make sure you can be able to access all remote hosts from your local system via ssh.
Now, let us check the uptime of both remote hosts from our local system. To do so, run:
pssh -h remotehosts.txt -l sk -A -i "uptime"
Here,
  • remotehosts.txt – Contains the IP addresses of both remote systems.
  • sk – the username of both remote systems
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] 20:51:15 [SUCCESS] 192.168.1.103:22
 20:50:50 up 8 min, 1 user, load average: 0.05, 0.11, 0.10
[2] 20:51:15 [SUCCESS] 192.168.1.104:22
 20:50:52 up 12 min, 1 user, load average: 0.00, 0.07, 0.12
As you see above, we have run the uptime command on two remote hosts and got the result in one go.
What about the kernel version? To check the installed version of both remote hosts, run:
pssh -h remotehosts.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] 20:53:09 [SUCCESS] 192.168.1.103:22
3.10.0-327.22.2.el7.x86_64
[2] 20:53:09 [SUCCESS] 192.168.1.104:22
4.4.0-21-generic
Very cool, isn’t? Can we create a directory on both remote hosts at once? Yes, of course! To do so, run the following command:
pssh -h remotehosts.txt -l sk -A -i "mkdir dir1"
Similarly, you can do anything you want to do on multiple remote hosts from your local system using PSSH.
Very very Important: Please be very careful while using PSSH. One bad command will perform simultaneously on multiple hosts and damage all hosts. So, be cautious while using this method in production. I suggest you to test this in a virtual machines. Once you’re familiar with PSSH, you can use it on production if you like to.

No comments: