def run_command(command, hosts, user, verbose=False, proxy_host=None, timeout=10, **kwargs): """Run ssh command using Parallel SSH.""" result = {"0": [], "1": []} if proxy_host: client = ParallelSSHClient(hosts, user='******', proxy_host=proxy_host, proxy_user=user, timeout=timeout) else: client = ParallelSSHClient(hosts, user=user, timeout=timeout) output = client.run_command(command, stop_on_errors=False, **kwargs) client.join(output) for host in hosts: if host not in output: # Pssh AuthenticationException duplicate output dict key # {'saclay.iot-lab.info': {'exception': ...}, # {'saclay.iot-lab.info_qzhtyxlt': {'exception': ...}} site = next(iter(sorted(output))) raise OpenA8SshAuthenticationException(site) result['0' if output[host]['exit_code'] == 0 else '1'].append(host) if verbose: for host in hosts: # Pssh >= 1.0.0: stdout is None instead of generator object # when you have ConnectionErrorException stdout = output[host].get('stdout') if stdout: for _ in stdout: pass return result
'ec2-54-227-191-220.compute-1.amazonaws.com', 'ec2-54-237-238-149.compute-1.amazonaws.com', ] key_file_path = '/Users/nbarendt/.ssh/eecs397-spring17.pem' priv_key = paramiko.RSAKey.from_private_key_file(key_file_path) master_client = ParallelSSHClient([master_public], pkey=priv_key) slave_client = ParallelSSHClient(slaves, pkey=priv_key) # kill any locust processes print("killing any 'locust' processes in cluster...") master_output = master_client.run_command('killall -q locust', sudo=True) slave_output = slave_client.run_command('killall -q locust', sudo=True) master_client.join(master_output) slave_client.join(slave_output) print(" done") # SFTP latest locust file to all locust instances print("copying latest mqtt-locustfile.py to all cluster instances") master_greenlets = master_client.copy_file('mqtt-locustfile.py', 'mqtt-locust/eecs397-locustfile.py') slave_greenlets = slave_client.copy_file('mqtt-locustfile.py', 'mqtt-locust/eecs397-locustfile.py') joinall(master_greenlets, raise_error=True) joinall(slave_greenlets, raise_error=True) print(" done")
Optimization Area: 1. Handle multiple Server with different login credentials. 2. Handle output if host is not reachable. 3. Connection handling if network fluctuate. ''' import sys import pssh.utils from pssh.pssh_client import ParallelSSHClient from pssh.exceptions import AuthenticationException, \ UnknownHostException, ConnectionErrorException cmd = None USERNAME = "******" PASSWORD = "******" pssh.utils.enable_host_logger() client = ParallelSSHClient(sys.argv[1:], user=USERNAME, password=PASSWORD) while cmd != "exit": cmd = raw_input("$>") try: output = client.run_command(cmd, sudo=True) client.join(output, consume_output=True) except (AuthenticationException, UnknownHostException, ConnectionErrorException): pass del client
from __future__ import print_function from pprint import pprint from pssh.pssh_client import ParallelSSHClient from pssh.exceptions import AuthenticationException, \ UnknownHostException, ConnectionErrorException import pssh.utils pssh.utils.enable_host_logger() hosts = ['vm-dc-js00001-dnguyen.svale.netledger.com'] client = ParallelSSHClient(hosts, proxy_host='nx') try: print("before run_command") output = client.run_command('ls -ltrh /home/mkettlewell', stop_on_errors=False) print("after run_command") client.join(output) print(output) for host in output: for line in output[host]['stdout']: print("Host %s - output: %s" % (host, line)) except (AuthenticationException, UnknownHostException, ConnectionErrorException): print("exception...") pass