Python paramiko 模块,sftp_client() 实例源码

我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用paramiko.sftp_client()

项目:JumpSSH    作者:AmadeusITGroup    | 项目源码 | 文件源码
def get_sftp_client(self):
        """ See documentation for available methods on paramiko.sftp_client at :
            http://docs.paramiko.org/en/latest/api/sftp.html

        :return: paramiko SFTP client object.
        :rtype: paramiko.sftp_client.SFTPClient

        Usage::
            # open session with remote host
            >>> from jumpssh import SSHSession
            >>> ssh_session = SSHSession('gateway.example.com', 'my_user', password='my_password').open()

            # get sftp client
            >>> sftp_client = ssh_session.get_sftp_client()
        """
        return paramiko.sftp_client.SFTPClient.from_transport(self.ssh_transport)
项目:JumpSSH    作者:AmadeusITGroup    | 项目源码 | 文件源码
def get(self,
            remote_path,
            local_path,
            use_sudo=False,
            username=None
            ):
        """Download a file from the remote host

        :param remote_path: remote path of the file to download
        :param local_path: local path where to download the file
        :param use_sudo: allow to download a file from a location current user does not have access
        :param username: sudo user

        Usage::

            # download remote file in local directory
            >>> ssh_session.get(remote_path='/path/to/remote/file', local_path='/local/folder')

            # donload remote file from a path not accessible by current user
            >>> ssh_session.get(local_path='/path/to/local/file', remote_path='/path/to/remote/file', use_sudo=True)
        """
        copy_path = remote_path
        remote_filename = os.path.basename(remote_path)
        sudo_username = username if username else 'root' if use_sudo else None

        # copy first remote file in a temporary location accessible from current user
        if use_sudo:
            copy_path = "/tmp/%s" % util.id_generator(size=15)
            copy_command = "cp %s %s" % (remote_path, copy_path)
            self.run_cmd(copy_command, silent=True, username=sudo_username)

        # if local download path is a directory, local filename will be same as remote
        if os.path.isdir(local_path):
            local_path = os.path.join(local_path, remote_filename)

        sftp_client = self.get_sftp_client()
        try:
            with open(local_path, mode='w') as local_file:
                with sftp_client.file(copy_path) as remote_file:
                    local_file.write(remote_file.read().decode('utf-8'))
        finally:
            if use_sudo:
                # cleanup temporary file
                self.run_cmd('rm %s' % copy_path, silent=True, username=sudo_username)
项目:JumpSSH    作者:AmadeusITGroup    | 项目源码 | 文件源码
def file(
            self,
            remote_path,
            content,
            use_sudo=False,
            owner=None,
            permissions=None,
            username=None,
            silent=False
    ):
        """ Method to create a remote file with the specified `content`

        :param remote_path: destination folder in which to copy the local file
        :param content: content of the file
        :param use_sudo: allow to copy file in location with restricted permissions
        :param owner: user that will own the file on the remote host
        :param permissions: permissions to apply on the remote file (chmod format)
        :param username: sudo user
        :param silent: disable logging

        Usage::

            # create file on remote host and with specified content at the specified path
            >>> ssh_session.file(remote_path='/path/to/remote/file', content='file content')

            # create file on remote host and with specified content at the specified path needing sudo permissions
            >>> ssh_session.file(remote_path='/path/to/remote/file', content='file content', use_sudo=True)

            # create file on remote host and with specified content at the specified path
            # with specified owner and permissions
            >>> ssh_session.file(remote_path='/path/to/remote/file', content='file content',
            ...                 owner='other_user', permissions='700')
        """
        if not silent:
            logger.debug("Create file '%s' on remote host '%s' as '%s'" % (remote_path, self.host, self.username))
        sftp_client = self.get_sftp_client()

        copy_path = remote_path
        if use_sudo:
            # copy local file on remote host in temporary dir
            copy_path = "/tmp/%s" % util.id_generator(size=15)

        # create file remotely
        with sftp_client.file(copy_path, mode='w+') as remote_file:
            remote_file.write(content)

        # mv this file in the final destination
        if use_sudo:
            move_command = "mv %s %s" % (copy_path, remote_path)
            self.run_cmd(move_command, silent=True, username=username or 'root')

        # file will be owned by the specified user
        if owner:
            full_owner = owner
            if ':' not in owner:
                full_owner = '{0}:{0}'.format(owner)
            self.run_cmd("sudo chown %s %s" % (full_owner, remote_path), silent=True)

        if permissions:
            self.run_cmd("sudo chmod %s %s" % (permissions, remote_path), silent=True)