def read_config(): try: with io.open(ACCOUNT_FILE, 'r', encoding='utf-8') as configfile: config_dict = utils.json_load(configfile.read()) config = Config(config_dict) if config.is_valid(): return config except Exception as e: logger.debug(e) return None
def scp_pexpect2(account, targets, jumps): import pexpect jump1 = jumps.pop(0) port = find_available_port() maps = '%s:%s:%s:%s' % (LOCALHOST, port, account.host, account.port) forwards = Forwards(maps, '') # Establish port forwarding vias = ','.join([a.name for a in reversed(jumps)]) ret = _ssh_pexpect3(jump1, vias=vias, forwards=forwards) if ret['status'] == 'fail': return ret forwarding = ret['p'] src, dst = targets.src.compile(host=LOCALHOST), \ targets.dst.compile(host=LOCALHOST) if account.identity: command = _SCP_COMMAND_IDENTITY.format(jump='', port=port, src=src, dst=dst, identity=account.identity) else: command = _SCP_COMMAND_PASSWORD.format(jump='', port=port, src=src, dst=dst) try: logger.debug(command) p = pexpect.spawn(command) # Password for dest host if not account.identity: p.expect([pexpect.TIMEOUT, '[p|P]assword:']) p.sendline(account.password) set_winsize(p) # Adjust window size # Set auto-adjust window size signal.signal(signal.SIGWINCH, sigwinch_passthrough(p)) p.interact() except Exception as e: logger.debug(e) return { 'status': 'fail', 'msg': 'Connection failed', }
def check_init(): flags = os.path.isdir(CONFIG_DIR) and os.path.isfile(ACCOUNT_FILE) if flags: try: config = read_config() if config: return STATUS_INITED else: return STATUS_BROKEN except Exception as e: logger.debug(e) return STATUS_UNINIT
def ssh_pexpect2(account, vias=None, forwards=None): import pexpect jump, passwords = compile_jumps(account, vias=vias) _forwards = forwards.compile() if forwards else '' if account.identity: command = _SSH_COMMAND_IDENTITY.format(jump=jump, forwards=_forwards, user=account.user, host=account.host, port=account.port, identity=account.identity) else: command = _SSH_COMMAND_PASSWORD.format(jump=jump, forwards=_forwards, user=account.user, host=account.host, port=account.port) try: logger.debug(command) p = pexpect.spawn(command) # Passwords for jump hosts if passwords: for password in passwords: p.expect([pexpect.TIMEOUT, '[p|P]assword:']) p.sendline(password) # Password for dest host if not account.identity: p.expect([pexpect.TIMEOUT, '[p|P]assword:']) p.sendline(account.password) set_winsize(p) # Adjust window size # Set auto-adjust window size signal.signal(signal.SIGWINCH, sigwinch_passthrough(p)) # If don't send an '\n', users have to press enter manually after # interact() is called # p.send('\x1b\x00') # Send Esc p.interact() except Exception as e: logger.debug(e) return { 'status': 'fail', 'msg': 'Connection failed', }
def _ssh_pexpect3(account, vias=None, forwards=None): import pexpect jump, passwords = compile_jumps(account, vias=vias) _forwards = forwards.compile(prefix='') if forwards else '' if account.identity: command = _SSH_COMMAND_IDENTITY.format(jump=jump, forwards=_forwards, user=account.user, host=account.host, port=account.port, identity=account.identity) else: command = _SSH_COMMAND_PASSWORD.format(jump=jump, forwards=_forwards, user=account.user, host=account.host, port=account.port) try: logger.debug(command) p = pexpect.spawn(command) # Passwords for jump hosts if passwords: for password in passwords: p.expect([pexpect.TIMEOUT, '[p|P]assword:']) p.sendline(password) # Password for dest host if not account.identity: p.expect([pexpect.TIMEOUT, '[p|P]assword:']) p.sendline(account.password) p.sendline() return { 'status': 'success', 'msg': 'Connected', 'p': p, } except Exception as e: logger.debug(e) return { 'status': 'fail', 'msg': 'Connection failed', }
def scp_pexpect(account, targets): import pexpect jump, passwords = compile_jumps(account, prefix='-oProxyJump=') src, dst = targets.src.compile(), targets.dst.compile() if account.identity: command = _SCP_COMMAND_IDENTITY.format(jump=jump, port=account.port, src=src, dst=dst, identity=account.identity) else: command = _SCP_COMMAND_PASSWORD.format(jump=jump, port=account.port, src=src, dst=dst) try: logger.debug(command) p = pexpect.spawn(command) # Passwords for jump hosts if passwords: for password in passwords: p.expect([pexpect.TIMEOUT, '[p|P]assword:']) p.sendline(password) # Password for dest host if not account.identity: p.expect([pexpect.TIMEOUT, '[p|P]assword:']) p.sendline(account.password) set_winsize(p) # Adjust window size # Set auto-adjust window size signal.signal(signal.SIGWINCH, sigwinch_passthrough(p)) p.interact() except Exception as e: logger.debug(e) return { 'status': 'fail', 'msg': 'Connection failed', }