def get_pssh_client(host, private_key_path, single_copy=False, port=8022, user='******'): if single_copy and isinstance(host, str): logger.debug('Trying single copy') return SSHClient(host, port=port, pkey=private_key_path, user=user) return ParallelSSHClient(host, port=port, pkey=private_key_path, user=user)
def configure(update, context): new_server = context.user_data['new_server'] ip = new_server['ip'] rootpass = new_server['pass'] #check if there's already API running on the server try: r = requests.get('http://{}'.format(ip)).json() if "Hi" in r['message']: update.message.reply_text( "Seems like setup is already done on this server. Now you should pick a server.", reply_markup=choose_server_markup) context.user_data['servers'].append(new_server) return CHOOSE_SERVER except RequestException: pass #check if auth is correct try: client = SSHClient(ip, user='******', password=rootpass) client.run_command('whoami', sudo=True) update.message.reply_text("Auth credentials ok.") except AuthenticationException: update.message.reply_text( "Auth credentials fail. Start-over with /start") return CONFIGURE update.message.reply_text( "Starting fresh server setup, it will take a few minutes...") command = "wget https://raw.githubusercontent.com/dathbezumniy/kmd-sync-api/master/sync_api_setup.sh " \ "&& chmod u+x sync_api_setup.sh && ./sync_api_setup.sh" output = client.run_command(command, sudo=True) #wait until all dependencies downloaded/installed then check if API is up time.sleep(200) try: r = requests.get('http://{}'.format(ip)).json() if "Hi" in r['message']: update.message.reply_text( "Seems like setup is done and API is up. Now you should pick a server.", reply_markup=choose_server_markup) context.user_data['servers'].append(new_server) return CHOOSE_SERVER except RequestException: update.message.reply_text( "Something went wrong. API didn't start, you can try to start over the configuration with /start" ) return CONFIGURE update.message.reply_text( "Something went wrong. API didn't start, you can try to start over the configuration with /start" ) return CONFIGURE
def __init__(self, host, username, password, port=22): """ Create the SSH instance and returns the object :param host: Device IP or Hostname :param username: username :param password: password :param port: ssh port :return: instance of ssh connection """ self.host = host self.username = username self.password = password self.port = port self.device = SSHClient(self.host, user=self.username, password=self.password, port=self.port)
def _create_cluster_ssh_conns(self): hostips = [n["publicIpAddress"] for n in self.cluster_nodes] hostconfigs = [HostConfig(port=n["port"]) for n in self.cluster_nodes] self._all_ssh = ParallelSSHClient(hostips, host_config=hostconfigs, user=self.admin_username) self._master_ssh = ParallelSSHClient(hostips[:1], host_config=hostconfigs[:1], user=self.admin_username) self._master_scp = SSHClient(hostips[0], port=hostconfigs[0].port, user=self.admin_username)
def scp(self, src, dst): """Copy file using Parallel SCP native client.""" result = {"0": [], "1": []} sites = self.groups.keys() for site in sites: try: client = SSHClient(site, user=self.config_ssh['user'], pkey=SSH_KEY, timeout=10) client.copy_file(src, dst) result["0"].append(site) except (SFTPError, UnknownHostError, AuthenticationError, pssh.exceptions.ConnectionError): result["1"].append(site) return _cleanup_result(result)
def send(cmd, pause=0, user=USER): """ Send a command to all bots """ hosts = get_ips() if pause == 0: client = ParallelSSHClient(hosts, user=user) output = client.run_command(cmd) # for host_output in output: # for line in host_output.stdout: # print(line) # exit_code = host_output.exit_code else: for host in hosts: client = SSHClient(host, user=user) output = client.run_command(cmd) # for line in output.stdout: # print(line) # exit_code = output.exit_code time.sleep(pause)
def send(cmd, user=USER, pause=0): droplets = get_servers() hosts = [d.ip_address for d in droplets] if pause == 0: client = ParallelSSHClient(hosts, user=user) output = client.run_command(cmd) # for host_output in output: # for line in host_output.stdout: # print(line) # exit_code = host_output.exit_code else: for host in hosts: client = SSHClient(host, user=user) output = client.run_command(cmd) for line in output.stdout: print(line) exit_code = output.exit_code time.sleep(pause)
def __init__(self, ip, user, password): self.HOST = ip self.USER = user self.PASSWD = password self.last_idle = 0.0 self.last_total = 0.0 print(utils.get_time() + "\tConnecting to " + ip) try: self.client = SSHClient(self.HOST, self.USER, self.PASSWD, timeout=1, num_retries=2) print(utils.get_time() + "\tSession started") self.conn = True except Exception: print(utils.get_time() + "\tSession not stablisehd. Closing") exit(-1)
def _client(self): if self._client_ is None: passwd = self.passwd if self.sshkey_name: pkey = self.sshkey_obj.path if ( self.sshkey_obj and self.sshkey_obj.path) else None if pkey: passwd = self.sshkey_obj.passphrase_ if self.allow_agent: passwd = None pkey = None passwd = None from pssh.clients import SSHClient self._log_debug("ssh connection: %s@%s:%s (passwd:%s,key:%s)" % (self.login, self.addr, self.port, passwd, pkey)) try: self._client_ = SSHClient( host=self.addr, user=self.login, password=passwd, port=self.port, # proxy_pkey=pkey, num_retries=10, allow_agent=self.allow_agent, timeout=self.timeout, retry_delay=1, ) except Exception as e: if str(e).find("Error connecting to host") != -1: msg = e.args[0] % e.args[1:] raise j.exceptions.Base("PSSH:%s" % msg) raise return self._client_
def __init__(self, host, port, username, password=None, pkey=None): from pssh.clients import SSHClient self.client = SSHClient(host=host, user=username, password=pass, port=port, pkey=pkey) self.threads = []
# 1 - Preparing nodes command = "rm -rf * && wget https://raw.githubusercontent.com/tonymorony/komodo-cctools-python/master/scripts/dexp2p/multi-server/prepare_dexp2p_node_ms.sh " \ "&& chmod u+x prepare_dexp2p_node_ms.sh && ./prepare_dexp2p_node_ms.sh" client = ParallelSSHClient(hosts, user="******") output = client.run_command(command, sudo=True) for node in output: for line in output[node]['stdout']: print(line) # 2 - Preparing "started nodes" file on each server i = 0 for host in hosts: print("Preparing file on node " + str(i + 1)) non_parallel_client = SSHClient(host, user="******") if i == 0: non_parallel_client.run_command("touch ip_list") else: line_with_hosts = "" for host in hosts[:i]: line_with_hosts += host + "\n" non_parallel_client.run_command("echo -e " + line_with_hosts + " >> ip_list") i = i + 1 print("Test nodes software prepared. Starting network.") # 3 - Starting network (need to do one by one) i = 0 for host in hosts: print("Starting network on node " + str(i + 1))
from pssh.clients import SSHClient client = SSHClient(host="192.168.56.105", user="******", password="******") ch, host, stdout, stderr, stdin = client.run_command("uptime") for line in stdout: print(line)
from pssh.utils import load_private_key from pssh.clients import SSHClient import yaml with open("host_config.yml") as yaml_file: host_config = yaml.load(yaml_file, Loader=yaml.CLoader) clients = [] for host, info in host_config.items(): client = SSHClient(host=host, port=info["port"], user=info["user"], password=info["password"]) for command in info["commands"]: output = client.run_command(command) clients.append(output) print(clients) for channel, host, stdout, stderr, stdin in clients: for line in stdout: print(host, line)
from pssh.clients import SSHClient host = '192.168.56.101' client = SSHClient(host, user="******", password="******") channel, host, stdout, stderr, stdin = client.run_command('ls /usr/local/src') for line in stdout: print(line)
from pssh.clients import SSHClient client = SSHClient("192.168.56.101", user="******", password="******") client.copy_remote_file('/etc/passwd', 'p.txt')
def connect_to_server(): from pssh.clients import SSHClient conn = SSHClient("192.168.56.101", user="******", password="******") return conn