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:
On RHEL, Fedora, CentOS:
Or,
On Debian, Ubuntu, Linux Mint:
Once PIP installed, run the following command to 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.
Add IP addresses of your remote hosts with port numbers one by one as exactly shown below.
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:
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:
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:
Sample output:
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:
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:
Post a Comment