test_direct_access.py 文件源码

python
阅读 16 收藏 0 点赞 0 评论 0

项目:kuberdock-platform 作者: cloudlinux 项目源码 文件源码
def _run_scp_command(cmd, user, host, password):
    """
    Emulate user command line interation using SCP protocol
    :param cmd: command to be executed
    :param user: remote host user
    :param host: remote host IP/hostname
    :param password: passwrod for remote user on host
    :returns None:
    """
    logger.debug(u'{}Running SCP: {}{}'.format(
        Style.DIM, cmd, Style.RESET_ALL))
    scp = pexpect.spawn(cmd)
    i = scp.expect(['\(yes/no\)\? ', '[Pp]assword: '])
    if i == 0:
        scp.sendline('yes')
        scp.expect('[Pp]assword: ')
    scp.sendline(password)
    time.sleep(1)
    try:
        while True:
            i = scp.expect([pexpect.EOF, '[0-9][0-9]:[0-9][0-9]   '],
                           timeout=5)
            if i == 0:
                logger.debug(u'{}{}{}'.format(Fore.YELLOW, scp.before,
                                              Style.RESET_ALL))
                break
            logger.debug(u'{}{}{}{}'.format(Fore.YELLOW, scp.before,
                                            scp.after, Style.RESET_ALL))
            time.sleep(.1)
    except pexpect.TIMEOUT:
        # A docker bug expecting an extra new line in the end. Ideally we
        # will exit the loop getting pexpect.EOF, i.e. i==0
        logger.debug(u'{}{}{}'.format(Fore.YELLOW, scp.before,
                                      Style.RESET_ALL))
    finally:
        if scp.isalive():
            scp.close()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号