Script to Send Mass Commands to Multiple Servers


One of the daily tasks as a System Engineer is to send mass commands to multiple servers a cross the fleet, like to yum update the system or just to check the server uptime, to give a couple of examples.

So, It’s important to know that there are many ways to accomplish this, for example using the tools like DSH (Distributed Shell), PSSH (Parallel SSH) or just writing your own Linux Bash Script for sending a command to multiple servers.

In the code down below, I present to you my basic bash script for looping through a list of servers, to send a command and print the output, it’s simple and it works like a charm.

Because of it’s simplicity, It should be self explanatory, but just in case, I added a few comments to make it more clear for you.

The Mass Commands Script Code


#Script name:
#Date 22/03/2021

# It sends a command(s) to a list of servers
# It requires a server list as a text file with 1 IP or Hostname per line
# and the command(s) to be sent to those servers.
# Usage example: ./ myserverlist.txt uptime
# Multiple commands: ./ myserverlist.txt \"command1; command2; command3\"

# Variable to receive the server list file name
# Variable to receive the command or commands

# Conditional to validate that it receives the text file with the servers
if [ "$*" == "" ]; then
 echo "It requires  a serverlist and command: ./ serverlist command";
 echo "for multiple commands use quotes and ; example: \"command1; command2; command3\"  ";
 exit 1

# Looping through the server list and sending your commands
for server in $(cat $serverlist);
 cmdres="$(ssh -qt $USER@$server "$cmd")";
 echo -e "$server $cmdres";


You will be prompted for the user password, on every server, if you don’t have a passwordless access or your SSH KEY installed, you can learn about installing your ssh keys on my tutorial, just click on this link.

The Mass Commands Script uses the current $user that runs the script to login into the remote server, if you need to change it, feel free to modify my Mass Command Linux Bash Script to match your needs.

The Conclusion

There are many ways for sending mass commands to remote Linux servers, they offer you different capabilities like PSSH that is able to use multiple threads to agilize the process, it’s on to you to choose your prefer method.

If you to want learn more about this tutorial or have any questions, feel free to send your comments down below.

Don’t forget to check our other Tutorials, we are constantly submitting new ones every week.

Tags: , , ,

2 Replies to “Script to Send Mass Commands to Multiple Servers”

  1. lon says:

    How to include the source server to query the uptime that not included in the serverlist?
    like my script is saved in machine 1 and my serverlist is machine 2 and machine 3.

    how to get the uptime for machine 1 and include also in results file.

    1. LAG says:


      You have to include “machine” 1 in your list.

Comments are closed.

Buy us a Coffee


Recent Posts

RSS Other Tutorials