Data Centers

How to run remote commands on multiple Linux servers with Parallel-SSH

There's a command line tool for running multiple commands on multiple Linux servers at once. Jack Wallen shows you how with Parallel-SSH.

Image: Jack Wallen

If you're an administrator of multiple Linux machines in your data center, you probably find yourself remoting into multiple machines every day to run daily admin tasks, such as gathering information on disk usage or running processes. What if you could log into a single machine and send those commands to remote machines, all from a single source?

A handy tool called Parallel-SSH makes that possible. With Parallel-SSH, you can run a list of commands on a list of Linux machines at once, which could be a boon for admins working with numerous data center servers.

But how do you use this special kind of magic? I'm going to demonstrate.

SEE: Data center automation research report 2018: Despite growth in data, automation adoption remains slow (Tech Pro Research)

I'll illustrate how to install and use Parallel-SSH on the Ubuntu Server 18.04 platform. The tool is available from the standard repository for most distributions, so you only need to alter the install tool to match your Linux flavor of choice. With that said, let's install and use Parallel-SSH.

Installation

The installation of Parallel-SSH can be completed with a single command. Open a terminal window and issue the command:

sudo apt-get install pssh

Once the installation is complete, you're ready to go.

Configure the commands

Instead of running the necessary commands individually, we're going to create a single file that contains all of the command we want to run on our remote servers. Issue the command:

sudo nano ~/pssh-commands 

within that file, copy the following:

#!/bin/bash

echo
# show system uptime
uptime
echo
# show who is logged on and what they are doing
who
echo
# show top 5 processes by RAM usage
ps -eo cmd,pid,ppid,%mem,%cpu --sort=-%mem | head -n 6

exit 0

Save and close that file.

Note: You can add whatever commands you need in the above file (the above is an example).

Creating hosts file

Next, we need to create a hosts file. Issue the command:

nano ~/pssh-hosts

In that file, the hosts will be listed, one per line (add as many as you need), in the form user@IP, like so:

jack@192.168.1.162
jack@192.168.1.221

Save and close that file.

Running Parallel-SSH

Now we're going to run the command. We have our commands and hosts files ready, so issue the following command to use those files:

parallel-ssh -h pssh-hosts -A -P -I < pssh-command

The above options are as follows:

  • -h - hostname (in this case, we're using a host file)
  • -A asks for a password
  • -P print output as it arrives
  • -I read input and send to each ssh process

You will be prompted for the password associated with user configured in pssh-hosts. Once authenticated, you should see successful output from the commands on the remote machines (Figure A).

Figure A

Figure A

Output from our successful Parallel-SSH command.


You can scroll through the output of the command to glean information about your servers.

The caveat

You knew the caveat was coming. One of the only problems you'll find with Parallel-SSH is its inability to run commands using sudo. This isn't a problem if you have a distribution that makes use of the root user, but even then you probably have the root user locked out of ssh login, so it's still becomes an issue. In other words, you won't run commands like sudo apt-get upgrade with Parallel-SSH. That's fine, as you most likely wouldn't want to make use of a tool like this to run commands that require root or sudo privileges. Instead, you can create various command files that handle different tasks (such as gathering networking information, disk information, process information, etc.).

Even with this caveat, Parallel-SSH is still an incredibly useful tool.

Also see

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks

Free Newsletters, In your Inbox