pysftp.Connection

Here are the examples of the python api pysftp.Connection taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

14 Examples 7

Example 1

Project: exporters
Source File: sftp_writer.py
View license
    @retry_long
    def write(self, dump_path, group_key=None, file_name=None):
        import pysftp
        if group_key is None:
            group_key = []

        filebase_path, file_name = self.create_filebase_name(group_key, file_name=file_name)
        destination = (filebase_path + '/' + file_name)
        self.logger.info('Start uploading to {}'.format(dump_path))
        with pysftp.Connection(self.sftp_host, port=self.sftp_port,
                               username=self.sftp_user,
                               password=self.sftp_password) as sftp:
            if not sftp.exists(filebase_path):
                sftp.makedirs(filebase_path)
            progress = SftpUploadProgress(self.logger)
            sftp.put(dump_path, destination, callback=progress)
        self.last_written_file = destination
        self._update_metadata(dump_path, destination)
        self.logger.info('Saved {}'.format(dump_path))

Example 2

Project: exporters
Source File: sftp_writer.py
View license
    def _check_write_consistency(self):
        import pysftp
        with pysftp.Connection(self.sftp_host, port=self.sftp_port,
                               username=self.sftp_user,
                               password=self.sftp_password) as sftp:
            for file_info in self.get_metadata('files_written'):
                try:
                    sftp_info = sftp.stat(file_info['filename'])
                except IOError as e:
                    if e.errno == errno.ENOENT:
                        raise InconsistentWriteState(
                            '{} file is not present at destination'.format(file_info['filename']))
                sftp_size = sftp_info.st_size
                if sftp_size != file_info['size']:
                    raise InconsistentWriteState('Wrong size for file {}. Expected: {} - got {}'
                                                 .format(file_info['filename'], file_info['size'],
                                                         sftp_size))
        self.logger.info('Consistency check passed')

Example 3

Project: luigi
Source File: ftp.py
View license
    def _sftp_connect(self):
        try:
            import pysftp
        except ImportError:
            logger.warning('Please install pysftp to use SFTP.')

        self.conn = pysftp.Connection(self.host, username=self.username, password=self.password,
                                      port=self.port, **self.pysftp_conn_kwargs)

Example 4

Project: auto_backup
Source File: backup_scheduler.py
View license
    def test_sftp_connection(self, cr, uid, ids, context=None):
        conf_ids= self.search(cr, uid, [])
        confs = self.browse(cr,uid,conf_ids)
        #Check if there is a success or fail and write messages 
        messageTitle = ""
        messageContent = ""
        for rec in confs:
            db_list = self.get_db_list(cr, uid, [], rec.host, rec.port)
            try:
                pathToWriteTo = rec.sftppath
                ipHost = rec.sftpip
                usernameLogin = rec.sftpusername
                passwordLogin = rec.sftppassword
                #Connect with external server over SFTP, so we know sure that everything works.
                srv = pysftp.Connection(host=ipHost, username=usernameLogin,
password=passwordLogin)
                srv.close()
                #We have a success.
                messageTitle = "Connection Test Succeeded!"
                messageContent = "Everything seems properly set up for FTP back-ups!"
            except Exception, e:
                messageTitle = "Connection Test Failed!"
                if len(rec.sftpip) < 8:
                    messageContent += "\nYour IP address seems to be too short.\n"
                messageContent += "Here is what we got instead:\n"
        if "Failed" in messageTitle:
            raise osv.except_osv(_(messageTitle), _(messageContent + "%s") % tools.ustr(e))
        else:
            raise osv.except_osv(_(messageTitle), _(messageContent))

Example 5

Project: auto_backup
Source File: backup_scheduler.py
View license
    def schedule_backup(self, cr, user, context={}):
        conf_ids= self.search(cr, user, [])
        confs = self.browse(cr,user,conf_ids)
        for rec in confs:
            db_list = self.get_db_list(cr, user, [], rec.host, rec.port)
            if rec.name in db_list:
                try:
                    if not os.path.isdir(rec.bkp_dir):
                        os.makedirs(rec.bkp_dir)
                except:
                    raise
                #Create name for dumpfile.
                bkp_file='%s_%s.dump' % (time.strftime('%d_%m_%Y_%H_%M_%S'),rec.name)
                file_path = os.path.join(rec.bkp_dir,bkp_file)
                uri = 'http://' + rec.host + ':' + rec.port
                conn = xmlrpclib.ServerProxy(uri + '/xmlrpc/db')
                bkp=''
                try:
                    bkp = execute(conn, 'dump', tools.config['admin_passwd'], rec.name)
                except:
                    logger.notifyChannel('backup', netsvc.LOG_INFO, "Couldn't backup database %s. Bad database administrator password for server running at http://%s:%s" %(rec.name, rec.host, rec.port))
                    continue
                fp = open(file_path,'wb')
                bkp = base64.decodestring(bkp)
                fp.write(bkp)
                fp.close()
            else:
                logger.notifyChannel('backup', netsvc.LOG_INFO, "database %s doesn't exist on http://%s:%s" %(rec.name, rec.host, rec.port))

            #Check if user wants to write to SFTP or not.
            if rec.sftpwrite is True:
                try:
                    #Store all values in variables
                    dir = rec.bkp_dir
                    pathToWriteTo = rec.sftppath
                    ipHost = rec.sftpip
                    usernameLogin = rec.sftpusername
                    passwordLogin = rec.sftppassword
                    #Connect with external server over SFTP
                    srv = pysftp.Connection(host=ipHost, username=usernameLogin,
password=passwordLogin)
                    #set keepalive to prevent socket closed / connection dropped error
                    srv._transport.set_keepalive(30)
                    #Move to the correct directory on external server. If the user made a typo in his path with multiple slashes (/odoo//backups/) it will be fixed by this regex.
                    pathToWriteTo = re.sub('([/]{2,5})+','/',pathToWriteTo)
                    print(pathToWriteTo)
                    try:
                        srv.chdir(pathToWriteTo)
                    except IOError:
                        #Create directory and subdirs if they do not exist.
                        currentDir = ''
                        for dirElement in pathToWriteTo.split('/'):
                            currentDir += dirElement + '/'
                            try:
                                srv.chdir(currentDir)
                            except:
                                print('(Part of the) path didn\'t exist. Creating it now at ' + currentDir)
                                #Make directory and then navigate into it
                                srv.mkdir(currentDir, mode=777)
                                srv.chdir(currentDir)
                                pass
                    srv.chdir(pathToWriteTo)
                    #Loop over all files in the directory.
                    for f in os.listdir(dir):
                        fullpath = os.path.join(dir, f)
                        if os.path.isfile(fullpath):
                            print(fullpath)
                            srv.put(fullpath)

                    #Navigate in to the correct folder.
                    srv.chdir(pathToWriteTo)

                    #Loop over all files in the directory from the back-ups.
                    #We will check the creation date of every back-up.
                    for file in srv.listdir(pathToWriteTo):
                        #Get the full path
                        fullpath = os.path.join(pathToWriteTo,file) 
                        #Get the timestamp from the file on the external server
                        timestamp = srv.stat(fullpath).st_atime
                        createtime = datetime.datetime.fromtimestamp(timestamp)
                        now = datetime.datetime.now()
                        delta = now - createtime
                        #If the file is older than the daystokeepsftp (the days to keep that the user filled in on the Odoo form it will be removed.
                        if delta.days >= rec.daystokeepsftp:
                            #Only delete files, no directories!
                            if srv.isfile(fullpath) and ".dump" in file:
                                print("Delete: " + file)
                                srv.unlink(file)
                    #Close the SFTP session.
                    srv.close()
                except Exception, e:
                    _logger.debug('Exception! We couldn\'t back up to the FTP server..')
                    #At this point the SFTP backup failed. We will now check if the user wants
                    #an e-mail notification about this.
                    if rec.sendmailsftpfail:
                        try:
                            ir_mail_server = self.pool.get('ir.mail_server') 
                            message = "Dear,\n\nThe backup for the server " + rec.host + " (IP: " + rec.sftpip + ") failed.Please check the following details:\n\nIP address SFTP server: " + rec.sftpip + "\nUsername: " + rec.sftpusername + "\nPassword: " + rec.sftppassword + "\n\nError details: " + tools.ustr(e) + "\n\nWith kind regards"
                            msg = ir_mail_server.build_email("[email protected]" + rec.name + ".com", [rec.emailtonotify], "Backup from " + rec.host + "(" + rec.sftpip + ") failed", message) 
                            ir_mail_server.send_email(cr, user, msg)
                        except Exception:
                            pass

            """Remove all old files (on local server) in case this is configured..
            This is done after the SFTP writing to prevent unusual behaviour:
            If the user would set local back-ups to be kept 0 days and the SFTP
            to keep backups xx days there wouldn't be any new back-ups added to the
            SFTP.
            If we'd remove the dump files before they're writen to the SFTP there willbe nothing to write. Meaning that if an user doesn't want to keep back-ups locally and only wants them on the SFTP (NAS for example) there wouldn't be any writing to the remote server if this if statement was before the SFTP write method right above this comment.
            """
            if rec.autoremove is True:
                dir = rec.bkp_dir
                #Loop over all files in the directory.
                for f in os.listdir(dir):
                    fullpath = os.path.join(dir, f)
                    timestamp = os.stat(fullpath).st_ctime
                    createtime = datetime.datetime.fromtimestamp(timestamp)
                    now = datetime.datetime.now()
                    delta  = now - createtime
                    if delta.days >= rec.daystokeep:
                        #Only delete files (which are .dump), no directories.
                        if os.path.isfile(fullpath) and ".dump" in f:
                            print("Delete: " + fullpath)
                            os.remove(fullpath)

Example 6

Project: Flexget
Source File: sftp.py
View license
def sftp_connect(conf):
    """
    Helper function to connect to an sftp server
    """
    sftp = None
    tries = CONNECT_TRIES
    retry_interval = RETRY_INTERVAL

    while not sftp:
        try:
            sftp = pysftp.Connection(host=conf.host, username=conf.username,
                                     private_key=conf.private_key, password=conf.password,
                                     port=conf.port, private_key_pass=conf.private_key_pass)
            sftp.timeout = SOCKET_TIMEOUT
            log.verbose('Connected to %s' % conf.host)
        except Exception as e:
            if not tries:
                raise e
            else:
                log.debug('Caught exception: %s' % e)
                log.warning('Failed to connect to %s; waiting %d seconds before retrying.' %
                            (conf.host, retry_interval))
                time.sleep(retry_interval)
                tries -= 1
                retry_interval += RETRY_STEP

    return sftp

Example 7

Project: odoo-saas-tools
Source File: res_config.py
View license
    def test_sftp_connection(self):
        server = self.env["ir.config_parameter"].get_param("saas_server.sftp_server", default=None)
        username = self.env["ir.config_parameter"].get_param("saas_server.sftp_username", default=None)
        password = self.env["ir.config_parameter"].get_param("saas_server.sftp_password", default=None)
        path = self.env["ir.config_parameter"].get_param("saas_server.sftp_path", default=None)
        sftp_rsa_key_path = self.env["ir.config_parameter"].get_param('saas_server.sftp_rsa_key_path')

        messageTitle = ""
        messageContent = ""

        try:
            # Connect with external server over SFTP, so we know sure that everything works.
            if sftp_rsa_key_path:
                srv = pysftp.Connection(host=server, username=username,
                                        private_key=sftp_rsa_key_path,
                                        private_key_pass=password)
            else:
                srv = pysftp.Connection(host=server, username=username, password=password)
            srv.close()
            # We have a success.
            messageTitle = "Connection Test Succeeded!"
            messageContent = "Everything seems properly set up for FTP back-ups!"
        except Exception as e:
            messageTitle = "Connection Test Failed!"
            messageContent += "Here is what we got instead:\n"
        if "Failed" in messageTitle:
            raise UserError(_(messageTitle), _(messageContent + "%s") % tools.ustr(e))
        else:
            _logger.info(_(messageTitle), _(messageContent))

Example 8

Project: odoo-saas-tools
Source File: saas_server.py
View license
    @api.model
    def _transport_backup(self, dump_db, filename=None):
        server = self.env['ir.config_parameter'].get_param('saas_server.sftp_server', None)
        username = self.env['ir.config_parameter'].get_param('saas_server.sftp_username', None)
        password = self.env['ir.config_parameter'].get_param('saas_server.sftp_password', None)
        path = self.env['ir.config_parameter'].get_param('saas_server.sftp_path', None)
        sftp_rsa_key_path = self.env['ir.config_parameter'].get_param(
            'saas_server.sftp_rsa_key_path', None)

        if sftp_rsa_key_path:
            srv = pysftp.Connection(host=server, username=username,
                                    private_key=sftp_rsa_key_path,
                                    private_key_pass=password)
        else:
            srv = pysftp.Connection(host=server, username=username,
                                    password=password)

        # set keepalive to prevent socket closed / connection dropped error
        srv._transport.set_keepalive(30)

        try:
            srv.chdir(path)
        except IOError:
            # Create directory and subdirs if they do not exist.
            currentDir = ''
            for dirElement in path.split('/'):
                currentDir += dirElement + '/'
                try:
                    srv.chdir(currentDir)
                except:
                    print('(Part of the) path didn\'t exist. Creating it now at ' + currentDir)
                    # Make directory and then navigate into it
                    srv.mkdir(currentDir, mode=777)
                    srv.chdir(currentDir)

        srv.chdir(path)
        with tempfile.TemporaryFile() as t:
            dump_db(t)
            t.seek(0)
            srv.putfo(t, filename)
        srv.close()

Example 9

Project: cider
Source File: sftp.py
View license
    def get_connection(self):
        """Open the SSH connection."""
        connection_id = self.get_connection_id()
        if connection_id:
            details = self.get_connection_details()
            try:
                server = pysftp.Connection(
                    host = details['host'],
                    username = details['user'],
                    password = details['password']
                )
                return server
            except:  # pylint: disable=W0702
                log.error('Connection to server over SSH failed.')
                self.send_error(200)
                return None
        else:
            connection_id = self.setup_connection()
            # TODO: This should properly parse the URL in order to maintain
            # existing parameters.
            self.redirect('?connection=' + connection_id)
            return None

Example 10

Project: cider
Source File: sftp.py
View license
    def get_connection(self):
        """Open the SSH connection."""
        connection_id = self.get_connection_id()
        if connection_id:
            details = self.get_connection_details()
            try:
                server = pysftp.Connection(
                    host = details['host'],
                    username = details['user'],
                    password = details['password']
                )
                return server
            except:  # pylint: disable=W0702
                log.error('Connection to server over SSH failed.')
                self.send_error(200)
                return None
        else:
            connection_id = self.setup_connection()
            # TODO: This should properly parse the URL in order to maintain
            # existing parameters.
            self.redirect('?connection=' + connection_id)
            return None

Example 11

View license
    @api.multi
    def verify_directories_button(self):
        ''' test connection and verify if directories are the same in the DB

            :returns None:
            :raises Warning:
                - if current user do not have key
                - if unable to connect to sftp
        '''
        self.ensure_one()

        key = [e for e in self.authentication_key_ids
               if e.user_id == self.env.user]

        if not key:
            raise exceptions.Warning(_("You don't have key"))

        if not key[0].key_active:
            raise exceptions.Warning(_('Key not active'))

        try:
            (tmp_key, tmp_d) = self._create_tmp_file(
                key[0].private_key_crypted)
            key_pass = self.authentication_key_ids.config()

            # connect sftp
            with pysftp.Connection(self.hostname,
                                   username=self.username,
                                   private_key=tmp_key.name,
                                   private_key_pass=key_pass) as sftp:

                directories = sftp.listdir()

            # save new directories
            self._save_directories(directories)

        except Exception as e:
            _logger.error("Unable to connect to the sftp: %s", e)
            raise exceptions.Warning(_('Unable to connect to the sftp'))

        finally:
            try:
                tmp_key.close()
            except:
                _logger.error("remove tmp_key file failed")
            try:
                shutil.rmtree(tmp_d)
            except:
                _logger.error("remove tmp directory failed")

Example 12

View license
    @api.multi
    def import_button(self):
        ''' download the file from the sftp where the directories
            were selected in the FDS configuration, and if possible import
            to bank Statments.
            Called by pressing import button.

            :returns action: configuration for the next wizard's view
        '''
        self.ensure_one()
        (fds_id, hostname, username, key, key_pass) = self._get_sftp_config()
        if not key:
            self.state = 'error'
            return self._do_populate_tasks()

        if not key.key_active:
            self.state = 'error'
            return self._do_populate_tasks()

        try:
            # create temp file
            (tmp_key, tmp_d) = self._create_tmp_file(key.private_key_crypted)

            # get name of directory where download
            dir = [(e.name, e.id) for e in
                   fds_id.directory_ids
                   if e.allow_download_file is True]

            # connect sftp
            with pysftp.Connection(hostname,
                                   username=username,
                                   private_key=tmp_key.name,
                                   private_key_pass=key_pass) as sftp:

                fds_files_ids = self._download_file(sftp, dir, tmp_d, fds_id)

            # import to bank statements
            self._import2bankStatements(fds_files_ids)
            self.state = 'done'
        except Exception as e:
            self.state = 'errorSFTP'
            _logger.error("Unable to connect to the sftp: %s", e)
        finally:
            try:
                tmp_key.close()
            except:
                _logger.error("remove tmp_key file failed")
            try:
                shutil.rmtree(tmp_d)
            except:
                _logger.error("remove tmp directory failed")

        self._changeMessage()
        return self._do_populate_tasks()

Example 13

View license
    @api.multi
    def send_export_button(self):
        ''' Upload pain_001 file to the FDS Postfinance by SFTP

            :returns action: configuration for wizard's next view
            :raises Warning:
                - If no FDS account and directory selected
                - If current user do not have key
                - If connection to SFTP fails
        '''
        self.ensure_one()
        if not self.fds_account_id:
            raise exceptions.Warning('Select a FDS account')

        if not self.fds_directory_id:
            raise exceptions.Warning('Select a directory')

        # check key of active user
        fds_keys_obj = self.env['fds.authentication.keys']
        key = fds_keys_obj.search([
            ['user_id', '=', self.env.uid],
            ['fds_account_id', '=', self.fds_account_id.id],
            ['key_active', '=', True]])

        if not key:
            raise exceptions.Warning(
                "You don't have access to the selected FDS account.")

        try:
            # create tmp file
            tmp_d = tempfile.mkdtemp()
            tmp_key = self._create_tmp_file(key.private_key_crypted, tmp_d)[0]
            tmp_f = self._create_tmp_file(self.file, tmp_d)[0]
            old_path_f = os.path.join(tmp_d, tmp_f.name)
            new_path_f = os.path.join(tmp_d, self.filename)
            shutil.move(old_path_f, new_path_f)
            key_pass = fds_keys_obj.config()

            # upload to sftp
            with pysftp.Connection(self.fds_account_id.hostname,
                                   username=self.fds_account_id.username,
                                   private_key=tmp_key.name,
                                   private_key_pass=key_pass) as sftp:
                with sftp.cd(self.fds_directory_id.name):
                    sftp.put(new_path_f)
                    _logger.info("[OK] upload file (%s) to SFTP",
                                 (self.filename))

            # change to initial name file (mandatory because of the close)
            shutil.move(new_path_f, old_path_f)
            self._state_confirm_on()
            self._add2historical()
        except Exception as e:
            _logger.error("Unable to connect to the SFTP: %s", e)
            raise exceptions.Warning('Unable to connect to the SFTP')

        finally:
            try:
                tmp_key.close()
            except:
                _logger.error("remove tmp_key file failed")
            try:
                tmp_f.close()
            except:
                _logger.error("remove tmp_f file failed")
            try:
                shutil.rmtree(tmp_d)
            except:
                _logger.error("remove tmp directory failed")

        return self._refresh_wizard()

Example 14

View license
    @api.multi
    def send_generate_file_button(self):
        ''' upload pain_001 file to the FDS Postfinance by sftp

            :returns action: configuration for the next wizard's view
            :raises Warning:
                - If no fds account and directory selected
                - if current user do not have key
                - if unable to connect to sftp
        '''
        self.ensure_one()
        if not self.fds_account_id:
            raise exceptions.Warning('Select a FDS account')

        if not self.fds_directory_id:
            raise exceptions.Warning('Select a directory')

        (key, key_pass) = self._get_sftp_key()

        try:
            # create tmp file
            tmp_d = tempfile.mkdtemp()
            tmp_key = self._create_tmp_file(key.private_key_crypted, tmp_d)[0]
            tmp_f = self._create_tmp_file(self.attachment_id.datas, tmp_d)[0]
            old_path_f = os.path.join(tmp_d, tmp_f.name)
            new_path_f = os.path.join(tmp_d, ''.join(self.filename.split(':')))
            shutil.move(old_path_f, new_path_f)

            # upload to sftp
            with pysftp.Connection(self.fds_account_id.hostname,
                                   username=self.fds_account_id.username,
                                   private_key=tmp_key.name,
                                   private_key_pass=key_pass) as sftp:

                with sftp.cd(self.fds_directory_id.name):
                    sftp.put(new_path_f)
                    _logger.info("[OK] upload file (%s) ", (self.filename))

            # change to initial name file (mandatory because of the close)
            shutil.move(new_path_f, old_path_f)
            self._state_confirm_on()
            self._add2historical()
        except Exception as e:
            self.attachment_id.unlink()
            _logger.error("Unable to connect to the sftp: %s", e)
            raise exceptions.Warning('Unable to connect to the sftp')

        finally:
            try:
                tmp_key.close()
            except:
                _logger.error("remove tmp_key file failed")
            try:
                tmp_f.close()
            except:
                _logger.error("remove tmp_f file failed")
            try:
                shutil.rmtree(tmp_d)
            except:
                _logger.error("remove tmp directory failed")

        return self._refresh_wizard()