📜 ⬆️ ⬇️

4 tools for simultaneous execution of commands on multiple Linux servers

The article, the translation of which we are publishing today, is devoted to technologies of simultaneous execution of commands on several Linux servers. Here we will talk about several well-known tools that implement such functionality. This material is useful for system administrators who, for example, regularly have to check the status of multiple remote systems. It is assumed that the reader already has several servers to which access via SSH is organized. In addition, when working simultaneously with several machines, it is very useful to configure SSH access to them by key, without a password . This approach, on the one hand, increases server security, and on the other, it facilitates working with it.



1. PSSH - Parallel SSH


PSSH is an open source command line toolkit written in Python and designed to execute SSH commands in parallel on multiple Linux systems. It works quickly and is easy to learn. PSSH includes tools such as parallel-ssh , parallel-scp , parallel-rsync , parallel-slurp and parallel-nuke (for details on these tools, see the man).

Before installing parallel-ssh on a Linux system, you must first install pip . Here is how it is done in different distributions:
')
 $ sudo apt install python-pip python-setuptools   #Debian/Ubuntu # yum install python-pip python-setuptools        #RHEL/CentOS # dnf install python-pip python-setuptools        #Fedora 22+ 

Then parallel-ssh installed using pip :

 $ sudo pip install parallel-ssh 

Next, you need to enter the host names or IP addresses of remote Linux servers and port information in the hosts (in fact, you can call it whatever you like). Here we need this command:

 $ vim hosts 

Here is an example of the contents of such a file:

 192.168.0.10:22 192.168.0.11:22 192.168.0.12:22 

After everything necessary is entered into the file, it's time to start parallel-ssh by passing the file name to this utility using the -h option, as well as the commands that need to be run on all servers whose addresses are in the hosts . The utility -i flag is used to display what gets into the standard output and error streams after completing the execution of commands on the servers.

The parallel-ssh startup command might look like this:

 $ parallel-ssh -h hosts "uptime; df -h" 

The following figure shows the use of the utility when working with three servers.


The parallel-ssh utility executes commands on multiple servers

2. Pdsh - Parallel Remote Shell Utility


Pdsh is, again, an open source solution, which is a shell for the simultaneous execution of commands on several Linux servers.

Here's how to install pdsh in various distributions:

 $ sudo apt install pdsh         #Debian/Ubuntu # yum install pdsh              #RHEL/CentOS # dnf install pdsh              #Fedora 22+ 

In order to execute commands on several servers, the addresses of these servers, like when using parallel-ssh , must be added to a file, which can also be called hosts . Then you need to run pdsh as follows:

 $ pdsh -w ^hosts -R ssh "uptime; df -h" 

Here the -w flag is used to specify the file with the list of servers, the -R flag is used to specify the remote command module (among the available remote command modules are ssh , rsh , exec ; rsh used by default). Notice the ^ icon in front of the server list file name.

This is what working with this team looks like.


Executing commands on multiple servers using pdsh

If you, when calling pdsh , did not specify a list of commands to be executed on the servers, this utility will run interactively. Details on pdsh can be found on the corresponding man page.

3. ClusterSSH


ClusterSSH is a command line tool designed to administer server clusters. It launches the administrator console and, for each server, a separate xterm window. After that on all these servers it is possible to simultaneously execute the same commands.

Install clusterssh :

 $ sudo apt install clusterssh    #Debian/Ubuntu # yum install clusterssh         #RHEL/CentOS $ sudo dnf install clusterssh    #Fedora 22+ 

Now, to connect to the servers, you need to run a command like this:

 $ clusterssh linode cserver contabo 

You can also use this design:

 $ clusterssh username@server1 username@server2 username@server3 

After this, you will see something similar to what is shown in the following figure.


Working with multiple servers using clusterssh

Commands entered in the administrator console are executed on all servers. To execute commands on a separate server, enter them in a window open for it.

4. Ansible


Ansible is a popular open source tool for automating IT processes. It is used to configure and manage systems, to install applications and to solve other problems.

Install ansible :

 $ sudo apt install ansible       #Debian/Ubuntu # yum install ansible            #RHEL/CentOS $ sudo dnf install ansible       #Fedora 22+ 

After that, you need to add server addresses to the /etc/ansible/hosts .

 $ sudo vim /etc/ansible/hosts 

Here is an example of a fragment of a similar file with several systems grouped into a webservers group:

 # Ex 2: A collection of hosts belonging to the 'webservers' group [webservers] 139.10.100.147 139.20.40.90 192.30.152.186 

Now, in order to get the information of the uptime and find out which users are connected to the hosts belonging to the webservers group, you can use the following construction:

 $ ansible webservers -a "w " -u admin 

Here, the -a option is used to specify the arguments passed to the module, and the -u flag allows you to set the default username used to connect to remote servers via SSH.

Notice that the ansible command-line ansible allows ansible to execute commands only one at a time.


Interaction with multiple servers by means of ansible

Results


In this article, we talked about tools that are designed to simultaneously execute commands on multiple servers running Linux. If you are thinking about automating tasks for managing multiple servers, we hope you will find something here that suits you.

Dear readers! Do you know of any useful utilities that simplify the administration of a large number of servers?

Source: https://habr.com/ru/post/426849/


All Articles