How to run multiple batch commands on multiple Linux servers

by time news

If you are managing multiple linux servers and want to run various commands on all linux servers but have no idea how to do it. No need to worry, in this simple server administration guide, we will show you how to run multiple commands on multiple Linux servers simultaneously.

To achieve this, you can use the pssh (parallel ssh) program, a command line utility to run ssh in parallel on multiple hosts. With it, you can send information to all ssh processes, from a shell script.

Requirements

And then.

  1. You must use SSH passwordless authentication for all remote servers.

Create a shell script

Therefore, you should start by preparing a script that contains the Linux commands that you want to run on the different servers. In this example, we will write a script that will collect the following information from multiple servers:

  • Check the uptime of the servers
  • Check who’s logged in and what they’re doing
  • List the top 5 running processes based on memory usage.

First create a script called comandos.sh with your favorite editor.

# vi comandos.sh

Then add the following commands to the script as shown.

#!/bin/bash
############################################## ############################
#Nombre del script: commands.sh                       
#Descripción: ejecuta múltiples comandos en múltiples servidores                                                                     
#Autor: Aaron Kili Kisinga       
#Correo electrónico: Esta dirección de correo electrónico está siendo protegida contra los robots de spam. Necesita tener JavaScript habilitado para poder verlo. 
############################################## ############################
echo
# mostrar el tiempo de actividad del sistema
uptime
echo
# mostrar quién está conectado y qué está haciendo
who
echo
# mostrar los 5 mejores procesos por uso de RAM
ps -eo cmd,pid,ppid,%mem,%cpu --sort=-%mem | head -n 6

exit 0

Save the file and close it. Then make the script executable as shown.

# chmod a+x comandos.sh

Create PSSH hosts file

Next, add the list of servers you want to run the commands on, to a file hosts.txt in the format [user@]host[:port] or just provide the IP addresses of the server.

But we suggest you use ssh aliases which can be specified in the file .ssh/config as explained in how to configure custom ssh connections to simplify remote access.

This method is more efficient and reliable, allowing you to specify configuration options (such as hostname, file identifier, port, username, etc.) for each remote server.

Below is our sample ssh host aliases file, also known as a user-specific ssh configuration file.

# vi ~/.ssh/config

SSH host aliases file

Then create a file hosts.txt here you can simply specify the aliases (names defined using the Host keyword in the archivo .ssh/config ) as shown.

# vi hosts.txt 

Add the server aliases.

servidor1
servidor2
servidor3

Run commands via script on multiple Linux servers

Now run the following command pssh specifying the file hosts.txt along with the script that contains various commands to run on various remote servers.

# pssh -h hosts.txt -P -I<./comandos.sh

Meaning of the flags used in the above command:

  • -h – Read the hosts file.
  • -P – Tells pssh to display output as it arrives.
  • -I – Reads input and sends it to each ssh process.

Run multiple commands on remote servers

That's all! In this article, we show how to run multiple commands on multiple servers in Linux. You can share any thoughts related to this topic via the comment section below.

You may also like

Leave a Comment