To achieve, this you can use the pssh (parallel ssh) program, a command-line utility for executing ssh in parallel on a number of hosts. With it, you can send input to all of the ssh processes, from a shell script.

 

Requirements:

  • Install Pssh to Run Commands on Multiple Remote Linux Servers
  • You must be using SSH passwordless authentication for all remote servers.
  • Create a Shell Script

 

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

  • Check uptime of servers
  • Check who is logged on and what they are doing
  • List top 5 running processes according to memory usage.

 

First, create a script called commands.sh with your favourite editor.

# vi commands.sh

 

Next, add the following commands to the script as shown.

#!/bin/bash 
###############################################################################
#Script Name    : commands.sh                       
#Description    : execute multiple commands on multiple servers                                                                     
#Author         : Vyga V       
#Email          : vyga@webmail.com 
################################################################################
echo
# show system uptime
uptime
echo
# show who is logged on and what they are doing
who
echo
# show top 5 processe by RAM usage 
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 +x commands.sh

 

Create PSSH Hosts File

 

Next, add the list of servers that you want to run the commands on, in a hosts.txt file, in the format  [user@]host[:port] or simply give the server IP addresses. But we suggest you use ssh aliases which can be specified in .ssh/config file as explained in how to configure custom ssh connections to simplify remote access.

 

This method is more efficient and reliable, it allows you to specify configuration options (such as host name, identify file, port, username etc..) for each remote server.

 

Following is our sample ssh hosts aliases file a.k.a user specific ssh configuration file.

# vi ~/.ssh/config

 

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

# vi hosts.txt 

 

Add the server aliases.

server1
server2
server3

 

Run Commands via a Script on Multiple Linux Servers

 

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

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

 

Meaning of the flags used in the above command:

-h– reads the hosts file.

-P – tells pssh to display output as it arrives.

-I – reads input and sends to each ssh process.

 

Kas see vastus oli kasulik? 0 Kasutajad peavad seda kasulikuks (0 Hääled)