How To Send Remote Commands to multiple servers With PSSH
Been a Linux System Administrator requires to handle a fleet of servers, it is important to know how to send remote commands to multiple servers with PSSH.
PSSH, parallel ssh program, is a command line tool for executing ssh in parallel on a number of 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.
Install pssh on CentOS, RHEL or Fedora
Run the package installer, yum for these distros or whatever yours have.
$ sudo yum install pssh
Example of the output:
[root@localhost ~]# yum install pssh Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile base: centos.excellmedia.net epel: repos.del.extreme-ix.org extras: centos.excellmedia.net openstack-rocky: centos.excellmedia.net rdo-qemu-ev: centos.excellmedia.net updates: centos.excellmedia.net Resolving Dependencies --> Running transaction check ---> Package pssh.noarch 0:2.3.1-5.el7 will be installed --> Finished Dependency Resolution
Examples of use pssh to send commands to multiple servers
It is recommend that you set up ssh keys for automation and not need to input your password. To generate your key pair for password less authentication follow this tutorial on SSH KEYS.
Once you have setup your ssh keys or not, let’s see how pssh works with the most basic example:
$ pssh -H 'test@192.168.0.102' date
Example output:
[root@localhost ~]# pssh -H 'root@192.168.0.1' date
[1] 12:38:17 [SUCCESS] root@192.168.0.1
To take a real advantage of using pssh I recommend you to include the option “-h server_list” with all the servers that you would like to send a command to.
And also use the “-p NUMBER” option to run parallel threads and finish your task faster.
First, make your server_list with this syntax [user@]host[:port] put just one per line, for example:
$ cat server_list root@192.168.0.1 root@192.168.0.2 root@192.168.0.3 root@192.168.0.4
If we want to run 4 process in parallel we run the pssh command like this:
$ pssh -p 4 -h server_list -i date
The output will be something like this:
[1] 16:15:15 [SUCCESS] root@192.168.0.1
Sun Feb 10 16:15:15 IST 2021
[2] 16:15:15 [SUCCESS] root@192.168.0.2
Sun Feb 10 16:15:15 IST 2021
[3] 16:15:15 [SUCCESS] root@192.168.0.3
Sun Feb 10 16:15:15 IST 2021
[4] 16:15:15 [SUCCESS] root@192.168.0.4
Sun Feb 10 16:15:15 IST 2021
This is a list of the most used options:
-P, --print print output as we get it
-p PAR, --par=PAR max number of parallel threads.
-h HOST_FILE, --hosts=HOST_FILE
-l USER, --user=USER username.
-i, --inline inline aggregated output and error for each server.
-I, --send-input read from standard input and send as input to ssh.
-o OUTDIR, --outdir=OUTDIR Save standard output to files in the given directory.
-t, --timeout timeout Make connections time out after the given number of seconds.
Another good idea is to use pssh to update your servers on demand, you can group your server per distro and to update your CentOS servers use:
$ pssh -p 4 -h centos_list -i "sudo yum -y update"
At last, you may wonder how to send multiple commands to multiple servers using pssh, here is an example:
lag@core~$ pssh -h testserver -i "date; uptime"
[1] 20:40:10 [SUCCESS] root@testserver
Wed Mar 31 14:40:10 EDT 2021
14:40:10 up 21 days, 13:54, 0 users, load average: 0.64, 0.29, 0.28
Conclusion
Pssh is a versatile tool for any Linux System Administrator, it’s major advantage against just using ssh command is the parallel processing that makes your work easier.
If you to want learn more about this tutorial or have any questions, feel free to send your comments down below.
Don’t forget to check our other Tutorials, we are constantly submitting new ones every week.
Tags: commands to multiple servers pssh, how to pssh, use pssh, using pssh multiple server