6/1/2016 - 8:42 AM

Running parallel remote command with pssh

Running parallel remote command with pssh

Method 1:

If you need to run the same command on multiple servers you can use a very useful tool called pssh. You can install it with Homebrew running:

brew install pssh

Once installed you can run the following command to get the current uptime on multiple server:

pssh -h list_of_hosts -l ubuntu -P uptime

Where list_of_hosts is a file containing an host per line:

The option -l let you specify the user to login on the server and -P option let you see the output of the command you're going to run on your console. There are also other arguments you can pass to pssh. Check them with pssh --help.

If you want to login with your public key you cannot pass it to pssh as an option but you have to specify it on your ~.ssh/config file:

Host *
    StrictHostKeyChecking no
    IdentityFile ~/.ssh/ec2.pem

This is my configuration to login in EC2 instances where I have to use my ec2.pem file. The option StrictHostKeyChecking no prevent the failure of the command due to SSH's host verification for known hosts.


Method 2:

pssh  -i  -h list_of_hosts  \
-x "-oStrictHostKeyChecking=no  -i /home/xxx/.ssh/ec2.pem" 'uptime'