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
apt based systems like Debian, Ubuntu, Mint, Kali etc with the following command.
$ sudo apt install pssh -y
Install Pssh For Fedora, CentOS, RHEL
We can install
yumbased systems with the following command .
$ sudo yum install pssh -y
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.
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"
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"
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"
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"