Cyberithub

15 Popular pssh command examples in Linux to perform parallel ssh in a Single Command

In this article, I will take you through 15 Popular pssh command examples in Linux to perform parallel ssh in a single command. PSSH is a very popular command line tool used for parallel SSH Connections in Linux. You can connect multiple servers with different Linux/Unix OS to perform parallel SSH Operations. In many occasions you might have noticed that to gather a simple information or to a run a simple command in multiple nodes, you need to manually login to every nodes and gather the output. By using pssh command you can perform same task in a single command.

In the below examples, I have used three remote hosts with three different OS to show parallel SSH Operations from different local node.

Local Node – 192.168.0.103(RHEL 7)

Remote Host 1 –  192.168.0.105(CentOS 7)

Remote Host 2 – 192.168.0.106(Ubuntu 18.04)

Remote Host 3 – 192.168.0.107(Solaris 11)

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
…]

15 Popular pssh command examples in Linux to perform parallel ssh in a Single Command 1

pssh command examples in Linux to perform parallel ssh

Also Read: How to Install and Setup Freeradius Server in Linux (RHEL/CentOS 7/8) Using 6 Easy Steps

Example 1: How to check date of multiple hosts in a Single Command

If you want to check date of multiple hosts in a single command then you need to put all the nodes in a single file and pass that file name with -h option. In this example, we have put root@192.168.0.105 and test@192.168.0.106 in a file named as hosts and then we are trying to get the output of date command from 192.168.0.105 and 192.168.0.106 using below pssh command.

[root@localhost ~]# pssh -h hosts -i date
[1] 12:51:09 [SUCCESS] root@192.168.0.105
Sunday, June 7, 2020 at 3:50:49 AM IST
[2] 12:51:09 [SUCCESS] test@192.168.0.106
Sat Jun 6 22:21:09 IST 2020

-h : Read hosts from the given host_file. More can be checked on pssh man page.

-i : Display standard output and standard error as each host completes.

NOTE:

Please note that here I am using root user to run all the below commands.You can use any user with sudo access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to the User.

Example 2: How to check the status of successful run of date command

If you want to check the status of successful run of date command in all the nodes present in hosts file then you need to run below command. In this example, we are trying to check the status of successful run of date command in nodes 192.168.0.105 and 192.168.0.106.

[root@localhost ~]# pssh -h hosts date
[1] 12:51:52 [SUCCESS] root@192.168.0.105
[2] 12:51:52 [SUCCESS] test@192.168.0.106

Example 3: How to check the root partition stats of multiple hosts in a Single Command

If you want to check the root partition stats of multiple hosts in a Single command then you need to use below pssh command. In this example we are trying to check root(/) partition usage of nodes 192.168.0.105 and 192.168.0.106 using df -h / command.

[root@localhost ~]# pssh -h hosts -i df -h /
[1] 12:56:47 [SUCCESS] root@192.168.0.105
Filesystem         Size Used Available Capacity Mounted on
rpool/ROOT/solaris 29G  2.7G   23G        11%      /
[2] 12:56:47 [SUCCESS] test@192.168.0.106
Filesystem Size Used Avail Use% Mounted on
/dev/sda1   20G  11G  8.1G 57%     /

Example 4: How to Check uptime of Limited nodes in a Single Command

If you want to check the uptime of limited nodes instead of all nodes in hosts file then you need to specify the node IP using -H option and user name with -l option as shown below. In this example, we are trying to display the uptime of nodes 192.168.0.105 and 192.168.0.107 by running the command uptime through root user as you can see in the below output.

[root@localhost ~]# pssh -H "192.168.0.105 192.168.0.107" -l root -i uptime
[1] 13:05:36 [SUCCESS] 192.168.0.105
4:05am up 2:47, 1 user, load average: 0.05, 0.05, 0.10
[2] 13:05:36 [SUCCESS] 192.168.0.107
13:05:36 up 2 min, 1 user, load average: 0.18, 0.28, 0.13

-H : Add the given host strings to the list of hosts.

-l : Use the given username as the default for any host entries that don’t specifically specify a user.

Example 5: How to check the status of successful run of uptime command in Limited Hosts

If you want to check the status of successful run of uptime command in Limited Hosts then you need to use below pssh command. In this example, we are trying to check the status of successful run of uptime command in nodes 192.168.0.107 and 192.168.0.105 as you can check in the below output.

[root@localhost ~]# pssh -H "192.168.0.105 192.168.0.107" -l root uptime
[1] 13:12:07 [SUCCESS] 192.168.0.107
[2] 13:12:07 [SUCCESS] 192.168.0.105

Example 6: How to prompt for password to run a command in Multiple Hosts

If you want to prompt for a password before running a command in multiple hosts then you need to use -A option as shown below. In this example, we are prompting password for user root to show uptime command output from nodes 192.168.0.105 and 192.168.0.107.

[root@localhost ~]# pssh -A -H "192.168.0.105 192.168.0.107" -l root -i uptime
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 13:13:48 [SUCCESS] 192.168.0.105
4:13am up 2:56, 1 user, load average: 0.04, 0.05, 0.08
[2] 13:13:48 [SUCCESS] 192.168.0.107
13:13:48 up 11 min, 1 user, load average: 0.58, 0.21, 0.12

-A : Prompt for a password and pass it to ssh.

Example 7: How to prompt for password to check the status of successful run of uptime command

If you want to prompt for a password to check the status of successful run of uptime command then you need to use below pssh command. In this example, we are trying to prompt password for user root to show the status of successful run of uptime command in nodes 192.168.0.105 and 192.168.0.107.

[root@localhost ~]# pssh -A -H "192.168.0.105 192.168.0.107" -l root uptime
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 13:14:30 [SUCCESS] 192.168.0.107
[2] 13:14:30 [SUCCESS] 192.168.0.105

Example 8: How to Check pssh command version

If you want to check pssh command version then you need to use pssh --version command as shown below. As you can see from below output current pssh version is 2.3.1.

[root@localhost ~]# pssh --version
2.3.1

–version : display version and exit

Example 9: How to timed out a command after few seconds

If you want to timed out a task running on multiple hosts then you need to use -t option as shown below. In this example, we are trying to timed out a command seq 100000000 > file.txt running through root user after 4 seconds in nodes 192.168.0.105 and 192.168.0.107 as shown in the below output.

[root@localhost ~]# pssh -t 4 -H "192.168.0.105 192.168.0.107" -l root "seq 100000000 > file.txt"
[1] 16:48:24 [FAILURE] 192.168.0.105 Timed out, Killed by signal 9
[2] 16:48:25 [FAILURE] 192.168.0.107 Timed out, Killed by signal 9

-t : Make connections time out after the given number of seconds.

Example 10: How to display Node IP Address of multiple Hosts

If you want to display IP Address of multiple hosts then you need to use below command. In this example, we are trying to display the IP of nodes 192.168.0.105 and 192.168.0.107 by echoing the value of PSSH_NODENUM variable as shown in the below output.

[root@localhost ~]# pssh -i -H 192.168.0.105 -H 192.168.0.107 'echo $PSSH_NODENUM'
[1] 16:53:01 [SUCCESS] 192.168.0.105

[2] 16:53:01 [SUCCESS] 192.168.0.107

Example 11: How to save Standard error to the files in the directory 

If you want to save Standard error to the files in the directory then you need to use -e option as shown below. In this example, we are trying to save Standard error to the files in /root/error/ directory while running seq 100000000 > file.txt command through root User in nodes 192.168.0.105 and 192.168.0.107.

[root@localhost ~]# pssh -e /root/error/ -H "192.168.0.105 192.168.0.107" -l root "seq 100000000 > file.txt"
[1] 17:31:13 [SUCCESS] 192.168.0.107
[2] 17:31:14 [SUCCESS] 192.168.0.105

-e : Save standard error to files in the given directory.

You can now go to /root/error directory and check the files generated in this directory.

[root@localhost ~]# cd /root/error/
[root@localhost error]# ls -lrt
total 8
-rw-r--r-- 1 root root 175 Jun 6 17:18 192.168.0.107
-rw-r--r-- 1 root root 301 Jun 6 17:18 192.168.0.105

NOTE:

Please note that in this example we are saving error files to /root/error/ directory however it is usually recommended to save temporary error files in /tmp path instead of saving in user home directory due to permission issue.

Example 12: How to save standard output to the given files in the directory

If you want to save standard output to the files in a directory then you need to use -o option as shown below. In this example, we are trying to save Standard output to the files in /root/output/ directory while running seq 100000000 > file.txt command through root User in nodes 192.168.0.105 and 192.168.0.107.

[root@localhost ~]# pssh -o /root/output/ -H "192.168.0.105 192.168.0.107" -l root "seq 100000000 > file.txt"
[1] 17:32:08 [SUCCESS] 192.168.0.107
[2] 17:32:09 [SUCCESS] 192.168.0.105

-o : Save standard output to files in the given directory.

You can go to /root/output and check the files created in this directory.

[root@localhost ~]# cd /root/output/
[root@localhost output]# ls -lrt
total 0
-rw-r--r-- 1 root root 0 Jun 6 17:21 192.168.0.105
-rw-r--r-- 1 root root 0 Jun 6 17:21 192.168.0.107

NOTE:

Please note that in this example we are saving output files to /root/output/ directory however it is usually recommended to save temporary output files in /tmp path instead of saving in user home directory due to permission issue.

Example 13: How to use both -o and -e option in Multiple Hosts

If you want to save both the standard output and standard error then you need to use both -o and -e option as shown below. In this example, we are trying to save both Standard output and Standard error to the files in /root/error/ directory while running seq 100000000 > file.txt command through root User for nodes 192.168.0.105 and 192.168.0.107.

[root@localhost output]# pssh -o /root/output/ -e /root/error/ -H "192.168.0.105 192.168.0.107" -l root "seq 100000000 > file.txt"
[1] 17:23:43 [SUCCESS] 192.168.0.107
[2] 17:23:46 [SUCCESS] 192.168.0.105

Example 14: How to use ssh options using -x option in Multiple Hosts

If you want to pass any of the ssh parameter then you need to use -x option as shown below. In this example, we are trying to set ssh parameter VerifyHostKeyDNS=no while trying to run command seq 100000000 > file.txt through root user on hosts 192.168.0.105 and 192.168.0.107.

[root@localhost ~]# pssh -x "VerifyHostKeyDNS=no" -H "192.168.0.105 192.168.0.107" -l root "seq 100000000 > file.txt"
[1] 17:36:01 [SUCCESS] 192.168.0.107
[2] 17:36:03 [SUCCESS] 192.168.0.105

-x : Passes extra SSH command-line arguments.

Example 15: How to check other options of pssh command

If you want to check all the other options available with pssh command then you need to use --help as you can see below.

[root@localhost ~]# pssh --help
Usage: pssh [OPTIONS] command [...]

Options:
--version show program's version number and exit
--help show this help message and exit
-h HOST_FILE, --hosts=HOST_FILE
hosts file (each line "[user@]host[:port]")
-H HOST_STRING, --host=HOST_STRING
additional host entries ("[user@]host[:port]")
-l USER, --user=USER username (OPTIONAL)
-p PAR, --par=PAR max number of parallel threads (OPTIONAL)
-o OUTDIR, --outdir=OUTDIR
output directory for stdout files (OPTIONAL)
-e ERRDIR, --errdir=ERRDIR
output directory for stderr files (OPTIONAL)
-t TIMEOUT, --timeout=TIMEOUT
timeout (secs) (0 = no timeout) per host (OPTIONAL)
-O OPTION, --option=OPTION
SSH option (OPTIONAL)
-v, --verbose turn on warning and diagnostic messages (OPTIONAL)
-A, --askpass Ask for a password (OPTIONAL)
-x ARGS, --extra-args=ARGS
Extra command-line arguments, with processing for
spaces, quotes, and backslashes
-X ARG, --extra-arg=ARG
Extra command-line argument
-i, --inline inline aggregated output and error for each server
--inline-stdout inline standard output for each server
-I, --send-input read from standard input and send as input to ssh
-P, --print print output as we get it

–help : display all available options

 

 

Popular Recommendations:-

15 virt-install examples | KVM Virtualization commands cheatsheet

How to Enable or Disable SELinux Temporarily or Permanently on RedHat/CentOS 7/8

10 Popular Examples of sudo command in Linux(RedHat/CentOS 7/8)

How to Install and Use telnet command in Linux (RedHat/Linux 7/8) using 5 Easy Steps

12 Most Popular rm command in Linux with Examples

Create a Self Signed Certificate using OpenSSL

How to Check timezone in Linux (timedatectl and date commands) Using 4 Easy Methods

How to fix ssh host key verification failed error in Linux (2 Easy Methods)

10 Useful iproute2 tools examples to Manage Network Connections in Linux

Leave a Comment