Skip to main content

How To Run Single Command On Multiple Remote Systems At Once

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.

Install PSSH

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

For more details about managing python packages using PIP, refer the following link.

Once PIP installed, run the following command to install PSSH.

$ sudo pip install pssh

PSSH has been installed! Let us go ahead and see how to use it.

Run Single Command On Multiple Remote Systems At Once Using PSSH

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 named sk with password ostechnix on all my remote hosts. You should have a same user with same password on all your remote systems as well.

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’ve already changed it. 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.

Important: Please be very careful while using PSSH. One bad command will perform simultaneously on multiple hosts and damage all hosts. So, be very careful 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.

Comments

Popular posts from this blog

How to use Ngx-Charts in Angular ?

Charts helps us to visualize large amount of data in an easy to understand and interactive way. This helps businesses to grow more by taking important decisions from the data. For example, e-commerce can have charts or reports for product sales, with various categories like product type, year, etc. In angular, we have various charting libraries to create charts.  Ngx-charts  is one of them. Check out the list of  best angular chart libraries .  In this article, we will see data visualization with ngx-charts and how to use ngx-charts in angular application ? We will see, How to install ngx-charts in angular ? Create a vertical bar chart Create a pie chart, advanced pie chart and pie chart grid Introduction ngx-charts  is an open-source and declarative charting framework for angular2+. It is maintained by  Swimlane . It is using Angular to render and animate the SVG elements with all of its binding and speed goodness and uses d3 for the excellent math functio...

Understand Angular’s forRoot and forChild

  forRoot   /   forChild   is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it: “Understanding how  forRoot()  works to make sure a service is a singleton will inform your development at a deeper level.” So let’s go. Providers & Injectors Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You  inject  the service and the dependency injection system takes care of providing an instance. import { Component, OnInit } from '@angular/core'; import { TestService } from 'src/app/services/test.service'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.compon...

How to solve Puppeteer TimeoutError: Navigation timeout of 30000 ms exceeded

During the automation of multiple tasks on my job and personal projects, i decided to move on  Puppeteer  instead of the old school PhantomJS. One of the most usual problems with pages that contain a lot of content, because of the ads, images etc. is the load time, an exception is thrown (specifically the TimeoutError) after a page takes more than 30000ms (30 seconds) to load totally. To solve this problem, you will have 2 options, either to increase this timeout in the configuration or remove it at all. Personally, i prefer to remove the limit as i know that the pages that i work with will end up loading someday. In this article, i'll explain you briefly 2 ways to bypass this limitation. A. Globally on the tab The option that i prefer, as i browse multiple pages in the same tab, is to remove the timeout limit on the tab that i use to browse. For example, to remove the limit you should add: await page . setDefaultNavigationTimeout ( 0 ) ;  COPY SNIPPET The setDefaultNav...