def send_bytes(stream, text, fail_ok=False, expect_prompt=True, prompt=None, timeout=180, send=True): """ Send user specified text to stream. """ LOG.info("Sending text: {}".format(text)) try: if send: stream.sendall("{}\n".format(text).encode('utf-8')) else: stream.sendall("{}".format(text).encode('utf-8')) if expect_prompt: time.sleep(4) if prompt: return expect_bytes(stream, prompt, timeout=timeout, fail_ok=fail_ok) else: rc = expect_bytes(stream, "~$", timeout=timeout, fail_ok=True) if rc != 0: send_bytes(stream, '\n', expect_prompt=False) expect_bytes(stream, 'keystone', timeout=timeout) except streamexpect.ExpectTimeout: if fail_ok: return -1 else: LOG.error("Failed to send text, logging out.") stream.sendall("exit".encode('utf-8')) raise except Exception as e: LOG.info("Connection failed with {}.".format(e)) raise return 0
def send_bytes(stream, text, fail_ok=False, expect_prompt=True, prompt=None, timeout=180, send=True, flush=True): """ Send user specified text to stream. """ time.sleep(1) if flush: try: incoming = stream.poll(1) # flush input buffers if incoming: incoming += b'\n' try: LOG.info(">>> send_bytes: Buffer has bytes!") stdout.write( incoming.decode('utf-8')) # streamexpect hardcodes it except Exception: pass except streamexpect.ExpectTimeout: pass LOG.info("Sending text: %s", text) try: if send: stream.sendall("{}\n".format(text).encode('utf-8')) else: stream.sendall("{}".format(text).encode('utf-8')) if expect_prompt: time.sleep(1) if prompt: return expect_bytes(stream, prompt, timeout=timeout, fail_ok=fail_ok) else: rc = expect_bytes(stream, "~$", timeout=timeout, fail_ok=True) if rc != 0: send_bytes(stream, '\n', expect_prompt=False) expect_bytes(stream, 'keystone', timeout=timeout) return except streamexpect.ExpectTimeout: if fail_ok: return -1 else: LOG.error("Failed to send text, logging out.") stream.sendall("exit".encode('utf-8')) raise except Exception as e: LOG.info("Connection failed with %s.", e) raise return 0
def expect_bytes(stream, text, timeout=180, fail_ok=False, flush=True): """ Wait for user specified text from stream. """ time.sleep(1) if timeout < 60: LOG.info("Expecting text within %s seconds: %s\n", timeout, text) else: LOG.info("Expecting text within %s minutes: %s\n", timeout / 60, text) try: stream.expect_bytes("{}".format(text).encode('utf-8'), timeout=timeout) except streamexpect.ExpectTimeout: if fail_ok: return -1 else: stdout.write('\n') LOG.error("Did not find expected text") # disconnect(stream) raise except Exception as e: LOG.info("Connection failed with %s", e) raise stdout.write('\n') LOG.info("Found expected text: %s", text) time.sleep(1) if flush: try: incoming = stream.poll(1) # flush input buffers if incoming: incoming += b'\n' try: LOG.info(">>> expect_bytes: Buffer has bytes!") stdout.write( incoming.decode('utf-8')) # streamexpect hardcodes it except Exception: pass except streamexpect.ExpectTimeout: pass return 0
def get_dir(source, remote_host, destination, patch=False, setup=False): """ get directory contents from remote server Note: does not get nested directories only files. args: - source: full path to directory e.g. /localhost/loadbuild/jenkins/CGCS_5.0_Host/latest_build/ - Remote host: name of host to log into, controller-0 by default e.g. yow-cgts4-lx.wrs.com - destination: where to store the files locally: e.g. /tmp/files/ """ username = '******' if platform == 'win32' or platform == 'win64': privatekeyfile = os.path.expanduser('C:\\Users\\{}\\.ssh\\'.format(username)) pass else: privatekeyfile = os.path.expanduser('~/.ssh/id_rsa') #mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile) LOG.info("Connecting to server {} with username {}".format(remote_host, username)) ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.connect(remote_host, username=username) sftp_client = ssh_client.open_sftp() LOG.info(sftp_client.listdir(source)) path = '' for items in sftp_client.listdir(source): path = source+items local_path = destination + items try: if patch: if path.endswith('.patch'): LOG.info("Sending file from {} to {}".format(path, local_path)) sftp_client.get(path, local_path) else: LOG.info('Sending {} to {}'.format(path, local_path)) sftp_client.get(path, local_path) except IOError: LOG.error("Cannot transfer {}".format(path)) LOG.info("Done") sftp_client.close() ssh_client.close()
def expect_bytes(stream, text, timeout=180, fail_ok=False): """ Wait for user specified text from stream. """ time.sleep(2) if timeout < 60: LOG.info("Expecting text within {} seconds: {}\n".format(timeout, text)) else: LOG.info("Expecting text within {} minutes: {}\n".format((timeout/60), text)) try: stream.expect_bytes("{}".format(text).encode('utf-8'), timeout=timeout) except streamexpect.ExpectTimeout: if fail_ok: return -1 else: LOG.error("Did not find expected text") # disconnect(stream) raise except Exception as e: LOG.info("Connection failed with {}.".format(e)) raise LOG.info("Found expected text: {}".format(text)) return 0