def login(host, username, password): """Processes the login request specified. Note that pysftp handles all edge cases neatly, raising well-documented exceptions if any problems are found (e.g., Invalid hostname, username, password, etc.). """ return pysftp.Connection(host=host, username=username, password=password)
import pysftp cnopts = pysftp.CnOpts() cnopts.hostkeys = None dir_path = '/data2/home/zhouxiangyong/Workspace/Dev/AortaSlice/data/aorta_extract_data1_fxc' with pysftp.Connection('s108', port=10822, username='******', password='******', cnopts=cnopts) as sftp: # out = sftp.execute('cd {}; ls'.format(dir_path)) sftp.cwd(dir_path) out = sftp.listdir() print(out)
print ('Sauvegarde de la configuration Nginx effectué') # Backup wordpress os.system("tar -zcvf " + bck_dir + "/wordpress_content-" + pipes.quote(DATETIME) + ".tar.gz /var/www/wordpress/wp-content") os.system("tar -zcvf " + bck_dir + "/wordpress_admin-" + pipes.quote(DATETIME) + ".tar.gz /var/www/wordpress/wp-admin") os.system("tar -zcvf " + bck_dir + "/wordpress_cfg-" + pipes.quote(DATETIME) + ".tar.gz /var/www/wordpress/wp-config.php") print ( ) print ('Sauvegarde de Wordpress effectué') # Transfert du backup, du script de sauvegarde et de crontab with pysftp.Connection(sftp_ip, username=sftp_id) as sftp: with sftp.cd(sftp_dir): sftp.put(bck_dir + 'wordpress-' + pipes.quote(DATETIME) + '.sql') sftp.put(bck_dir + 'wordpress_content-' + pipes.quote(DATETIME) + '.tar.gz') sftp.put(bck_dir + 'wordpress_admin-' + pipes.quote(DATETIME) + '.tar.gz') sftp.put(bck_dir + 'wordpress_cfg-' + pipes.quote(DATETIME) + '.tar.gz') sftp.put(bck_dir + 'nginx_enb-' + pipes.quote(DATETIME) + '.tar.gz') sftp.put(bck_dir + 'nginx_av-' + pipes.quote(DATETIME) + '.tar.gz') sftp.put('/opt/backup_restore.py') sftp.put('/etc/cron.d/root') print ( ) print ('Transfert des sauvegardes effectué') # Supression des fichiers de plus de 30 jours
import pysftp with pysftp.Connection('roborio-3260.local', username='******', password="******") as sftp: sftp.put("/home/lucas/Desktop/autonomousVariables.csv") sftp.close()
def run(self, config_box: Box, cnopts: pysftp.CnOpts = None, **format_kwargs: Any) -> pd.DataFrame: with prefect.context(**format_kwargs) as data: hostname = config_box["hostname"] username = config_box["username"] remoterootpath = config_box["target_dir"] if data.get("parameters"): if data.parameters.get("cnopts"): cnopts = data.parameters["cnopts"] if config_box.get("private_key_path"): private_key = config_box["private_key_path"] if config_box.get("private_key_passphrase"): # has a passphrase, use it private_key_passphrase = config_box[ "private_key_passphrase"] sftp = pysftp.Connection( host=hostname, username=username, private_key=private_key, private_key_pass=private_key_passphrase, cnopts=cnopts, ) else: sftp = pysftp.Connection( host=hostname, username=username, private_key=private_key, cnopts=cnopts, ) elif config_box.get("password"): password = config_box["password"] sftp = pysftp.Connection(host=hostname, username=username, password=password, cnopts=cnopts) files_data = [] try: with sftp.cd(remoterootpath): for f in sftp.listdir(): # Extra dirs like wip and done require us to check if we're just looking at files if sftp.isfile(f): # get the dates from the ftp site itself sftpattrs = sftp.stat(f) files_data.append({ "File Name": f, "MTime": datetime.datetime.fromtimestamp( sftpattrs.st_mtime), }) finally: sftp.close() filesdf = pd.DataFrame(files_data, columns=["File Name", "MTime"]) self.logger.info("Found {} files to process.".format( len(filesdf.index))) return filesdf
percent = float(transferred) / toBeTransferred * 100 print '%0.1f' % (percent), '%' try: pass except Exception as exp: print(type(exp)) print(exp) pass try: srv = pysftp.Connection('raspberrypi3', username='******', password='******', cnopts=cnopts, port=22) # src.timeout(1) srv.chdir('ssuhrid/lan/data') # Get the directory and file listing # data = src.listdir() # # Prints out the directories and files, line by line # for i in data: # print i start = time.time() print 'start' srv.put('data/video.mkv', callback=printTotals) end = time.time()
def __init__(self, host, port, user, password=None): self.__connection = pysftp.Connection(host, username=user, password=password) self.__url_maker = UrlMaker(self.__protocol, host, user, password, port)
elif key == 'box': title = 'Box Filter' elif key == 'nearest 10': title = '10 Nearest Points' elif key == 'sort': title = 'Box Filter, Sort by Distance' else: raise RuntimeError('unknown chart %s' % key) if key == 'sort': try: # 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(''' </body> </html> ''') with pysftp.Connection('home.apache.org', username='******') as c: with c.cd('public_html'): c.put('/x/tmp/geobench.html', 'geobench.html')
for conndir in _conndir: for opdir in _opdir: if conndir.ref == opdir.conn_ref: conndir.add_op(opdir) _opdir = [] conns_with_ops = [] for conndir in _conndir: if conndir.ops: conns_with_ops.append(conndir) _conndir = conns_with_ops for conndir in _conndir: try: if conndir.protocol == 'sftp': with pysftp.Connection(host=conndir.host, username=conndir.username, password=conndir.password, port=conndir.port) as conn: conndir.set_access_time() log("Connection with host '{}' established at {}". format(conndir.host, conndir.access_time)) for opdir in conndir.ops: opdir.args = format_args(opdir.args, start) choose_func(conn, conndir, opdir) log("Connection with host '{}' terminated at {}". format(conndir.host, str(dt.datetime.now()))) elif conndir.protocol == 'local': conndir.set_access_time() log("Local host operations begun at at {}".format( conndir.access_time)) for opdir in conndir.ops: opdir.args = format_args(opdir.args, start)
def transfer(self, ip, passwd, src, dst, action='push', user='******', port=36000, timeout=60): # 忽略hostkeys错误 cnopts = pysftp.CnOpts() cnopts.hostkeys = None # 若src以斜杠结尾,则去掉这个斜杠,是否是目录后面会有判断逻辑 if src[-1] == '/': src = src[0:-1] try: with pysftp.Connection(ip, username=user, password=passwd, port=int(port), cnopts=cnopts) as sftp: # 拉取文件或目录 if action == 'pull': try: # 判断远程来源是目录还文件 if sftp.isdir(src): # 判断本地目录是否存在,若不存在则创建 if not os.path.exists(dst): try: os.makedirs(dst) except Exception as e: print e pass # 若为目录则分别取得父目录和需要操作的目录路径,进入父目录,然后执行sftp parent_dir = src.rsplit('/', 1)[0] opt_dir = src.rsplit('/', 1)[1] sftp.chdir(parent_dir) sftp.get_r(opt_dir, dst, preserve_mtime=True) else: # 拉取src远程文件到dst本地文件夹 if dst[-1] == '/': # 判断本地目录是否存在,若不存在则创建 if not os.path.exists(dst): try: os.makedirs(dst) except Exception as e: print e pass os.chdir(dst) sftp.get(src, preserve_mtime=True) # 拉取src远程文件到dst本地文件 else: file_dir = dst.rsplit('/', 1)[0] dst_file = dst.rsplit('/', 1)[1] # 取得目标文件名称 # 判断本地目录是否存在,若不存在则创建 if not os.path.exists(file_dir): try: os.makedirs(file_dir) except Exception as e: print e pass os.chdir(file_dir) sftp.get(src, dst_file, preserve_mtime=True) except Exception as e: return 1, e else: try: # 判断本地文件是目录还是文件,若是目录则使用put_r 递归推送 if os.path.isdir(src): # 判断目的目录是否存在,若不存在则创建 if not sftp.exists(dst): try: sftp.makedirs(dst) except Exception as e: print e pass sftp.put_r(src, dst, preserve_mtime=True) # 否则先进入目标目录,然后使用put单文件推送 else: # 推送src源文件到dst目的文件夹 if dst[-1] == '/': # 判断目的目录是否存在,若不存在则创建 if not sftp.exists(dst): try: sftp.makedirs(dst) except Exception as e: print e pass sftp.chdir(dst) sftp.put(src, preserve_mtime=True) # 推送src源文件到dst目的文件 else: file_dir = dst.rsplit('/', 1)[0] # 判断目的目录是否存在,若不存在则创建 if not sftp.exists(file_dir): try: sftp.makedirs(file_dir) except Exception as e: print e pass sftp.chdir(file_dir) sftp.put(src, dst, preserve_mtime=True) except Exception as e: return 1, e return 0, 'success' except socket.timeout as e: return 502, e except paramiko.ssh_exception.AuthenticationException: print "Password [%s] error" % passwd client.close() return 403, "Password [%s] error" % passwd except Exception as e: print e # 系统重装后会出现hostkey验证失败问题,需要先删除known_hosts中记录 if "Host key for server" in str(e): os.system('sed -i "/^\[%s].*/d" ~/.ssh/known_hosts' % ip) client.close() return 503, 'Hostkeys Error' else: client.close() return 1, e
import pysftp import sys import string cnopts = pysftp.CnOpts(knownhosts='C:\\Simulations\\Launcher\\known_hosts.txt') with pysftp.Connection(host="newriver1.arc.vt.edu", username='******', private_key="C:\\Simulations\\Launcher\\newriver2", cnopts=cnopts) as sftp: print("Connection succesfully stablished ... ") path = sys.argv[1] localpath = path.replace("\\", "\\\\") name_index = path.rfind("\\") name = path[name_index + 1:] if sftp.isdir(path[15:name_index].replace("\\", "/")) != True: sftp.makedirs(path[15:name_index].replace("\\", "/")) remotepath = "/home/knightkp" + path[14:].replace("\\", "/") sftp.put(path, remotepath) defaults = input("Accept Defaults (Y or N): ") if defaults == "Y" or defaults == 'y': walltime = "2:00:00" nodes = 1 cores = 8 queue = "dev_q" job_name = name
import pysftp sftp = pysftp.Connection('192.168.159.131', username='******', password='******') sftp.cd('/home/user/Desktop/files') sftp.get('a.txt')
def secure_FTP(self, destination_folder='/mnt/dmz/atlas_provider_1'): sftp = pysftp.Connection(**self.cinfo) sftp.cwd(destination_folder) print 'set current working directory -', sftp.getcwd() return sftp
print ("recording stopped") stream.stop_stream() stream.close() audio.terminate() waveFile = wave.open(WAVE_OUTPUT_DIR + WAVE_OUTPUT_FILENAME, 'wb') waveFile.setnchannels(CHANNELS) waveFile.setsampwidth(audio.get_sample_size(FORMAT)) waveFile.setframerate(RATE) waveFile.writeframes(b''.join(Recordframes)) waveFile.close() # ftp this to the main server import pysftp with pysftp.Connection (host='45.76.113.79', username='******') as sftp: sftp.put (WAVE_OUTPUT_DIR + WAVE_OUTPUT_FILENAME, '/home/ftpuser/ftp/files/' + WAVE_OUTPUT_FILENAME) # then delete it os.remove(WAVE_OUTPUT_DIR + WAVE_OUTPUT_FILENAME) sys.exit(0)
def schedule_backup(self): conf_ids = self.search([]) for rec in conf_ids: db_list = self.get_db_list(rec.host, rec.port) if rec.name in db_list: try: if not os.path.isdir(rec.folder): os.makedirs(rec.folder) except Exception: raise # Create name for dumpfile. bkp_file = '%s_%s.%s' % (time.strftime('%d_%m_%Y_%H_%M_%S'), rec.name, rec.backup_type) file_path = os.path.join(rec.folder, bkp_file) uri = 'http://' + rec.host + ':' + rec.port try: bkp_resp = requests.post(uri + '/web/database/backup', stream=True, data={ 'master_pwd': tools.config['admin_passwd'], 'name': rec.name, 'backup_format': rec.backup_type }) bkp_resp.raise_for_status() except Exception: _logger.debug( "Couldn't backup database %s. Bad database " "administrator password for server running at " "http://%s:%s" % (rec.name, rec.host, rec.port)) continue with open(file_path, 'wb') as fp: # see https://github.com/kennethreitz/requests/issues/2155 bkp_resp.raw.read = functools.partial(bkp_resp.raw.read, decode_content=True) shutil.copyfileobj(bkp_resp.raw, fp) else: _logger.debug("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.sftp_write is True: try: # Store all values in variables directory = rec.folder path_to_write_to = rec.sftp_path ip_host = rec.sftp_host post_host = rec.sftp_port username_login = rec.sftp_user password_login = rec.sftp_password # Connect with external server over SFTP srv = pysftp.Connection(host=ip_host, username=username_login, password=password_login, port=post_host) # 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 path_to_write_to = re.sub('([/]{2,5})+', '/', path_to_write_to) _logger.debug('sftp remote path: %s' % path_to_write_to) try: srv.chdir(path_to_write_to) except IOError: # Create directory and subdirs if they do not exist. current_dir = '' for dirElement in path_to_write_to.split('/'): current_dir += dirElement + '/' try: srv.chdir(current_dir) except: _logger.info( '(Part of the) path didn\'t exist. ' 'Creating it now at ' + current_dir) # Make directory and then navigate into it srv.mkdir(current_dir, mode=777) srv.chdir(current_dir) pass srv.chdir(path_to_write_to) # Loop over all files in the directory. for f in os.listdir(directory): if rec.name in f: fullpath = os.path.join(directory, f) if os.path.isfile(fullpath): if not srv.exists(f): _logger.info( 'The file %s is not yet on the remote ' 'FTP Server ------ Copying file' % fullpath) srv.put(fullpath) _logger.info( 'Copying File % s------ success' % fullpath) else: _logger.debug( 'File %s already exists on the remote ' 'FTP Server ------ skipped' % fullpath) # Navigate in to the correct folder. srv.chdir(path_to_write_to) # Loop over all files in the directory from the back-ups. # We will check the creation date of every back-up. for file_in_dic in srv.listdir(path_to_write_to): if rec.name in file_in_dic: # Get the full path fullpath = os.path.join(path_to_write_to, file_in_dic) # 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 days_to_keep_sftp # (the days to keep that the user filled in on the # Odoo form it will be removed. if delta.days >= rec.days_to_keep_sftp: # Only delete files, no directories! if srv.isfile(fullpath) and ( ".dump" in file_in_dic or '.zip' in file_in_dic): _logger.info( "Delete too old file from SFTP " "servers: " + file_in_dic) srv.unlink(file_in_dic) # 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.send_mail_sftp_fail: # TODO This code is not working for version 10! try: ir_mail_server = self.pool.get('ir.mail_server') message = "Dear,\n\nThe backup for the server " +\ rec.host + " (IP: " + rec.sftp_host +\ ") failed.Please check the following " \ "details:\n\nIP address SFTP server: " +\ rec.sftp_host + "\nUsername: "******"\nPassword: "******"\n\nError details: " + \ tools.ustr(e) + "\n\nWith kind regards" msg = ir_mail_server.build_email( "pgsql_auto_backup@" + rec.name + ".com", [rec.email_to_notify], "Backup from " + rec.host + "(" + rec.sftp_host + ") failed", message) ir_mail_server.send_email(self.env.cr, self.env.user, msg) except Exception: pass # Remove all old files (on local server) in case this is # configured.. if rec.autoremove: directory = rec.folder # Loop over all files in the directory. for f in os.listdir(directory): fullpath = os.path.join(directory, f) # Only delete the ones wich are from the current database # (Makes it possible to save different databases in the # same folder) if rec.name in fullpath: timestamp = os.stat(fullpath).st_ctime createtime = datetime.datetime.fromtimestamp(timestamp) now = datetime.datetime.now() delta = now - createtime if delta.days >= rec.days_to_keep: # Only delete files (which are .dump and .zip), # no directories. if os.path.isfile(fullpath) and (".dump" in f or '.zip' in f): _logger.info( "Delete local out-of-date file: " + fullpath) os.remove(fullpath)
import pysftp # Create credentials.py to store sftp credentials as dict object import credentials as cred sftp = pysftp.Connection( 'hostname', username = cred.dict_obj['username'], password = cred.dict_obj['password'], private_key = 'absolute_filepath_for_sshkey.ppk' ) # Change working directory once connection is established with sftp.cd('/parent_dir'): sftp.chdir('child_dir') sftp.chdir('grandchild_dir') # Put the my_csv.csv file into specified directory sftp.put('/absolute_filepath/my_csv.csv', preserve_mtime=True) # Check that it wsa successfully put there assert sftp.exists('my_csv.csv'), 'Upload unsuccessful' # Close connection sftp.close()
int(100 * (int(x) / int(y))))] == "": print("{}% ({} Transfered(B)/ {} Total File Size(B))".format( str("%.2f" % (100 * (int(x) / int(y)))), x, y)) progressDict[str(int(100 * (int(x) / int(y))))] = "1" with open(os.path.join(sys.path[0], "upload_sftp.json"), 'r') as f: config = json.load(f) print(config) localpath = sys.argv[1] """remotepath: the destination path on the SFTP server. Note that the filename should be included. Only specifying a directory may result in an error.""" remotepath = sys.argv[2] print(localpath) print(remotepath) cnopts = pysftp.CnOpts() cnopts.hostkeys = None srv = pysftp.Connection(host=config["host"], port=config["port"], username=config["username"], password=config["password"], cnopts=cnopts) srv.put(localpath, remotepath, callback=printProgressDecimal) srv.close()
def main(): arguments = docopt(__doc__) verbose = arguments['--verbose'] debug = arguments['--debug'] dryrun = arguments['--dry-run'] quiet = arguments['--quiet'] study = arguments['<study>'] # setup logging log_level = logging.WARN if quiet: log_level = logging.ERROR if verbose: log_level = logging.INFO if debug: log_level = logging.DEBUG logging.getLogger().setLevel(log_level) # setup the config object cfg = datman.config.config(study=study) zips_path = cfg.get_path('zips') meta_path = cfg.get_path('meta') # Check the local project zips dir exists, create if not if not os.path.isdir(zips_path): logger.warning('Zips directory: {} not found; creating.' .format(zips_path)) if not dryrun: os.mkdir(zips_path) server_config = get_server_config(cfg) for mrserver in server_config: mrusers, mrfolders, pass_file_name, port = server_config[mrserver] if isinstance(mrfolders, str): mrfolders = [mrfolders] if isinstance(mrusers, str): mrusers = [mrusers] pass_file = os.path.join(meta_path, pass_file_name) passwords = read_password(pass_file) # actually do the copying assert len(passwords) == len(mrusers), \ 'Each mruser in config should have and entry in the password file' for iloc in range(len(mrusers)): mruser = mrusers[iloc] password = passwords[iloc] with pysftp.Connection(mrserver, username=mruser, password=password, port=port) as sftp: valid_dirs = get_valid_remote_dirs(sftp, mrfolders) if len(valid_dirs) < 1: logger.error('Source folders {} not found' ''.format(mrfolders)) for valid_dir in valid_dirs: # process each folder in turn logger.debug('Copying from:{} to:{}' .format(valid_dir, zips_path)) process_dir(sftp, valid_dir, zips_path)
logging.debug('------------------------------------') logging.debug('---- Starting sending process -----') logging.debug('------------------------------------') #deshabilito hostkeys chequeo cnopts = pysftp.CnOpts() cnopts.hostkeys = None # disable host key checking. logging.debug('Preparing connection') try: #connection setup srv = pysftp.Connection(host="hostname", username="******", password="******", port=19219, cnopts=cnopts) logging.debug('Connection established: <%s>', srv) logging.debug('----------------------------------') except Exception: logging.error(traceback.format_exc()) #logging.error('timeout <%s> exceeded! exiting program ',seconds) print("Unexpected error:", sys.exc_info()[0]) sys.exit() os.chdir("/home/backups")
def sshs(self, inst, user='******', keyfile='~/.ssh/id_rsa'): inst = self.get_instance(inst) ssh = self.ssh(inst, user=user, keyfile=keyfile) ftp = pysftp.Connection(ssh) return inst, ssh, ftp
print "File " + f.filename + " was copied over" #print "Put " + f.fullpath + " into " + cur_remote_dir pass for f in files_only_remote: #Remove file from remote if not test_run: sftp.remove(f.fullpath) print "File " + f.filename + " was removed from the server" pass if __name__ == "__main__": parser = argparse.ArgumentParser(description='-test (optional)') parser.add_argument('-t','--test', help='Specify the test option to mock a deploy (only logs what would happen, without actually doing anything)', action='store_true', required=False) opts = parser.parse_args() test_run = opts.test if(test_run): print("TEST RUNNING DEPLOY SCRIPT") else: print("RUNNING DEPLOY SCRIPT") with pysftp.Connection(hostname, username=username, password=password) as sftp: comp_dirs("/", sftp)
def testslot(request, pid, vsid): if request.method == 'GET': finish_listen = request.GET.get('listened', 'notyet') fail_select = request.GET.get('fail_select', False) p = get_object_or_404(Project, pk=pid) slot = get_object_or_404(VoiceSlot, pk=vsid) if p.bravo_server: try: with pysftp.Connection( p.bravo_server.address, username=p.bravo_server.account, private_key=settings.PRIVATE_KEY) as conn: remote_path = slot.filepath() filename = "{0}.wav".format(str(uuid.uuid4())) filenamezip = filename + ".gz" local_path = os.path.join(settings.MEDIA_ROOT, filename) local_path_zip = os.path.join(settings.MEDIA_ROOT, filenamezip) if conn.exists(remote_path): conn.get(remote_path, local_path) else: conn.get(remote_path + ".gz", local_path_zip) with gzip.open(local_path_zip, 'rb') as in_file: out_file = open(local_path, 'wb') s = in_file.read() with open(local_path, 'w') as f: f.write(s) filepath = settings.MEDIA_URL + filename slot.check_out(request.user) except IOError as e: messages.danger( request, "File missing on server \"{0}\"".format( p.bravo_server.name)) slot.status = VoiceSlot.MISSING slot.save() Action.log(request.user, Action.AUTO_MISSING_SLOT, 'Slot found missing by individual slot test', slot) return redirect("projects:project", pid) except pysftp.ConnectionException: messages.danger( request, "Connection error to server \"{0}\"".format( p.bravo_server.name)) return redirect("projects:project", pid) except pysftp.CredentialException: messages.danger( request, "Credentials error to server \"{0}\"".format( p.bravo_server.name)) return redirect("projects:project", pid) except pysftp.AuthenticationException: messages.danger( request, "Authentication error to server \"{0}\"".format( p.bravo_server.name)) return redirect("projects:project", pid) except pysftp.SSHException: messages.danger( request, "SSH error to server \"{0}\"".format(p.bravo_server.name)) return redirect("projects:project", pid) return render( request, "projects/testslot.html", contexts.context_testslot(request.user_agent.browser, p, slot, filepath, finish_listen, fail_select)) messages.danger(request, "No server associated with project") return redirect("projects:project", pid) return submitslot(request, vsid)
def run(self, workfile: str, config_box: Box, source="root", target="wip", cnopts: pysftp.CnOpts = None, **format_kwargs: Any): with prefect.context(**format_kwargs) as data: # This moves (renames) files around on the SFTP site. # It's helpful for managing "state" as the files are processed locally. hostname = config["hostname"] username = config["username"] remoterootpath = config["target_dir"] if data.get("parameters"): if data.parameters.get("cnopts"): cnopts = data.parameters["cnopts"] if config_box.get("private_key_path"): private_key = config_box["private_key_path"] if config_box.get("private_key_passphrase"): # has a passphrase, use it private_key_passphrase = config_box[ "private_key_passphrase"] sftp = pysftp.Connection( host=hostname, username=username, private_key=private_key, private_key_pass=private_key_passphrase, cnopts=cnopts, ) else: sftp = pysftp.Connection( host=hostname, username=username, private_key=private_key, cnopts=cnopts, ) elif config_box.get("password"): password = config_box["password"] sftp = pysftp.Connection(host=hostname, username=username, password=password, cnopts=cnopts) # "root" is special if source == "root": remotesourcepath = remoterootpath else: remotesourcepath = os.path.join(remoterootpath, source) remotetargetpath = os.path.join(remoterootpath, target) logger.debug("Moving %s from %s to %s" % (filename, remotesourcepath, remotetargetpath)) # Move the file from source to target on the SFTP try: with sftp.cd(remoterootpath): if not sftp.isfile(os.path.join(remotesourcepath, filename)): # Working in notebooks you might have already moved the file in another block logger.warning( "The file %s isn't in the remote folder." % os.path.join(remotesourcepath, filename)) else: if not sftp.isdir(remotetargetpath): sftp.mkdir(remotetargetpath) if not sftp.isfile( os.path.join(remotetargetpath, filename)): # The file might already be in DONE... sftp.rename( os.path.join(remotesourcepath, filename), os.path.join(remotetargetpath, filename), ) finally: sftp.close()
# Connect to transfer.retailpro.com and download PrismCentralManager executables # By Zach Cutberth import pysftp import config cnopts = pysftp.CnOpts() cnopts.hostkeys = None srv = pysftp.Connection(host='transfer.retailpro.com', username=config.sftp_user, password=config.sftp_pass, cnopts=cnopts) srv.get_d('/Kanmo/PrismCentralManager', 'c:\\rpsupport\\PrismCentralManager\\', True) srv.close()
#Upload a list of video files to pylon5 and check if files already exist (pseudo-code example) #Imports for sftp: import pysftp #Define videos to send to server newfns = list_of_local_video_file_paths #gathered in whatever way you see fit #Initialize connection to PSC pylon5: remote_loc = '/pylon5/grant_id/userid/videos' #This is your intended destination on pylon5 srvr = pysftp.Connection('data.bridges.psc.edu', username='******', password='******', port=22) srvr.cwd(remote_loc) for i, d in enumerate(newfns): if srvr.exists(newfns[i]): print('%d) %s found, skipping.' % (i, newfns[i])) else: print('%d) moving %s....' % (i, newfns[i])) srvr.put(d, newfns[i]) print(' Finished.') print('Finished all! Files moved!') f = srvr.listdir() #for i in f: # print(i) srvr.close()
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.%s' % (time.strftime('%d_%m_%Y_%H_%M_%S'), rec.name, rec.backup_type) file_path = os.path.join(rec.bkp_dir, bkp_file) uri = 'http://' + rec.host + ':' + rec.port bkp = '' try: bkp_resp = requests.post(uri + '/web/database/backup', stream=True, data={ 'master_pwd': tools.config['admin_passwd'], 'name': rec.name, 'backup_format': rec.backup_type }) bkp_resp.raise_for_status() except: _logger.debug( "Couldn't backup database %s. Bad database administrator password for server running at http://%s:%s" % (rec.name, rec.host, rec.port)) continue with open(file_path, 'wb') as fp: # see https://github.com/kennethreitz/requests/issues/2155 bkp_resp.raw.read = functools.partial(bkp_resp.raw.read, decode_content=True) shutil.copyfileobj(bkp_resp.raw, fp) else: _logger.debug("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 portHost = rec.sftpport usernameLogin = rec.sftpusername passwordLogin = rec.sftppassword #Connect with external server over SFTP srv = pysftp.Connection(host=ipHost, username=usernameLogin, password=passwordLogin, port=portHost) #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) _logger.debug('sftp remote path: %s' % 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: _logger.info( '(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): if not srv.exists(f): _logger.info( 'The file %s is not yet on the remote FTP Server ------ Copying file' % fullpath) srv.put(fullpath) _logger.info('Copying File % s------ success' % fullpath) else: _logger.debug( 'File %s already exists on the remote FTP Server ------ skipped' % 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: _logger.info( "Delete too old file from SFTP servers: " + 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: "******"\nPassword: "******"\n\nError details: " + tools.ustr( e) + "\n\nWith kind regards" msg = ir_mail_server.build_email( "auto_backup@" + 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: _logger.info("Delete local out-of-date file: " + fullpath) os.remove(fullpath)
id_reclamo = params[2] path_local_base = '/sistemas/' + server_local_path + '/public_html/tmp/reclamos/' path_remote_base = '/sistemas/tmp/reclamos/' imagen01 = params[0] imagen02 = params[1] img1 = str(id_reclamo) + '-cargo.jpg' img2 = str(id_reclamo) + '-imagen.jpg' try: srv = pysftp.Connection(host=server_host, username=server_user, password=server_pass, port=server_port) if srv.exists(path_remote_base): ''' I'll to remote directory ''' srv.chdir(path_remote_base) if srv.exists(imagen01): srv.execute('convert ' + imagen01 + ' ' + path_remote_base + img1) if srv.exists(img1): srv.get(img1, path_local_base + img1) srv.remove(img1) if srv.exists(imagen02): srv.execute('convert ' + imagen02 + ' ' + path_remote_base + img2) if srv.exists(img2):
rel205_path = '/Volumes/ice2/suzanne/ATL03_rel205/GreenlandSea/' filenames = [ f for f in os.listdir(rel205_path) if os.path.isfile(os.path.join(rel205_path, f)) ] print(filenames) # set strings for path composition prod = 'ATL03/' rel = 'rel001/' fpath = '/data1/' + prod + rel print(fpath) # domain name try: srv = pysftp.Connection(host='scf.gsfc.nasa.gov', username='******', private_key='/Users/suzanne/.ssh/id_rsa') # print(srv.listdir()) srv.chdir(fpath) # print(srv.pwd) # print(srv.listdir()) srv.close() except Exception as e: print('darn', e) #for i in data: # print(i) #ftp = ftplib.FTP('my.cmems-du.eu') #ftp.login('sdickinson1','cloudy66,') #path = 'Core/SEALEVEL_GLO_PHY_L4_REP_OBSERVATIONS_008_047/' #dataset-duacs-rep-global-merged-allsat-phy-l4' #ftp.cwd(path)
rePATH="/ethtorrent/downloads/complete/TEST/" #remote path lcPATH="/home/ego/TEST/" #local path dirList=os.listdir(lcPATH) #list of all the file in lcPATH tmpPATH=lcPATH+".tmp" syncList="" #initialise the sync varialbe, needed to compare if a file # .... have been already downladed fileExist=0 #initialize the varialbe needed to compare the sync list and # .... the file in rePATH dir #connect to the server through sftp, using credential !!!!! need to create a credential file, for more security. with pysftp.Connection(ftpIP, username=ftpUSER, password=ftpPWD, cnopts=cnopts) as sftp: with sftp.cd(rePATH): #go to the directory rePATH tmp = sftp.listdir('') #list the files present in rePATH (DIR remote) #try if the .sync path / directory exist, otherwise create it try: os.stat(lcPATH + ".sync") except: os.mkdir(lcPATH + ".sync") syncList = os.listdir(lcPATH + ".sync") #create the list of the file in .sync (DIR local) #test if the file is present in local or as already been downloaded for i in range(len(tmp)): fileExist=0
def download_from_sftp(dst, host, host_path, user, password, post=None): with pysftp.Connection(host, username=user, password=password) as sftp: sftp.get(host_path, localpath=dst) if post is not None: post(dst)