How to use the pysftp.Connection function in pysftp

To help you get started, we’ve selected a few pysftp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mlflow / mlflow / tests / store / artifact / test_sftp_artifact_repo.py View on Github external
def sftp_mock():
    return MagicMock(autospec=pysftp.Connection)
github google / starthinker / starthinker / util / data / __init__.py View on Github external
bucket_create(auth, project.id, destination['storage']['bucket'])

    # put the file
    file_out = destination['storage']['bucket'] + ':' + destination['storage']['path'] + variant
    if project.verbose: print('SAVING', file_out)
    object_put(auth, file_out, rows_to_csv(rows))

  if 'sftp' in destination:
    try:
      cnopts = pysftp.CnOpts()
      cnopts.hostkeys = None

      path_out, file_out = destination['sftp']['file'].rsplit('.', 1)
      file_out = path_out + variant + file_out

      sftp = pysftp.Connection(host=destination['sftp']['host'], username=destination['sftp']['username'], password=destination['sftp']['password'], port=destination['sftp']['port'], cnopts=cnopts)

      if '/' in file_out:
        dir_out, file_out = file_out.rsplit('/', 1)
        sftp.cwd(dir_out)

      sftp.putfo(rows_to_csv(rows), file_out)

    except e:
      print(str(e))
      traceback.print_exc()
github Catboy96 / Gekko / gekko / gekko.py View on Github external
with open(ignfile, 'r', encoding='UTF-8') as fr:
            lines = fr.read().split('\n')
            while '' in lines:
                lines.remove('')
    except FileNotFoundError:
        lines = []

    # Establish SFTP connection
    try:
        cnopts = pysftp.CnOpts()
        cnopts.hostkeys = None
        print("Connecting to %s:%s... " % (host, port), end='')
        if key != '':
            sftp = pysftp.Connection(host, username=user, port=int(port), private_key=key, cnopts=cnopts)
        else:
            sftp = pysftp.Connection(host, username=user, port=int(port), password=password, cnopts=cnopts)
        print("Connected.")
    except pysftp.exceptions.ConnectionException:
        print("\n\nAn error occurred when establishing connection.\nCheck for Internet connection.")
        exit(8)
    except paramiko.ssh_exception.AuthenticationException:
        print("\n\nAuthentication failed.")
        exit(7)

    # Check for uploading directory
    print("Checking for %s... " % path, end='')
    if sftp.exists(path):
        print("Exist.")
        print("Change directory to %s... Done." % path)
        sftp.cd(path)
    else:
        # Remote directory do not exist
github it-projects-llc / odoo-saas-tools / saas_server_backup_ftp / models / saas_server.py View on Github external
}
        if rsa_key_path:
            params["private_key"] = self.rsa_key_path
            if rsa_key_passphrase:
                params["private_key_pass"] = rsa_key_passphrase
        else:
            params["password"] = password

        cnopts = pysftp.CnOpts()
        if sftp_public_key:
            key = paramiko.RSAKey(data=base64.b64decode(sftp_public_key))
            cnopts.hostkeys.add(server, 'ssh-rsa', key)
        else:
            cnopts.hostkeys = None

        with pysftp.Connection(**params, cnopts=cnopts) as sftp:

            # set keepalive to prevent socket closed / connection dropped error
            sftp._transport.set_keepalive(30)
            try:
                sftp.chdir(path)
            except IOError:
                # Create directory and subdirs if they do not exist.
                currentDir = ''
                for dirElement in path.split('/'):
                    currentDir += dirElement + '/'
                    try:
                        sftp.chdir(currentDir)
                    except Exception as e:
                        print(('(Part of the) path doesn\'t exist. Creating it now at ' + currentDir))
                        # Make directory and then navigate into it
                        sftp.mkdir(currentDir, mode=777)
github Lucas-C / dotfiles_and_notes / languages / python / sftp_cli.py View on Github external
def main(argv):
    args, remaining_argv = parse_args(argv)
    if not args.password:
        args.password = getpass.getpass(prompt='Password for user {} : '.format(args.user))
    cnopts = pysftp.CnOpts()
    if args.disable_host_key_checking:
        cnopts.hostkeys = None  # cf. http://pysftp.readthedocs.io/en/release_0.2.9/cookbook.html#pysftp-cnopts
        # also avoid a AttributeError: 'Connection' object has no attribute '_sftp_live'
    with pysftp.Connection(args.host, username=args.user, password=args.password, cnopts=cnopts) as sftp:
        with sftp.cd(args.dir):
            print(getattr(sftp, args.command)(*remaining_argv) or remaining_argv)
github manuwhs / Trapyng / Examples / IoTubes / FTP_lib.py View on Github external
def connect(self):
        """ Establish connection with the server """
        
        if (self.connection_flag == False):
            cnopts = pysftp.CnOpts()
            cnopts.hostkeys = None   
            sftp = pysftp.Connection(self.mySFTP_config.URL, username=self.mySFTP_config.USER, 
                                     password=self.mySFTP_config.PASSWORD, cnopts = cnopts)
            
            self.connection_flag = True
            self.sftp = sftp
github mikemccand / luceneutil / src / python / writeGeoGraphs.py View on Github external
# This data is a lie ... was running GeoPoint box query but doing messed up sort as if LatLonDVField had been used at indexing time:
        del data['GeoPoint']
      except KeyError:
        pass
    writeOneGraph(data, 'search-%s' % key, title, 'M hits/sec', allTimes)

  writeOneGraph(indexKDPS, 'index-times', 'Indexing K docs/sec', 'K docs/sec', allTimes)
  writeOneGraph(readerHeapMB, 'reader-heap', 'Searcher Heap Usage', 'MB', allTimes)
  writeOneGraph(indexMB, 'index-size', 'Index Size', 'MB', allTimes)

  f.write('''


''')

with pysftp.Connection('home.apache.org', username='mikemccand') as c:
  with c.cd('public_html'):
    c.put('/x/tmp/geobench.html', 'geobench.html')
github tjcsl / ion / intranet / apps / files / views.py View on Github external
def create_session(hostname, username, password):
    cnopts = pysftp.CnOpts()
    cnopts.hostkeys = None
    return pysftp.Connection(hostname, username=username, password=password, cnopts=cnopts)
github ubuntupodcast / podpublish / podpublish / uploader.py View on Github external
def _sftp_put_file(config, cinfo, file_in):
    with pysftp.Connection(**cinfo) as sftp:
        print("Connected to: " + config.sftp['host'])
        print("Making directory: " + config.sftp['remote_directory'])
        sftp.makedirs(config.sftp['remote_directory'])
        with sftp.cd(config.sftp['remote_directory']):
            print("Changed to: " + config.sftp['remote_directory'])
            print("Putting: " + file_in)
            sftp.put(file_in)
            print("Setting permissions: 644")
            sftp.chmod(file_in, 644)
github jamesrobertlloyd / gp-structure-search / source / sandpit.py View on Github external
def fear_connect():
    return pysftp.Connection('fear', username=config.USERNAME, password=config.PASSWORD)