def _send_dir(access, local_dir_path, listing):
    access, listing = _load_access_listing(access, listing)
    auth = access['auth']
    remote_dir_path = os.path.normpath(cut_remote_user_dir(access['dirPath']))

    with create_ssh_client(
            host=access['host'],
            port=access.get('port', DEFAULT_PORT),
            username=auth['username'],
            password=auth.get('password'),
            private_key=auth.get('privateKey'),
            passphrase=auth.get('passphrase'),
            timeout=access.get('timeout', DEFAULT_TIMEOUT),
            auth_timeout=access.get('authTimeout', DEFAULT_AUTH_TIMEOUT),
            banner_timeout=access.get('bannerTimeout',
                                      DEFAULT_BANNER_TIMEOUT)) as ssh_client:
        if listing:
            with ssh_client.open_sftp() as sftp_client:
                ssh_mkdir(sftp_client, remote_dir_path)
                send_directory(listing, sftp_client, local_dir_path,
                               remote_dir_path)
        else:
            with ssh_client.open_sftp() as sftp_client:
                remote_parent_dir = os.path.dirname(remote_dir_path)
                ssh_mkdir(sftp_client, remote_parent_dir)
            with SCPClient(ssh_client.get_transport()) as scp_client:
                scp_client.put(local_dir_path, remote_dir_path, recursive=True)
def _send_file(access, local_file_path):
    with open(access) as f:
        access = json.load(f)

    if not os.path.isfile(local_file_path):
        raise FileNotFoundError(
            'Could not find local file "{}"'.format(local_file_path))

    auth = access['auth']
    remote_file_path = cut_remote_user_dir(access['filePath'])

    remote_dir_path = os.path.dirname(remote_file_path)

    with create_ssh_client(
            host=access['host'],
            port=access.get('port', DEFAULT_PORT),
            username=auth['username'],
            password=auth.get('password'),
            private_key=auth.get('privateKey'),
            passphrase=auth.get('passphrase'),
            timeout=access.get('timeout', DEFAULT_TIMEOUT),
            auth_timeout=access.get('authTimeout', DEFAULT_AUTH_TIMEOUT),
            banner_timeout=access.get('bannerTimeout',
                                      DEFAULT_BANNER_TIMEOUT)) as client:
        with client.open_sftp() as sftp:
            ssh_mkdir(sftp, remote_dir_path)
            sftp.put(local_file_path, remote_file_path)
def _receive_file_validate(access):
    with open(access) as f:
        access = json.load(f)

    jsonschema.validate(access, FILE_SCHEMA)

    auth = access['auth']

    remote_file_path = cut_remote_user_dir(access['filePath'])

    with create_ssh_client(
            host=access['host'],
            port=access.get('port', DEFAULT_PORT),
            username=auth['username'],
            password=auth.get('password'),
            private_key=auth.get('privateKey'),
            passphrase=auth.get('passphrase'),
            timeout=access.get('timeout', DEFAULT_TIMEOUT),
            auth_timeout=access.get('authTimeout', DEFAULT_AUTH_TIMEOUT),
            banner_timeout=access.get('bannerTimeout',
                                      DEFAULT_BANNER_TIMEOUT)) as client:
        with client.open_sftp() as sftp:
            try:
                with sftp.open(remote_file_path, mode='r', bufsize=0):
                    pass
            except FileNotFoundError:
                raise FileNotFoundError(
                    'Could not find remote file "{}"'.format(remote_file_path))
def _receive_file(access, local_file_path):
    with open(access) as f:
        access = json.load(f)

    if not os.path.isdir(os.path.dirname(local_file_path)):
        raise NotADirectoryError(
            'Could not create local file "{}". The parent directory does not exist.'
            .format(local_file_path))

    auth = access['auth']

    remote_file_path = cut_remote_user_dir(access['filePath'])

    with create_ssh_client(
            host=access['host'],
            port=access.get('port', DEFAULT_PORT),
            username=auth['username'],
            password=auth.get('password'),
            private_key=auth.get('privateKey'),
            passphrase=auth.get('passphrase'),
            timeout=access.get('timeout', DEFAULT_TIMEOUT),
            auth_timeout=access.get('authTimeout', DEFAULT_AUTH_TIMEOUT),
            banner_timeout=access.get('bannerTimeout',
                                      DEFAULT_BANNER_TIMEOUT)) as client:
        with client.open_sftp() as sftp:
            try:
                sftp.get(remote_file_path, local_file_path)
            except FileNotFoundError:
                raise FileNotFoundError(
                    'Remote file "{}" could not be found'.format(
                        remote_file_path))
def _receive_dir(access, local_dir_path, listing):
    local_dir_path = os.path.normpath(local_dir_path)
    access, listing = _load_access_listing(access, listing)
    auth = access['auth']
    remote_dir_path = os.path.normpath(cut_remote_user_dir(access['dirPath']))

    if not os.path.isdir(os.path.dirname(local_dir_path)):
        raise FileNotFoundError(
            'Could not create local directory "{}", because parent directory does not exist'
            .format(local_dir_path))

    with create_ssh_client(
            host=access['host'],
            port=access.get('port', DEFAULT_PORT),
            username=auth['username'],
            password=auth.get('password'),
            private_key=auth.get('privateKey'),
            passphrase=auth.get('passphrase'),
            timeout=access.get('timeout', DEFAULT_TIMEOUT),
            auth_timeout=access.get('authTimeout', DEFAULT_AUTH_TIMEOUT),
            banner_timeout=access.get('bannerTimeout',
                                      DEFAULT_BANNER_TIMEOUT)) as client:
        with SCPClient(client.get_transport()) as scp_client:
            if listing:
                try:
                    os.mkdir(local_dir_path)
                except FileExistsError:
                    pass
                fetch_directory(listing, scp_client, local_dir_path,
                                remote_dir_path)
            else:
                try:
                    scp_client.get(remote_dir_path,
                                   local_dir_path,
                                   recursive=True)
                except SCPException:
                    raise FileNotFoundError(
                        'Could not find remote directory "{}"'.format(
                            remote_dir_path))
                except FileNotFoundError:
                    raise NotADirectoryError(
                        'Could not create local directory "{}", because parent directory does not exist'
                        .format(local_dir_path))
def _send_file_validate(access):
    with open(access) as f:
        access = json.load(f)

    jsonschema.validate(access, FILE_SCHEMA)

    # check whether authentication works
    auth = access['auth']
    with create_ssh_client(host=access['host'],
                           port=access.get('port', DEFAULT_PORT),
                           username=auth['username'],
                           password=auth.get('password'),
                           private_key=auth.get('privateKey'),
                           passphrase=auth.get('passphrase'),
                           timeout=access.get('timeout', DEFAULT_TIMEOUT),
                           auth_timeout=access.get('authTimeout',
                                                   DEFAULT_AUTH_TIMEOUT),
                           banner_timeout=access.get('bannerTimeout',
                                                     DEFAULT_BANNER_TIMEOUT)):
        pass
def _send_dir_validate(access, listing):
    access, listing = _load_access_listing(access, listing)

    jsonschema.validate(access, DIR_SCHEMA)
    if listing:
        jsonschema.validate(listing, LISTING_SCHEMA)

    # check whether authentication works
    auth = access['auth']
    with create_ssh_client(host=access['host'],
                           port=access.get('port', DEFAULT_PORT),
                           username=auth['username'],
                           password=auth.get('password'),
                           private_key=auth.get('privateKey'),
                           passphrase=auth.get('passphrase'),
                           timeout=access.get('timeout', DEFAULT_TIMEOUT),
                           auth_timeout=access.get('authTimeout',
                                                   DEFAULT_AUTH_TIMEOUT),
                           banner_timeout=access.get('bannerTimeout',
                                                     DEFAULT_BANNER_TIMEOUT)):
        pass
Пример #8
0
def _mount_dir(access, local_dir_path):
    with open(access) as f:
        access = json.load(f)

    os.makedirs(local_dir_path, exist_ok=True)

    host = access['host']
    port = access.get('port', DEFAULT_PORT)
    dir_path = access['dirPath']
    auth = access['auth']
    username = auth['username']
    password = auth.get('password')
    private_key = auth.get('privateKey')
    passphrase = auth.get('passphrase')
    ciphers = access.get('ciphers')

    enable_password = False
    identity_file = None

    if private_key:
        identity_file = create_identity_file(private_key)
    elif password:
        enable_password = True
    else:
        raise InvalidAuthenticationError(
            'At least password or private_key must be present.')

    try:
        check_remote_dir_available(access)
    except FileNotFoundError:
        with create_ssh_client(
                host=access['host'],
                port=access.get('port', DEFAULT_PORT),
                username=auth['username'],
                password=auth.get('password'),
                private_key=auth.get('privateKey'),
                passphrase=auth.get('passphrase')) as ssh_client:
            with ssh_client.open_sftp() as sftp_client:
                ssh_mkdir(sftp_client, dir_path)

    with create_configfile(ciphers,
                           enable_password=enable_password) as temp_configfile:
        command = create_sshfs_command(
            host=host,
            port=port,
            username=username,
            local_dir_path=local_dir_path,
            remote_path=dir_path,
            configfile_path=temp_configfile.name,
            writable=access.get('writable', False),
            enable_password_stdin=enable_password,
            identity_file=identity_file.name if identity_file else None)

        if private_key:
            if passphrase:
                try:
                    _mount_with_key_and_passphrase(command, passphrase)
                except InvalidAuthenticationError as e:
                    raise InvalidAuthenticationError(
                        'Could not mount directory using\n\thost={host}\n\tport={port}\n\tlocalDir={local_dir_path}\n\t'
                        'dirPath={dir_path}\n\tauthentication=key + passphrase\nvia sshfs:\n{error}.'
                        .format(host=host,
                                port=port,
                                local_dir_path=local_dir_path,
                                dir_path=dir_path,
                                error=str(e)))
                finally:
                    identity_file.close()
            else:
                process_result = subprocess.run(command,
                                                stderr=subprocess.PIPE)

                identity_file.close()

                if process_result.returncode != 0:
                    raise InvalidAuthenticationError(
                        'Could not mount directory using\n\thost={host}\n\tport={port}\n\tlocalDir={local_dir_path}\n\t'
                        'dirPath={dir_path}\n\tauthentication=key no passphrase\nvia sshfs:\n{error}'
                        .format(host=host,
                                port=port,
                                local_dir_path=local_dir_path,
                                dir_path=dir_path,
                                error=process_result.stderr.decode('utf-8')))
        elif password:
            process_result = subprocess.run(command,
                                            input=password.encode('utf-8'),
                                            stderr=subprocess.PIPE)

            if process_result.returncode != 0:
                raise InvalidAuthenticationError(
                    'Could not mount directory using\n\thost={host}\n\tport={port}\n\tlocalDir={local_dir_path}\n\t'
                    'dirPath={dir_path}\n\tauthentication=password\nvia sshfs:\n{error}'
                    .format(host=host,
                            port=port,
                            local_dir_path=local_dir_path,
                            dir_path=dir_path,
                            error=process_result.stderr.decode('utf-8')))