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.
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]
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:
You should see something like:
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:
18.104.22.168 22.214.171.124 126.96.36.199 188.8.131.52 184.108.40.206 220.127.116.11
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:
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:
 08:34:07 [SUCCCESS] 18.104.22.168:22  08:34:07 [SUCCCESS] 22.214.171.124:22  08:34:08 [SUCCCESS] 126.96.36.199:22  08:34:08 [SUCCCESS] 188.8.131.52:22  08:34:08 [SUCCCESS] 184.108.40.206:22  08:34:09 [SUCCCESS] 220.127.116.11:22
-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.