async def pinger_loop(self): """ Coroutine that implements the pinger and version checker. """ while not self._pinger_stop.is_set(): await asyncio.sleep(PINGER_CYCLE_TIME) try: async with await asyncio.wait_for(asyncssh.connect( self.sga_host, port=self.sga_port, username='******', password='******', preferred_auth='password', known_hosts=None, login_timeout=10, ), timeout=10) as sga_conn: async with await asyncio.wait_for(asyncssh.connect( self.host, port=self.port, tunnel=sga_conn, username='******', password='******', preferred_auth='password', known_hosts=None, login_timeout=10, ), timeout=10) as conn: uname_result = await asyncio.wait_for(conn.run( 'uname -a', check=True), timeout=10) nvidia_result = await asyncio.wait_for(conn.run( 'cat /usr/libnvidia/version-pdk.txt', check=True), timeout=10) if uname_result.exit_status == 0 and nvidia_result.exit_status == 0: # ping succeeded self._pinger_connected.set() if self._pinger_version_uname.full(): self._pinger_version_uname.get_nowait() await self._pinger_version_uname.put(uname_result.stdout) if self._pinger_version_nvidia.full(): self._pinger_version_nvidia.get_nowait() await self._pinger_version_nvidia.put(nvidia_result.stdout) else: # ping failed self._pinger_connected.clear() except asyncio.exceptions.TimeoutError: self._pinger_connected.clear() except OSError: self._pinger_connected.clear() except Exception as e: log.error(f'WTF HPA ERROR!!! {e.format_exc()}') raise e
async def sftp_copy_from_json(self, input): try: src_path = input.get("src_path") dest_path = input.get("dest_path") src_host = input.get("src_host") src_port = input.get("src_port") src_username = input.get("src_username") src_password = input.get("src_password") dest_host = input.get("dest_host") dest_port = input.get("dest_port") dest_username = input.get("dest_username") dest_password = input.get("dest_password") except: return "Couldn't get all objects" curr_dir = os.getcwd() temp_dir = os.path.join(curr_dir, r'temp_data') os.makedirs(temp_dir) async with asyncssh.connect(host=src_host, port=src_port, username=src_username, password=src_password, known_hosts=None) as conn: async with asyncssh.connect(host=dest_host, port=dest_port, username=dest_username, password=dest_password, tunnel=conn, known_hosts=None) as tunneled_conn: # grab remote file, place in container async with conn.start_sftp_client() as sftp: results = await sftp.get(src_path, temp_dir) spliced_path = src_path.split('/') file_name = spliced_path[len(spliced_path) - 1] # copy grabbed file to desired location async with tunneled_conn.start_sftp_client() as sftp2: results2 = await sftp2.put(temp_dir + "/" + file_name, dest_path) # cleaning up temp file for file in os.listdir(temp_dir): file_path = os.path.join(temp_dir, file) if os.path.isfile(file_path): os.remove(file_path) os.rmdir(temp_dir) return "Successfully Copied File."
async def _establish_connection(self): """Establishing SSH connection to the network device""" logger.info( "Host {}: Establishing connection to port {}".format(self._host, self._port) ) output = "" # initiate SSH connection fut = asyncssh.connect(**self._connect_params_dict) try: self._conn = await asyncio.wait_for(fut, self._timeout) except asyncssh.DisconnectError as e: raise DisconnectError(self._host, e.code, e.reason) except asyncio.TimeoutError: raise TimeoutError(self._host) self._stdin, self._stdout, self._stderr = await self._conn.open_session( term_type="Dumb", term_size=(200, 24) ) logger.info("Host {}: Connection is established".format(self._host)) # Flush unnecessary data delimiters = map(re.escape, type(self)._delimiter_list) delimiters = r"|".join(delimiters) output = await self._read_until_pattern(delimiters) logger.debug( "Host {}: Establish Connection Output: {}".format(self._host, repr(output)) ) return output
def run_client(): with (yield from asyncssh.connect('localhost')) as conn: chan, session = yield from conn.create_session(MySSHClientSession, 'ls abc') yield from chan.wait_closed() yield from conn.wait_closed()
def run_client(): with (yield from asyncssh.connect('localhost')) as conn: chan, session = yield from conn.create_session(MySSHClientSession, 'echo $TERM; stty size', term_type='xterm-color', term_size=(80, 24)) yield from chan.wait_closed()
async def run_client(self, loop): async with asyncssh.connect(host='localhost', port=3509, client_keys="/home/francisco/.ssh/id_rsa", known_hosts=None) as conn: self.chan, session = await conn.create_session(self.SSHClientSession) #i=0 while True: await loop.run_in_executor(executor=None, func=self.dispatch)
async def pinger_loop(self): """ Coroutine that constantly pings SGA to see if it's alive. """ while not self._pinger_stop.is_set(): await asyncio.sleep(PINGER_CYCLE_TIME) try: async with await asyncio.wait_for(asyncssh.connect( self.host, port=self.port, username = '******', password='******', login_timeout=10, preferred_auth='password', known_hosts=None, ), timeout=10) as conn: result = await asyncio.wait_for(conn.run('echo "Test"', check=True), timeout=10) if result.exit_status == 0: # ping succeeded self._pinger_connected.set() else: # ping failed self._pinger_connected.clear() except asyncio.exceptions.TimeoutError: self._pinger_connected.clear() except OSError: self._pinger_connected.clear() except Exception as e: log.error(f'WTF HPA ERROR!!! {e.format_exc()}') raise e
async def ssh_upload_file(event): split_text = event.text.split(None, 1) if len(split_text) == 1: await client.update_message(event, SSH_UPLOAD_HELP) return to_upload = split_text[1] await client.update_message(event, "`Connecting...`") async with asyncssh.connect(str(SSH_HOSTNAME), port=int(SSH_PORT), username=str(SSH_USERNAME), password=str(SSH_PASSWORD), passphrase=str(SSH_PASSPHRASE), client_keys=SSH_KEY, known_hosts=None) as conn: async with conn.create_process( f"test -f {to_upload} && echo 1") as process: stdout, _ = await process.communicate() if stdout: async with conn.start_sftp_client() as ftp: await client.update_message(event, "`Downloading...`") await ftp.get(to_upload, to_upload) await client.send_from_disk(event, to_upload, force_document=True) os.remove(to_upload) else: await client.update_message( event, f"__File Not Found__: `{to_upload}`")
async def prove(self): usernamedic = self.read_file( self.parameter['U']) if 'U' in self.parameter.keys( ) else self.read_file('dict/ssh_usernames.txt') passworddic = self.read_file( self.parameter['P']) if 'P' in self.parameter.keys( ) else self.read_file('dict/ssh_passwords.txt') async for (username, password) in self.generate_dict(usernamedic, passworddic): known_hosts_path = os.path.join(os.path.expanduser('~'), '.ssh', 'known_hosts') if os.path.exists(known_hosts_path): os.remove(known_hosts_path) try: async with asyncssh.connect(host=self.target_host, port=self.target_port, username=username, password=password, known_hosts=None) as conn: self.flag = 1 self.res.append({ "info": username + "/" + password, "key": "ssh burst" }) return except asyncssh.misc.ConnectionLost: pass except: pass
async def run_client(): async with asyncssh.connect('localhost') as conn: async with conn.create_process('bc') as process: for op in ['2+2', '1*2*3*4', '2^32']: process.stdin.write(op + '\n') result = await process.stdout.readline() print(op, '=', result, end='')
async def run_client(): async with asyncssh.connect('localhost') as conn: local_proc = subprocess.Popen(r'echo "1\n2\n3"', shell=True, stdout=subprocess.PIPE) remote_result = await conn.run('tail -r', stdin=local_proc.stdout) print(remote_result.stdout, end='')
async def _init_ssh(self, init_boot_time=True) -> None: await self.ssh_ready.wait() if not self._conn: self.ssh_ready.clear() try: self._conn = await asyncio.wait_for( asyncssh.connect( self.address, port=self.port, known_hosts=None, client_keys=self.pvtkey if self.pvtkey else None, username=self.username, password=self.password if not self.pvtkey else None, ), timeout=self.cmd_timeout, ) self.logger.info( f"Connected to {self.address} at {time.time()}") self.ssh_ready.set() if init_boot_time: await self.init_boot_time() except Exception as e: self.logger.error(f"ERROR: Unable to connect, {str(e)}") self.last_exception = e self._conn = None self.ssh_ready.set() return
async def sync(self, fnames): while True: try: async with asyncssh.connect(self.hostname, username="******", client_keys=[self.media_key], known_hosts=None) as conn: async with conn.start_sftp_client() as sftp: sources = [] for fname in fnames: sources.append( os.path.join(self.media_dir.path, fname)) await sftp.put(sources, "media/") await sftp.remove("media/remove-when-done") except Exception: log.exception("%s: failed to sync, retrying", self.hostname) await asyncio.sleep(1) else: break # Create the .synced file with open(os.path.join(self.media_dir.path, f"{self.hostname}.synced"), "wt"): pass self.sync_task = None
async def openssh(self, inicmd): host = self.host try: async with asyncssh.connect(host['ip'], port=host['port'], username=host['username'], password=host['password'], known_hosts=None, keepalive_interval=3) as conn: chan, session = await conn.create_session( MySSHClientSession, term_type='xterm-color', term_size=(1200, 600)) await session.init(host, self, inicmd + '\n') sftp = await conn.start_sftp_client() chan.write(inicmd + '\n') self.sshs[host['name']] = [chan, sftp] await self.readdir(host['name']) await chan.wait_closed() except Exception as e: print(49, e) await self.send(text_data=json.dumps({ 'action': 'connectfail', 'host': self.host['name'] }))
async def is_in_use(self, config: SSHConfig): async with asyncssh.connect(self.local_ip_address, **config.options()) as conn: result = await conn.run("users") LOGGER.debug(f"Saw '{result.stdout.strip()}' when looking for logged-in users") all_users = set(result.stdout.strip().split()) return not all_users.issubset({'jenkins', ''})
async def exec_process(process, connections, commands, routes): username = process.get_extra_info("username") route = routes.get(username, {}) conn = connections.get(route.get("connection"), {}) commands_list = commands.get(route.get("command"), []) if not route or not conn: # TODO: Say something terrible process.stdout.write( "Configuration error! Please contact your administrator.") process.exit(0) async with connect(conn["host"], conn["port"], username=conn["username"], password=conn["password"]) as conn: result = await conn.run(command=" ; ".join(commands_list) or process.command, encoding=None, term_type=process.get_terminal_type(), term_size=process.get_terminal_size(), stdin=process.stdin, stdout=process.stdout, stderr=process.stderr) process.exit(result.exit_status)
async def download(request): """ :param path: Filepath """ host, port, username, password = parse_headers(request, default_user=None, default_port=22) path = request.query.get('path', '') if not path: raise MissingMandatoryQueryParameter('path') try: async with asyncssh.connect(host, port=port, username=username, password=password, known_hosts=None) as conn: async with conn.start_sftp_client() as sftp: async with sftp.open(path, 'rb') as fp: response = web.StreamResponse() response.content_type = 'application/octet-stream' await response.prepare(request) chunk = await fp.read(CHUNK_SIZE) while chunk: await response.write(chunk) chunk = await fp.read(CHUNK_SIZE) return response except asyncssh.misc.Error as exc: raise AsyncsshError(exc) except OSError: raise ServerUnreachable
async def crawl_host(self, hostname='localhost', path='/', username=None, glob='*.fastq'): ''' Use SSH to crawl a host looking for raw files ''' if username is None: username = getpass.getuser() find_command = f'find -L {path} ! -readable -prune -o -name "{glob}" ' async with asyncssh.connect(hostname, username=username) as conn: result = await conn.run(find_command, check=False) if result.exit_status == 0: files = result.stdout.split("\n") else: raise ValueError(f"Crawl failed: {result.stderr}") # add new files added = 0 for f in files: if not f.startswith('/'): f = path + f added += self.add_raw_file(f, scheme='ssh', username=username, hostname=hostname) self.log.info(f'Found {added} new raw files')
async def _run_ssh_command(self, cmd, username, password) -> tuple: try: async with await asyncio.wait_for(asyncssh.connect( self.server_name, known_hosts=None, username=username, password=password), timeout=5) as conn: try: output = await asyncio.wait_for(conn.run(cmd, check=True), timeout=60) except asyncio.futures.TimeoutError: result = ( False, f'Timed out while running the ssh command {cmd} on {self.server_name}' ) return result result = (True, output.stdout.strip()) except asyncio.futures.TimeoutError: result = ( False, f'Timed out while establishing the ssh connection to {self.server_name}' ) except (OSError, asyncssh.Error) as e: msg = f'SSH connection failed to host {self.server_name}: {(e.__str__())}' result = (False, msg) print(msg) return result
async def remote_random_port(self): """Select unoccupied port on the remote host and return it. If this fails for some reason return `None`.""" username = self.get_remote_user(self.user.name) kf = self.ssh_keyfile.format(username=username) cf = kf + "-cert.pub" k = asyncssh.read_private_key(kf) c = asyncssh.read_certificate(cf) # this needs to be done against remote_host, first time we're calling up async with asyncssh.connect(self.remote_host, username=username, client_keys=[(k, c)], known_hosts=None) as conn: result = await conn.run(self.remote_port_command) stdout = result.stdout stderr = result.stderr retcode = result.exit_status if stdout != b"": ip, port = stdout.split() port = int(port) self.log.debug("ip={} port={}".format(ip, port)) else: ip, port = None, None self.log.error("Failed to get a remote port") self.log.error("STDERR={}".format(stderr)) self.log.debug("EXITSTATUS={}".format(retcode)) return (ip, port)
def run_client(): with (yield from asyncssh.connect('localhost')) as conn: server = yield from conn.start_server(connection_requested, '', 8888, encoding='utf-8') yield from server.wait_closed() yield from conn.wait_closed()
async def connect_ssh(ip, command, username="******", password="******"): print(f"Подключаюсь к {ip}") # asyncssh.misc.KeyExchangeFailed: No matching encryption algorithm found, # sent [email protected],[email protected],aes256-ctr,aes192-ctr,aes128-ctr # and received aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc ssh_coroutine = asyncssh.connect( ip, username=username, password=password, encryption_algs="+aes128-cbc,aes256-cbc", ) # так как нет встроенного таймаута, запускаем через wait_for ssh = await asyncio.wait_for(ssh_coroutine, timeout=10) writer, reader, stderr = await ssh.open_session( term_type="Dumb", term_size=(200, 24) ) output = await reader.readuntil(">") writer.write("enable\n") output = await reader.readuntil("Password") writer.write("cisco\n") output = await reader.readuntil("#") writer.write("terminal length 0\n") output = await reader.readuntil("#") print(f"Отправляю команду {command} на устройство {ip}") writer.write(command + "\n") output = await reader.readuntil([">", "#"]) ssh.close() return output
async def _connect_ssh(hostname: Hostname, *args, **kwargs) -> AsyncIterator[Machine]: """Connect to the given host, retrying if necessary. A pretty simple wrapper around asyncssh.connect, with a couple of changes: - waits for /var/lib/cloud/instance/boot-finished to exist (AWS Ubuntu has this when the machine is ready) - yields a Machine instead of just connections: a nice wrapper of the connection with a hostname - retries until the machine is ready """ reraise_err = None async for attempt in AsyncRetrying(wait=wait_fixed(2)): with attempt: async with asyncssh.connect(hostname, *args, **kwargs) as conn: # SSH may be ready but really the system isn't until this file exists. await conn.run("test -f /var/lib/cloud/instance/boot-finished", check=True) try: yield Machine(conn, Hostname(hostname), kwargs) except BaseException as err: # pylint: disable=broad-except # Exceptions from "yield" have nothing to do with us. # We reraise them below without retrying. reraise_err = err if reraise_err is not None: raise reraise_err from None
async def _loop_body(): # establish a SSH connection. async with asyncssh.connect(host) as conn: cprint(f"[{host:<{L}}] SSH connection established!", attrs=['bold']) while True: if False: #verbose: XXX DEBUG print(f"[{host:<{L}}] querying... ") result = await asyncio.wait_for(conn.run(exec_cmd), timeout=timeout) now = datetime.now().strftime('%Y/%m/%d-%H:%M:%S.%f') if result.exit_status != 0: cprint( f"[{now} [{host:<{L}}] error, exitcode={result.exit_status}", color='red') context.host_set_message( host, colored(f'error, exitcode={result.exit_status}', 'red')) else: if verbose: cprint( f"[{now} [{host:<{L}}] OK from gpustat ({len(result.stdout)} bytes)", color='cyan') # update data context.host_status[host] = result.stdout # wait for a while... await asyncio.sleep(poll_delay)
async def ls(request): """ :param path: (optional) Path to list :param extension: (optional) Filter by extension """ host, port, username, password = parse_headers(request, default_user=None, default_port=22) path = request.query.get('path', '') path = path.rstrip('/') + '/' extension = request.query.get('extension', '') try: async with asyncssh.connect(host, port=port, username=username, password=password, known_hosts=None) as conn: async with conn.start_sftp_client() as sftp: files = [ f'{path}{f}' for f in await sftp.listdir(path) if f not in ('.', '..') and ( not extension or f.endswith(extension)) ] return web.json_response(files) except asyncssh.misc.Error as exc: raise AsyncsshError(exc) except OSError: raise ServerUnreachable
def run_client(): with (yield from asyncssh.connect('localhost')) as conn: listener = yield from conn.forward_local_port('', 0, 'www.google.com', 80) print('Listening on port %s...' % listener.get_port()) yield from listener.wait_closed() yield from conn.wait_closed()
async def _check_conda_env(self, user, path, host): async with asyncssh.connect(host, client_keys=[f'/certs/{user}.key'], username=user) as conn: response = await conn.run( f'ls -la {os.path.join(path, "bin", "jupyter-labhub")}') return response
async def process(self, body): logging.info('SSH plugin process called') username = body.get('user', self.kwargs['global_config']['ssh']['user']) try: async with asyncssh.connect( body['host'], known_hosts=None, username=username, client_keys=[ self.kwargs['global_config']['ssh']['id_rsa_filepath'] ]) as conn: res = await conn.run(body['commands'], check=False) result = res.stdout except Exception as e: logging.error(e) result = 'Execution error' logging.debug('Processing finished') return { "head": '### <code>{}@{} $ {}</code>'.format(username, body['host'], body['commands']), "body": '<pre>\n{}\n</pre>'.format(result) }
async def setup(spawner): username = spawner.user.name remote_host = "corijupyter.nersc.gov" keyfile = "/certs/{username}.key".format( username=username) # NEED to have in NERSCSpawner now certfile = keyfile + "-cert.pub" k = asyncssh.read_private_key(keyfile) c = asyncssh.read_certificate(certfile) try: async with asyncssh.connect(remote_host, username=username, client_keys=[(k, c)], known_hosts=None) as conn: result = await conn.run("myquota -c $HOME") retcode = result.exit_status except: spawner.log.warning( f"Problem connecting to {remote_host} to check quota oh well") retcode = 0 if retcode: from jinja2 import Markup e = web.HTTPError(507, reason="Insufficient Storage") e.jupyterhub_message = Markup( "<br>Your home directory is over quota! " + "Try moving or archiving and deleting some files from there, " + "then come back and try again.<br>") raise e
async def run_client(node): async with asyncssh.connect(node['host'], port=node['port'], username=node['username'], password=node['password']) as conn: result = await conn.run('; '.join(commands), check=True) print(result.stdout, end='')
async def run_client(host): try: async with asyncssh.connect(host, known_hosts=None, username='', password='') as conn: await conn.run('sudo su -c "socat tcp-l:12345 /dev/usbPIRIT0 > /dev/null 2>&1 &"') except Exception as exc: logging.error('{0};{1}'.format(host, exc)) try: reader, writer = await asyncio.open_connection(host, 12345) #('00','') #status #('02','2') #firmware #('11','30,2') #address commands = [('02','2')] response = [] for command in commands: writer.write(message(command[0], command[1])) info = await reader.read(100) info = get_data(info) response.append(info) result = ';'.join(response) logging.info('{0};{1}'.format(host,result)) except Exception as err: logging.error('{0};{1}'.format(host, err)) return
async def run_client( number, name, host, port, username, password, exec_cmd, timeout=30.0, ): host_port = str(number) + ':' + host + ':' + str(port) while True: try: # start SSH connection, or reconnect if it was disconnected async with asyncssh.connect(host=host, port=port, username=username, password=password) as conn: print(f"[{host_port}] SSH connection established!") while True: result = await asyncio.wait_for(conn.run(exec_cmd), timeout=timeout) show_info = result.stdout if result.exit_status == 0 else result.stderr host_status[host_port] = name + f'(port:{port}) ' + \ datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + '\n' + \ ' 显卡序号 温度 利用率 显存占用 使用情况\n' + \ show_info # wait for a while... await asyncio.sleep(interval) except asyncio.CancelledError: print(f"[{host_port}] Closed as being cancelled.") break except (asyncio.TimeoutError): # timeout (retry) print(f"[{host_port}] Timeout after {timeout} sec") host_status[ host_port] = f"[{host_port}] Timeout after {timeout} sec" except (asyncssh.misc.DisconnectError, asyncssh.misc.ChannelOpenError, OSError) as ex: # error or disconnected (retry) print(f"[{host_port}] Disconnected") host_status[host_port] = f"[{host_port}] Disconnected" except Exception as e: # A general exception unhandled, throw print(f"[{host_port}] {type(e).__name__}: {e}") host_status[host_port] = f"{type(e).__name__}: {e}" raise # retry upon timeout/disconnected, etc. print(f"[{host_port}] Disconnected, retrying in {interval} sec...") await asyncio.sleep(interval)
async def run_client(): async with asyncssh.connect('localhost') as conn: server = await conn.start_server(connection_requested, '', 8888, encoding='utf-8') await server.wait_closed()
async def _upload(path, username, password): cfg = _config() async with connect(cfg["ssh"]["host"], username=username, password=password) as conn: target = (conn, cfg["ssh"]["base"] + cfg["path"]) await scp(path / cfg["filename"]["data"], target)
async def run_client(): async with asyncssh.connect(host='localhost', port=3509, client_keys="/home/francisco/.ssh/id_rsa", known_hosts=None) as conn: chan, session = await conn.create_session(SSHClientSession) while True: message = input('> ') chan.write(message+'\n')
async def query_nxos_switches(node, node_ip): log.info(f'Verifying Power_On_Hours on {node} at IP {node_ip}') cat_smartctl = 'cat /mnt/pss/smartctl_full_dump.log ' \ '| tail -n 103 ' \ '| egrep "Power_On_Hours"' mnt_bootflash = 'mount | grep bootflash' sh_uptime = 'show system uptime' try: async with asyncssh.connect(node_ip, username=USR, password=PWD, client_keys=None, known_hosts=None) as conn: poh_result = await conn.run(cat_smartctl, check=True) rw_status_result = await conn.run(mnt_bootflash, check=True) uptime_result = await conn.run(sh_uptime, check=True) pow_on_hrs = int( re.findall(POH_RE, str(poh_result.stdout))[-1][-1]) rw_status = (re.findall(MNT_BF_RE, str(rw_status_result.stdout)))[0] sys_up_time = (re.findall(SYS_UP_RE, str(uptime_result.stdout)))[-1][:2] time_before_fail(node, pow_on_hrs, int(sys_up_time[0]), int(sys_up_time[1]), rw_status) except PermissionDenied as e: log.error(f'Permission denied to SSH to Nexus switch at {node_ip}.' f'Verify credentials.') except TimeoutError as e: log.error(f'Unable to connect to Nexus switch at {node_ip}. ' f'Verify network connectivity.')
def run_client(): with (yield from asyncssh.connect('localhost')) as conn: stdin, stdout, stderr = yield from conn.open_session('bc') for op in ['2+2', '1*2*3*4', '2^32']: stdin.write(op + '\n') result = yield from stdout.readline() print(op, '=', result, end='')
def run_client(): global conn with (yield from asyncssh.connect("localhost")) as conn: listener = yield from conn.create_server(connection_requested, "", 8080) yield from listener.wait_closed() yield from conn.wait_closed()
def run_client(): with (yield from asyncssh.connect('localhost')) as conn: chan, session = yield from conn.create_session(MySSHClientSession, 'env', env={ 'LANG': 'en_GB', 'LC_COLLATE': 'C'}) yield from chan.wait_closed() yield from conn.wait_closed()
async def __aenter__(self): gen = asyncio.wait_for( asyncssh.connect( self.host, port=self.port, username=self.user, **self.connection_kwargs ), timeout=self.connect_timeout, ) self.connection = await gen return self
def run_client(): with (yield from asyncssh.connect('localhost')) as conn: server = yield from conn.create_server(connection_requested, '', 8888, encoding='utf-8') if server: yield from server.wait_closed() else: print('Listener couldn''t be opened.', file=sys.stderr)
async def run_client(): async with asyncssh.connect('localhost') as conn: result = await conn.run('ls abc') if result.exit_status == 0: print(result.stdout, end='') else: print(result.stderr, end='', file=sys.stderr) print('Program exited with status %d' % result.exit_status, file=sys.stderr)
async def run_client(): async with asyncssh.connect('localhost') as conn: chan, session = await conn.create_connection(MySSHTCPSession, 'www.google.com', 80) # By default, TCP connections send and receive bytes chan.write(b'HEAD / HTTP/1.0\r\n\r\n') chan.write_eof() await chan.wait_closed()
async def decorator(ctx, srv, *args, **kwargs): connection = asyncssh.connect( srv.ip, known_hosts=None, username=srv.username, client_keys=[ctx.obj["private_key"]] ) async with connection as open_connection: await func(ctx, srv, open_connection, *args, **kwargs)
def run_client(): with (yield from asyncssh.connect('localhost')) as conn: reader, writer = yield from conn.open_connection('www.google.com', 80) # By default, TCP connections send and receive bytes writer.write(b'HEAD / HTTP/1.0\r\n\r\n') writer.write_eof() # We use sys.stdout.buffer here because we're writing bytes response = yield from reader.read() sys.stdout.buffer.write(response)
def _start_sftp(self): """Open an SFTP client session""" conn = yield from asyncssh.connect(self._server_addr, self._server_port, known_hosts=None, client_keys=['ckey']) sftp = yield from conn.start_sftp_client() return conn, sftp
async def run_client(): async with asyncssh.connect(**ssh_config) as conn: result = await conn.run('echo $TERM; stty size', term_type='xterm-color', term_size=(80, 24)) print(result.stdout, end='') result = await conn.run('whoami') print(result.stdout, end='') result = await conn.run('pwd') print(result.stdout, end='')
async def brcd_nodefind(switch, wwpn): command = 'nodefind ' + wwpn async with asyncssh.connect( switch.ip, username=switch.username, password=switch.password ) as conn: nodefind = await conn.run(command, check=True) if 'No device found' in nodefind.stdout: return None else: fabricshow = await conn.run('fabricshow', check=True) return brcd_analyze(nodefind.stdout, fabricshow.stdout)
async def _check_file(url): #pragma: no cover ''' asyncronously checks a URL based in its scheme ''' # Parse the URL and connect url = urllib.parse.urlparse(url) async with asyncssh.connect( url.hostname, username=url.username) as conn: return await conn.run( f'[[ -f {url.path} ]] && echo -n "Y" || echo -n "N"' )
def run_client(username, password): with (yield from asyncssh.connect( 'localhost', username=username, password=password, known_hosts=None )) as conn: stdin, stdout, stderr = yield from conn.open_session('echo "Hello!"') output = yield from stdout.read() print(output, end='') yield from stdout.channel.wait_closed() status = stdout.channel.get_exit_status() if status: print('Program exited with status %d' % status, file=sys.stderr) else: print('Program exited successfully')
async def get_info(url): current_info = self.get_fileinfo(url) purl = urllib.parse.urlparse(url) async with asyncssh.connect(purl.hostname,username=purl.username) as conn: if current_info.canonical_path is None: readlink = await conn.run(f'readlink -f {purl.path}',check=False) if readlink.exit_status == 0: readlink = readlink.stdout.strip() current_info = current_info._replace(canonical_path=readlink) if current_info.md5 is None: md5sum = await conn.run(f'md5sum {purl.path}',check=False) if md5sum.exit_status == 0: md5sum = md5sum.stdout.strip().split()[0] current_info = current_info._replace(md5=md5sum) if current_info.size is None: size = await conn.run(f'stat -c "%s" {purl.path}',check=False) if size.exit_status == 0: size = int(size.stdout.strip()) current_info = current_info._replace(size=size) return current_info
async def _establish_connection(self): """Establishing SSH connection to the network device""" logger.info('Host {}: Establishing connection to port {}'.format(self._host, self._port)) output = "" # initiate SSH connection fut = asyncssh.connect(**self._connect_params_dict) try: self._conn = await asyncio.wait_for(fut, self._timeout) except asyncssh.DisconnectError as e: raise DisconnectError(self._host, e.code, e.reason) except asyncio.TimeoutError: raise TimeoutError(self._host) self._stdin, self._stdout, self._stderr = await self._conn.open_session(term_type='Dumb', term_size=(200, 24)) logger.info("Host {}: Connection is established".format(self._host)) # Flush unnecessary data delimiters = map(re.escape, type(self)._delimiter_list) delimiters = r"|".join(delimiters) output = await self._read_until_pattern(delimiters) logger.debug("Host {}: Establish Connection Output: {}".format(self._host, repr(output))) return output
def SSHClient(host_tuple, cmdlist): """ Perform SSH client logic """ host, ip = host_tuple # Link-local IPv6 support if ip.startswith('fe80'): # Try to assume eth0 as the interface as a fallback ip = ip+'%'+args.interface if args.interface else ip+"%eth0" try: # Wait at most CONNECT_TIMEOUT seconds for asyncssh.connect() to return with (yield from wait_for(asyncssh.connect(ip, **_SSH_OPTS), CONNECT_TIMEOUT)) as conn: conn.usr = conn.get_extra_info("username") log.warning('[%s:%s] SSH connection initiated', host, conn.usr) for cmd in cmdlist: conn.cmd = cmd.strip() try: # Initiate command execution session = SSHClientSession # Wait at most SESSION_TIMEOUT seconds for session to complete chan, session = yield from wait_for( conn.create_session(session, conn.cmd), SESSION_TIMEOUT) yield from wait_for(chan.wait_closed(), SESSION_TIMEOUT) except AIOTimeout: session.error = "Timeout" finally: if session.error: log.critical('[%s:%s] %s (%s)', host, conn.usr, conn.cmd, session.error) log.critical('[%s:%s] Failure detected, breaking...', host, conn.usr) sessionfailures[host] = (cmd, session.error) break # pylint: disable=lost-exception except (OSError, asyncssh.Error, AIOTimeout) as e: log.error('[%s] SSH connection failed: %s', host, repr(e)) connectfailures[host] = repr(e)
async def crawl_host(self,hostname='localhost',path='/', username=None,glob='*.fastq'): ''' Use SSH to crawl a host looking for raw files ''' if username is None: username = getpass.getuser() find_command = f'find -L {path} ! -readable -prune -o -name "{glob}" ' async with asyncssh.connect(hostname,username=username) as conn: result = await conn.run(find_command,check=False) if result.exit_status == 0: files = result.stdout.split("\n") else: raise ValueError(f"Crawl failed: {result.stderr}") # add new files added = 0 for f in files: if not f.startswith('/'): f = path + f added += self.add_raw_file(f,scheme='ssh',username=username, hostname=hostname) self.log.info(f'Found {added} new raw files')
async def run_client(): async with asyncssh.connect('localhost') as conn: listener = await conn.forward_local_port('', 0, 'www.google.com', 80) print('Listening on port %s...' % listener.get_port()) await listener.wait_closed()
async def run_client(): async with asyncssh.connect("localhost") as conn: chan, session = await conn.create_session(MySSHClientSession, "ls abc") await chan.wait_closed()
def run_client(): with (yield from asyncssh.connect('localhost')) as conn: listener = yield from conn.forward_local_port('', 8080, 'www.google.com', 80) yield from listener.wait_closed()
async def run_client(): async with asyncssh.connect('localhost') as conn: listener = await conn.forward_remote_port('', 8080, 'localhost', 80) await listener.wait_closed()