There's a command line tool for running multiple commands on multiple Linux servers at once. Jack Wallen shows you how with Parallel-SSH.
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.
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.
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:
In that file, the hosts will be listed, one per line (add as many as you need), in the form user@IP, like so:
Save and close that file.
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).
You can scroll through the output of the command to glean information about your servers.
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.
- How to install the Ansible server management system on Ubuntu (TechRepublic)
- How to install the Icinga2 Monitoring tool on Ubuntu Server 16.04 (TechRepublic)
- How to install Rundeck local and remote services controller on Ubuntu 18.04 (TechRepublic)
- How to install and run the Maltrail detection system on Ubuntu Server 18.04 (TechRepublic)
- Data center automation: 2 acronyms you should know (ZDNet)
- Ubuntu Linux 18.10 arrives (ZDNet)