PSSH – Execute commands on multiple Linux servers in parallel

By default everyone prefers openSSH to connect remote Linux servers. OpenSSH is one of the best tool to connect remote server securely and doing the job perfectly.

But it doesn’t have the features to execute commands on multiple Linux servers simultaneously, this is where Parallel SSH came in  to picture.

PSSH stands for Parallel SSH ,a beautiful slim utility, which is capable to run commands on multiple Linux servers simultaneously with minimal system load on the client host.

It’s written in Python language.

Parallel SSH is very useful to push security patches to Linux servers, when you have small environment (10-30 Servers), otherwise you have to login one by one server to perform the patch update.

Not only for patch update and useful for many other activities.

Suggested Read : rtop – A Nifty Tool to Monitor Remote Server Over SSH

ParallelSSH comes with bunch of below useful utilities.

  • pssh – Parallel ssh : To execute commands on multiple Linux servers simultaneously
  • pscp – Parallel scp : To copy files on multiple Linux servers simultaneously
  • prsync – Parallel rsync : Perform incremental copy on multiple Linux servers in parallel
  • pnuke – Parallel nuke : kills processes on multiple Linux servers in parallel
  • pslurp – Parallel slurp

Installing  PSSH in Linux through Package Manager:

All the major Linux distributions has included the PSSH package in default repository, we can easily install PSSH with help of distribution package manager.

[Install PSSH on Debian/Ubuntu/Mint]
$ sudo apt-get install pssh

[Install PSSH on Fedora]
$ sudo dnf install pssh

[Install PSSH on openSUSE]
$ sudo zypper install pssh

Enable yaourt or packer in order to install ParallelSSH to Arch Linux based systems.

$ yaourt -S pssh
or
$ packer -S pssh

Enable EPEL Repository in order to install ParallelSSH to RHEL/CentOS based systems.

$ sudo yum install pssh

Install PSSH in Linux through pip

pip is a recommended tool for installing Python packages in Linux. Use pip command instead of package manager to get latest

build.

[Install pip on Debian/Ubuntu/Mint]
$ sudo apt-get install python-pip

[Install pip on RHEL/CentOS]
$ sudo yum install python-pip

[Install pip on Fedora]
$ sudo dnf install python-pip

[Install pip on openSUSE]
$ sudo zypper install python-pip

[Install pip on Arch Linux based system]
$ sudo pacman -S python-pip

Finally run the pip tool to install ParallelSSH on Linux.

$ sudo pip install pssh

How to use pssh command ?

There is no big deal to use pssh, even non technical guys can easily handle the utility. Just create a text file and add hosts (each one in separate line) one by one (you can add only IP or IP+UserName or IP+UserName+SSH Port Number (Optional because, it’s taking the port info from SSH)).

To play on this, I have created a file called pssh-hosts-test with following details.

$ nano pssh-hosts-test
192.168.1.101
192.168.1.102
192.168.1.103

To push updates on Debian base, just use the following command.

$ pssh --hosts pssh-hosts-test --user root -i -A "apt install update && apt install upgrade"

Run the uptime command on multiple Linux servers.

$ pssh --hosts pssh-hosts-test --user root -i -A "uptime"
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 09:50:37 [SUCCESS] 192.168.1.101
 09:50:37 up 214 days, 15:31,  1 user,  load average: 0.00, 0.01, 0.05
[2] 09:50:37 [SUCCESS] 192.168.1.102
 09:50:56 up 39 days, 15:51,  1 user,  load average: 2.30, 2.13, 1.17
[3] 09:50:37 [SUCCESS] 192.168.1.103
 09:50:42 up 298 days, 15:03,  0 users,  load average: 1.07, 1.29, 0.86

Run the date command on multiple Linux servers.

$ pssh --hosts pssh-hosts-test --user root -i -A "date"
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 09:50:48 [SUCCESS] 192.168.1.101
Wed Mar 22 09:50:48 IST 2017
[2] 09:50:48 [SUCCESS] 192.168.1.102
Wed Mar 22 09:51:07 IST 2017
[3] 09:50:48 [SUCCESS] 192.168.1.103
Wed Mar 22 09:50:53 IST 2017

Run the w command on multiple Linux servers.

$ pssh --hosts pssh-hosts-test --user root -i -A "w"
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 09:50:31 [SUCCESS] 192.168.1.101
 09:50:31 up 214 days, 15:31,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    vps1.2daygeek.com 22Mar17  2:55   0.28s  0.28s -bash
[2] 09:50:31 [SUCCESS] 192.168.1.102
 09:50:50 up 39 days, 15:51,  1 user,  load average: 2.42, 2.15, 1.17
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    vps2.2daygeek.com 12Mar17  2:42   0.31s  0.31s -bash
[3] 09:50:31 [SUCCESS] 192.168.1.103
 09:50:36 up 298 days, 15:03,  0 users,  load average: 1.16, 1.31, 0.86
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    vps3.2daygeek.com 13Mar17  2:42   0.31s  0.31s -bash

Run the df -h command on multiple Linux servers.

$ pssh --hosts pssh-hosts-test --user root -i -A "df -h"
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 09:49:37 [SUCCESS] 192.168.1.101
Filesystem        Type   Size  Used Avail Use% Mounted on
/dev/ploop43022p1 ext4    11G  1.1G  9.2G  11% /
none              tmpfs  4.0K     0  4.0K   0% /sys/fs/cgroup
none              tmpfs  103M   68K  103M   1% /run
none              tmpfs  512M     0  512M   0% /dev/shm
none              tmpfs  5.0M     0  5.0M   0% /run/lock
none              tmpfs  512M     0  512M   0% /run/shm
none              tmpfs  100M     0  100M   0% /run/user
[2] 09:50:31 [SUCCESS] 192.168.1.102
Filesystem      Size  Used Avail Use% Mounted on
udev            979M     0  979M   0% /dev
tmpfs           200M  6.3M  194M   4% /run
/dev/sda1        38G  7.6G   28G  22% /
tmpfs           999M   14M  986M   2% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           999M     0  999M   0% /sys/fs/cgroup
tmpfs           200M  120K  200M   1% /run/user/1000
[3] 09:50:31 [SUCCESS] 192.168.1.103
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 990M     0  990M   0% /dev
tmpfs                   1001M     0 1001M   0% /dev/shm
tmpfs                   1001M  1.4M 1000M   1% /run
tmpfs                   1001M     0 1001M   0% /sys/fs/cgroup
/dev/mapper/fedora-root   27G  7.2G   18G  29% /
tmpfs                   1001M     0 1001M   0% /tmp
/dev/sda1                976M  143M  767M  16% /boot
tmpfs                    201M   12K  201M   1% /run/user/42
tmpfs                    201M   28K  201M   1% /run/user/1000

I have a shell script at /opt/login-info.sh which used to pull the date wise login report.

$ pssh --hosts pssh-hosts-test --user root -i -A "/opt/login-info.sh"
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 09:49:37 [SUCCESS] 192.168.1.101
Mar 22 01:12:27 vps1.2daygeek.com sshd[50310]: Accepted password for magi from 10.0.2.15 port 42550 ssh2
Mar 22 01:12:27 vps1.2daygeek.com sshd[10350]: pam_unix(sshd:session): session opened for user magi by (uid=0)
Mar 22 07:08:11 vps1.2daygeek.com sshd[35100]: pam_unix(sshd:session): session closed for user magi
[2] 09:49:37 [SUCCESS] 192.168.1.102
Mar 22 07:12:27 vps2.2daygeek.com sshd[27320]: Accepted password for magi from 10.0.2.15 port 46640 ssh2
Mar 22 07:12:27 vps2.2daygeek.com sshd[72320]: pam_unix(sshd:session): session opened for user magi by (uid=0)
[3] 09:49:37 [FAILURE] 192.168.1.103 Exited with error code 127
Stderr: bash: /opt/login-info.sh: No such file or directory

To print the /home directory files & folder.

$ pssh --hosts pssh-hosts-test --user root -i -A "ls -lh /home"

To run Multiple commands, use the following format.

$ pssh --hosts pssh-hosts-test --user root -i -A "uptime; w;"
or
$ pssh --hosts pssh-hosts-test --user root -i -A "uptime && w"

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

2 Comments on “PSSH – Execute commands on multiple Linux servers in parallel”

Leave a Reply

Your email address will not be published. Required fields are marked *