Guide Area

Execute Commands on Multiple Remote Servers over SSH

If you are a sysadmin, this probably happened to you several times: You have multiple servers (often tens of IPs) where you need to execute specific command. For instance, you would like to add one user’s pubkey to a list of authorized keys on the servers. But the idea of connecting to all of those servers manually and executing the command over and over again makes you wanna throw up. Worry no more. There is a tool called PSSH which allows you to execute commands on multiple remote servers over SSH.

In this tutorial I will show you how to do it. The process is fairly simple, but I will also include a table of available arguments which are available while using the tool.

Installation

I will not describe installation process for all possible linux distributions. If you’r distro is not covered here, try to google for installation steps for your linux. On debian alternatives (Debian, Ubuntu, etc.), PSSH is installed via Python’s pip package manager. I assume that you have Python installed. Version 2.7 usually comes with a standard installation of the OS. So all you have to do is to install pip for python and then pssh:

Install pip – method #1

If you want to install pip for system Python version (the one installed by default), install pip by executing command:

sudo apt-get install python-pip
Install pip – method #2

If you want to install pip for another version, installed by you after installation of your OS, follow these steps:

cd /tmp/
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

 

[notification type=”notification_warning” ]Change the python command to your specified Python version, f.e. ‘python3.4 get-pip.py'[/notification] [notification type=”notification_warning” ]From official pip website:
Be cautious if you are using a Python install that is managed by your operating system or another package manager. get-pip.py does not coordinate with those tools, and may leave your system in an inconsistent state.[/notification]
Install PSSH

Installing PSSH is fairly simple. You install it either for system Python, or your custom version. Execute one of the commands below based on your situation.

# If you're using system Python
sudo pip install

# If you're using custom Python version
python3.4 -m pip install pssh

To verify the installation, execute this command:

whereis pssh

You should see something like:

pssh: /usr/local/bin/pssh

Usage

PSSH takes a file with list of hosts as an argument. Each of your hosts needs to be added to this file, separated by a new line. So for example, your file could look something like this:

88.212.244.43
88.212.244.44
88.212.244.45
88.212.244.46
88.212.244.47
88.212.244.48

Save this file with any name, f.e. “pssh_hosts” (no postfix necessary). Then, prepare you command. For instance, we might want to export an environmental variable on each of those servers. The command would look like this:

export TESTENV="/usr/bin"

And now the whole, PSSH command will look like this:

pssh -h pssh_hosts -l username -A export TESTENV="/usr/bin"

Where -h (or –hosts) is the location of the hosts file, -l (or –user (I know, doesn’t make sense)) is for specifying username and -A (or –askpass) will force PSSH to ask you for login password, in case your pubkey is not in the authorized_keys file on all of the servers. After this is done, the result should be looking like this:

[1] 08:34:07 [SUCCCESS] 88.212.244.43:22
[2] 08:34:07 [SUCCCESS] 88.212.244.44:22
[3] 08:34:08 [SUCCCESS] 88.212.244.45:22
[4] 08:34:08 [SUCCCESS] 88.212.244.46:22
[5] 08:34:08 [SUCCCESS] 88.212.244.47:22
[6] 08:34:09 [SUCCCESS] 88.212.244.48:22

Arguments

-h / --hosts – File with a list of servers
-H / --host – One specific host
-l / --user – User used when connecting to server
-p / --par – Number of concurrent operations
-t / --timeout – Time connection out after x seconds (0 = no timeout)
-o / --outdir – Save standard output to files in outdir
-e / --errdir – Save error logs to files in errdir
-x / --extra-args – Special SSH arguments
-X / --extra-arg – One special SSH argument
-o / --options – Specific SSH options from SSS config file
-A / --askpass – Ask user to enter password when connecting to server
-i / --inline – Display standard output in cmd
--inline-stdout – Display only stdout (not sterr) in cmd
-v / --verbose – Debugging mode
-I / --send-input – Read input and send to each SSH process

All arguments are copied from PSSH home page https://linux.die.net/man/1/pssh. If you have any questions, either browse the home page, or leave a comment and I will try to help. I hope this will help you to save some time.

Vladimir Marton

DevOps Engineer focused on cloud infrastructure, automation, CI/CD and programming in Javascript, Python, PHP and SQL. Guidearea is my oldest project where I write articles about programming, marketing, SEO and others.