PSSH - Parallel SSH (Secure Shell)

PSSH is a one the quick and easy tools that I use to execute several basic commands like hostname, ls, python -V, uptime for monitoring, copying and clearing (basic os admin stuff)

When you have a not too large and not too small architectures, these kind of tools comes in hand to build quick tools.

Installing parallel-ssh


In [ ]:
!pip install parallel-ssh

Imports


In [ ]:
from pprint import pprint
from pssh.pssh_client import ParallelSSHClient

List of Hosts


In [ ]:
# stored all the host ips in one file
fname = '/Users/sampathm/devbox/pssh-hosts'

In [ ]:
hosts = open(fname).read().split()
print(hosts)

# # sample output # #
# ['172.16.13.17',
#  '172.16.13.68',
#  '172.16.13.70']

Configuration


In [ ]:
# configure our client to do work
client = ParallelSSHClient(slave_hosts)

# credentials
client.user = 'hadoop-user'
client.password = 'hadoop-user-2q7r1z$3'

Execution


In [ ]:
output = client.run_command('hostname')

for host in output:
    for line in output[host].stdout:
        try:
            print("Host %s \t Output: %s" % (host, line))
        except:
            print("---" * 12, host)