Debug School

Cover image for PSSH
Suyash Sambhare
Suyash Sambhare

Posted on

PSSH

PSSH: Run parallel SSH commands

OpenSSH is one of the most widely used and powerful tools available for Linux, which allows you to securely connect to remote Linux systems via a shell and allows you to transfer files securely to and from remote systems.
However one of the major limitations of OpenSSH is that you cannot execute the same command on multiple hosts in one go. You might have to use an automation tool like Ansible to achieve that.
PSSH (Parallel SSH) is a Python-based application, that allows you to execute commands on multiple hosts in parallel at the same time.

Features

PSSH tool includes parallel versions of OpenSSH and related tools such as:

  • pssh – running ssh in parallel on multiple remote hosts.
  • pscp – copying files in parallel to several hosts.
  • prsync – efficiently copying files to multiple hosts in parallel.
  • pnuke – kills processes on multiple remote hosts in parallel.
  • pslurp – copies files from multiple remote hosts to a central host in parallel.

These tools are God for System Administrators who find themselves working with large collections of nodes on a network.

Syntax

pssh [-vAiIP] [-h hosts_file] [-H [user@]host[:port]] [-l user] [-p par] [-o outdir] [-e errdir] [-t timeout] [-O options] [-x args] [-X arg] command ...
pssh -I [-vAiIP] [-h hosts_file] [-H [user@]host[:port]] [-l user] [-p par] [-o outdir] [-e errdir] [-t timeout] [-O options] [-x args] [-X arg] [command ...]

Description

pssh is a program for executing ssh in parallel on several hosts. It provides features such as sending input to all of the processes, passing a password to ssh, saving output to files, and timing out.
The PSSH_NODENUM and PSSH_HOST environment variables are sent to the remote host. The PSSH_NODENUM variable is assigned a unique number for each SSH connection, starting with 0 and counting up. The PSSH_HOST variable is assigned the name of the host as specified in the hosts list. Note that sshd drops environment variables by default, so sshd_config on the remote host must include the line: AcceptEnv PSSH_NODENUM PSSH_HOST

Commands

  • Connect to host1 and host2, and print "hello, world" from each: pssh -i -H "host1 host2" echo "hello, world"
  • Print "hello, world" from each host specified in the file hosts.txt: pssh -i -h hosts.txt echo "hello, world"
  • Run a command as root with a prompt for the root password: pssh -i -h hosts.txt -A -l root echo hi
  • Run a long command without timing out: pssh -i -h hosts.txt -t 0 sleep 10000
  • If the file hosts.txt has a large number of entries, say 100, then the parallelism option may also be set to 100 to ensure that the commands are run concurrently: pssh -i -h hosts.txt -p 100 -t 0 sleep 10000
  • Run a command without checking or saving host keys: pssh -i -H host1 -H host2 -x "-O StrictHostKeyChecking=no -O UserKnownHostsFile=/dev/null -O GlobalKnownHostsFile=/dev/null" echo hi
  • Print the node number for each connection (this will print 0, 1, and 2): pssh -i -H host1 -H host1 -H host2 'echo $PSSH_NODENUM'
  • If you have a set of hosts that you connect to frequently with specific options, it may be helpful to create an alias such as: alias pssh_servers="pssh -h /path/to/server_list.txt -l root -A"
  • The ssh_config file can include an arbitrary number of Host sections. Each host entry specifies ssh options which apply only to the given host. Host definitions can even behave like aliases if the HostName option is included. This ssh feature, in combination with pssh host files, provides a tremendous amount of flexibility.

PSSH

Installation

The pip command is a small program for installing and managing Python software packages index.
On RHEL, install the pip package to install the PSSH program using yum or dnf.

yum install python-pip
dnf install python-pip
pip install pssh

On Ubuntu / Debian
sudo apt install python-pip
sudo pip install pssh

Examples

  1. Make a host file with the hosts along with the IP address and port number that you need to connect to
  2. Follow this format:
vi hostslist
10.1.11.26:22
10.1.11.27:22
Enter fullscreen mode Exit fullscreen mode
  1. Use the previously created host file, including the -h hostslist
  2. Specify the username on all hosts with -l suyi
  3. To show the output use the -i option
  4. Timeout the connections after x seconds. Use the option -t 20
pssh -h hostslist -l suyi -A echo "Test Message"

Warning: do not enter your password if anyone else has a superuser
privileges or access to your account.
Password: 
[1] 17:23:29 [SUCCESS] 10.1.11.26:22
[2] 17:23:30 [SUCCESS] 10.1.11.27:22
Enter fullscreen mode Exit fullscreen mode

To see users currently logged in to servers:

pssh -h hostslits -l suyi -A -i "w"
Warning: do not enter your password if anyone else has a superuser
privileges or access to your account.
Password: 
[1] 12:35:58 [SUCCESS] 10.1.11.26:22
 12:35:58 up 16 days, 20:18,  3 users,  load average: 0.08, 0.14, 0.09
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     seat0    login-           01Jan24  0.00s  0.00s  0.00s /usr/libexec/gdm-wayland-session --register-session gno
root     tty2     tty2             01Jan24 16days  1:23m  0.16s /usr/libexec/tracker-miner-fs
suyi    pts/0    10.1.11.123    12:35    0.00s  0.03s  0.01s w

[2] 12:35:58 [SUCCESS] 10.1.11.27:22
 12:37:31 up 46 days,  1:34,  2 users,  load average: 0.16, 0.13, 0.14
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
suyi    pts/0    login-           12:37    2.00s  0.03s  0.02s w
suyi    pts/1    10.1.11.123    29Dec23 19days  0.14s  0.15s sshd: suyi [priv]
Enter fullscreen mode Exit fullscreen mode

Ref: https://linux.die.net/man/1/pssh

Top comments (0)