Pssh – Execute Commands On Multiple Remote Linux Servers Using Local System – POFTUT

Pssh – Execute Commands On Multiple Remote Linux Servers Using Local System


ssh  is very useful protocol and tool used by Linux system administrators. It provides easy, secure and fast access to the remote systems and servers. It also provides file copy between local and remote systems securely. But in an enterprise environment we generally need to run single command on multiple remote servers. In this case ssh command can help but it has some restrictions. We can use pssh  or parrallel ssh  to exceed restrictions.

Install Pssh For Debian, Ubuntu, Mint, Kali

We will start by installating  pssh  tool into deb or apt based systems like Debian, Ubuntu, Mint, Kali etc with the following command.

$ sudo apt install pssh -y

Install Pssh For Fedora, CentOS, RHEL

Syntax

We can install pssh into rpm or yumbased systems with the following command .

$ sudo yum install pssh -y
Install Pssh For Fedora, CentOS, RHEL Syntax
Install Pssh For Fedora, CentOS, RHEL Syntax

Remote Host Inventory

As we want to run single command on multiple remote systems the best way to specify remote systems is creating remote host inventory. So we will provide this inventory file to the pssh command. Inventory is just a text file where every remote host we want to run command is listed line by line. The location of the host inventory file is not important because we will specify it by hand. Here is an example host file. We can specify IP address to. Keep in mind that the hostnames should resolved by local or remote dns.

192.168.115.146
db1
app5

Run Given Command On Remote

Now we can use pssh in our local system. We will start with a simple command to run on remote system. We do not need any installation or change on the remote system to run commands with pssh . The command we want to run must be supported on the remote system. In this example we will use remote host inventory file named inventory and run command echo "Hello POFTUT" . The command will run with our local user name on the remote system.

$ pssh -h inventory  -A echo "Hello POFTUT"
Run Given Command On Remote
Run Given Command On Remote

Specify Remote Username

What if we need to use different user on the remote systems. We can specify the user name with the -l  login parameter. In this example we will specify the username root  for remote systems.

$ pssh -h inventory -l root  -A echo "Hello POFTUT"
Specify Remote Username
Specify Remote Username

Print Command Output To Local Terminal

Up to now we have executed commands in the remote systems but do not get any feedback or output from commands. We can use -i interactive option which will bind remote systems command output to our local terminal and print output to our shell.

$ pssh -h inventory -i  -A echo "Hello POFTUT"
Print Command Output To Local Terminal
Print Command Output To Local Terminal

Save Standard Output To A File

In previous example we have printed command output to our local terminal. Another useful option provided by pssh is writing or saving this output into a file without any external command or app. We will use -o  option and provide the path . In this example we will set the output path as comm . For each remote host a text file will be created and the provided output will be saved.

$ pssh -h inventory -o comm  -A echo "Hello POFTUT"
Save Standard Output To A File
Save Standard Output To A File

LEARN MORE  How To Use Rsync Over Ssh In A Secure Manner?

3 thoughts on “Pssh – Execute Commands On Multiple Remote Linux Servers Using Local System”

    • Thanks for contribution. I am definitely with you where Ansible has a lot of plus over pssh but in some cases pssh provides practical or compatible solutions to Linux system administrators.

      Reply
  1. user@fx ~ $ sudo apt install pssh
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    pssh is already the newest version (2.3.1-1).
    0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
    user@fx ~ $ pssh –help
    No command ‘pssh’ found, did you mean:

    I’m on Linux Mint 18.3

    Reply

Leave a Comment