def connect_ftps(): #Connect to the server print("Step 1") if SECURE_CONN == '1': ftps = FTP_TLS(SERVER) print("Secure connection") else: ftps = FTP(SERVER) print("Unsecure connection") print("Step 2") ftps.set_debuglevel(2) print("Step 3") ftps.set_pasv(False) print("Step 4") ftps.connect(port=int(PORT), timeout=160) print("Step 5") ftps.login(USER, PASS) print("Step 6") ftps.cwd(TARGET_DIR) print("Step 7") ftps.set_pasv(True) print("Step 8") #Set up the secure connection only in FTPS mode if SECURE_CONN == '1': ftps.prot_p() else: pass #ftps.ccc() return ftps
def ftps_connect(host): ftps = FTP_TLS() ftps.ssl_version = ssl.PROTOCOL_SSLv23 ftps.connect(host) ftps.login(settings.NC_FTP_USER, settings.NC_FTP_PASSWORD) ftps.prot_p() return ftps
def sendPackagesFtp(): print "---- Send packages by FTP" global serverFtp global userFtp global passFtp from ftplib import FTP_TLS ftps = FTP_TLS(serverFtp) ftps.set_debuglevel(1) ftps.login(userFtp, passFtp) ftps.prot_p() try: ftps.sendcmd('MKD ' + '/files/' + strProductVer) except Exception: print 'Directory already exists' ftps.cwd('/files/' + strProductVer) filesListFtp = ftps.nlst() filesList = os.listdir(packagesPath) newFilesList = [e for e in filesList if not(e in filesListFtp)] for fileName in newFilesList: ftps.storbinary('STOR ' + fileName, open(packagesPath + '\\' + fileName, 'rb')) ftps.quit()
def get_ftp(self): if self.protocol == 'secure': _ftp = FTP_TLS(self.host, self.user, self.pw) _ftp.prot_p() else: _ftp = FTP(self.host, self.user, self.pw) return _ftp
def write_file(self): output = open('chan_output.htm', 'w') for channel in self.main.channels: self.logger.log(LOG_DEBUG, "Writing output for %s, topic %s" % (channel, self.topics[channel])) output.write("<b>Channel:</b> %s\n<br /><b>Topic:</b> %s\n<br /><b>Users:</b>\n<ul>\n" % (channel, self.topics[channel])) for user in self.main.channels[channel]['users']: output.write(" <li>%s</li>\n" %(user)) output.write("</ul>\n\n") output.close output = open('chan_output.htm', 'r') self.update_all = 0 ftp_server = 'whub25.webhostinghub.com' ftp_user = '******' passfile = open('password.txt','r') password = passfile.readline() passfile.close() password = password.rstrip('\n') self.logger.log(LOG_INFO, "Connecting to FTP server %s, username %s" % (ftp_server, ftp_user)) ftp = FTP_TLS(ftp_server, ftp_user, password) ftp.prot_p() self.logger.log(LOG_INFO, "Successfully logged in, storing file") ftp.storlines('STOR chan_output.htm', output) ftp.close() output.close() self.update_all = 0 self.logger.log(LOG_INFO, "Done")
def _open_ftp(self): # type: () -> FTP """Open a new ftp object.""" _ftp = FTP_TLS() if self.tls else FTP() _ftp.set_debuglevel(0) with ftp_errors(self): _ftp.connect(self.host, self.port, self.timeout) _ftp.login(self.user, self.passwd, self.acct) try: _ftp.prot_p() # type: ignore except AttributeError: pass self._features = {} try: feat_response = _decode(_ftp.sendcmd("FEAT"), "latin-1") except error_perm: # pragma: no cover self.encoding = "latin-1" else: self._features = self._parse_features(feat_response) self.encoding = "utf-8" if "UTF8" in self._features else "latin-1" if not PY2: _ftp.file = _ftp.sock.makefile( # type: ignore "r", encoding=self.encoding) _ftp.encoding = self.encoding self._welcome = _ftp.welcome return _ftp
def download_npc(): ftp_files = [] os_files = [] try: ftps = FTP_TLS() ftps.connect(CFG_FTPS_HOST, CFG_FTPS_PORT) log_it( 'connected to ' + CFG_FTPS_HOST + ' welcome message: ' + str(ftps.getwelcome()), 'info') ftps.login(CFG_FTPS_USER, CFG_FTPS_PASS) ftps.prot_p() log_it('changing dir: ' + CFG_FTPS_DIR, 'info') ftps.cwd(CFG_FTPS_DIR) ftp_files = ftps.nlst() for f in ftp_files: if not os.path.isfile(CFG_ARCHIVE_DIR + f): ftps.retrbinary('RETR ' + f, open(CFG_ARCHIVE_DIR + f, 'wb').write) log_it('downloading file ' + f, 'info') else: log_it( 'skipping ' + f + ' as it already exists in ' + CFG_ARCHIVE_DIR, 'debug') except ftplib.all_errors, e: log_it('unable to connect to ' + CFG_FTPS_HOST + ' %s' % e, 'error')
def sync(self): """ downloads all needed_files from self.hostname (FTP) of the downloaded files, extracts .gz files to same local_working_dir -using self.extract function parses the .txt downloaded needed_files -using the self.parse function """ ftps = FTP_TLS(self.hostname) # connect to host, default port ftps.login(self.username, self.password) ftps.prot_p() ftps.cwd(self.remote_dir) # change into "logs" directory ftps.retrlines('LIST *.gz *.txt', self.ftp_list_callback) # list directory contents for needed_file in self.needed_files: if self.logging: print "Writing {0} to {1}...".format(needed_file, self.local_working_dir) ftps.retrbinary("RETR " + needed_file, open(os.path.join(self.local_working_dir, needed_file), 'wb').write) if self.logging: print "done syncing files" for needed_file in self.needed_files: if needed_file.endswith(".gz"): self.extract(os.path.join(self.local_working_dir, needed_file)) txt_file_name = needed_file.replace('.gz','')#if already a .txt file, this is unnceccessary but works. self.parse(txt_file_name) if self.logging: print "done extracting/parsing .gz files" ftps.quit()
def __connectTls(self, host, user, password): ftp_tls = FTP_TLS(host) ftp_tls.login(user, password) ftp_tls.prot_p() return ftp_tls
def sendUpdateFilesFtp(): print "---- Send update files by FTP" global serverFtp global userFtp global passFtp from ftplib import FTP_TLS ftps = FTP_TLS(serverFtp) ftps.set_debuglevel(1) ftps.login(userFtp, passFtp) ftps.prot_p() ftps.cwd('/files/updates') ftps.storbinary('STOR ' + 'file_list.md5', open(quiterssFileRepoPath + '\\file_list.md5', 'rb')) ftps.storbinary('STOR ' + 'VersionNo.h', open(quiterssSourcePath + '\\src\\VersionNo.h', 'rb')) ftps.storbinary('STOR ' + 'HISTORY_EN', open(quiterssSourcePath + '\\HISTORY_EN', 'rb')) ftps.storbinary('STOR ' + 'HISTORY_RU', open(quiterssSourcePath + '\\HISTORY_RU', 'rb')) prepareFileList7z = [] for file in prepareFileList: prepareFileList7z.append(file + '.7z') for fileName in prepareFileList7z: ftps.storbinary('STOR ' + 'windows' + fileName.replace('\\','/'), open(quiterssFileRepoPath + '\\windows' + fileName, 'rb')) ftps.quit()
def check_ftps(hostname, temp_name, username, password, verbose): ftps_services_failed = [] if verbose: print("-" * 60) if verbose: print(temp_name) ftps = FTP_TLS(hostname) ftps.login(username, password) ftps.prot_p() #ftps.retrlines('LIST') if verbose: ftps.set_debuglevel(2) # Upload the file if verbose: print("FTPS: Uploading the file.") try: ftps.storbinary('STOR {0}'.format('ftps.txt'), open(temp_name, 'rb')) except: if verbose: print("FTPS: Uploading the file failed.") ftps_services_failed.append('ftps_upload') else: if verbose: print("FTPS: Uploaded file successfully.") pass # Download the file if verbose: print("FTPS: Downloading the file.") try: myfile = open('/tmp/ftps.txt', 'wb') ftps.retrbinary('RETR {0}'.format('ftps.txt'), myfile.write) except: if verbose: print("FTPS: Downloading the uploaded file failed.") ftps_services_failed.append('ftps_download') else: if verbose: print("FTPS: Downloaded the uploaded file successfully.") # Delete the file from remote system try: ftps.delete('ftps.txt') except: if verbose: print("FTPS: Deleting uploaded file failed.") ftps_services_failed.append('ftps_delete') else: if verbose: print("FTPS: Deleted the uploaded file successfully.") pass # Close the ftps connection. ftps.close() # Detel the file which is downloaded delete_temp_file('/tmp/ftps.txt', verbose) return ftps_services_failed
def get_ftp_data(self, cr, uid, ids, context={}): for chain in self.browse(cr, uid, ids, context=context): config_obj = chain.ftp_config_id try: conn = FTP_TLS(host=config_obj.host, user=config_obj.username, passwd=config_obj.passwd) conn.prot_p() except: conn = FTP(host=config_obj.host, user=config_obj.username, passwd=config_obj.passwd) filenames = conn.nlst() for filename in filenames: input_file = StringIO() conn.retrbinary('RETR %s' % filename, lambda data: input_file.write(data)) input_string = input_file.getvalue() input_file.close() csv_reader = unicode_csv_reader( StringIO(input_string), delimiter=str(chain.separator), quoting=(not chain.delimiter and csv.QUOTE_NONE) or csv.QUOTE_MINIMAL, quotechar=chain.delimiter and str(chain.delimiter) or None, charset=chain.charset) self.import_to_db(cr, uid, ids, csv_reader=csv_reader, context=context) conn.delete(filename) conn.quit() return True
def connexionftp(adresseftp, nom, mdpasse, passif): """connexion au serveur ftp et ouverture de la session - adresseftp: adresse du serveur ftp - nom: nom de l'utilisateur enregistré ('anonymous' par défaut) - mdpasse: mot de passe de l'utilisateur ('anonymous@' par défaut) - passif: active ou désactive le mode passif (True par défaut) retourne la variable 'ftplib.FTP' après connexion et ouverture de session """ try: verbose('Attente connexion FTP .....') if modeSSL: ftp = FTP_TLS() ftp.connect(adresseftp, 21) ftp.login(nom, mdpasse) ftp.prot_p() ftp.set_pasv(passif) else: ftp = (ftplib.FTP(adresseftp, nom, mdpasse)) ftp.cwd(destination) verbose ('Destination : '+destination) verbose('Connexion FTP OK') etat = ftp.getwelcome() verbose("Etat : "+ etat) return ftp except: verbose('Connexion FTP impossible', True) suppressionDossierTemp(dossierTemporaireFTP) sys.exit()
def upload(self,path,type,name=None): try: fname = name if name else path.split('\\')[-1] ftps = FTP_TLS(self.host) ftps.login(self.user,self.password) ftps.prot_p() # force encoding to utf-8, this will let us to work with unicode file names ftps.encoding = "utf-8" ftps.cwd(self.user_dir) ftps.cwd(type) if type != 'files': # if type of requested file is screenshot or keylog # upload it to special folder on ftp today = date.today().strftime("%b-%d-%Y") if today not in ftps.nlst(): ftps.mkd(today) ftps.cwd(today) ftps.storbinary('STOR %s' % fname, open(path, 'rb')) return 'Upload Started!' if fname in ftps.nlst(): return 'File Already on FTP' ftps.storbinary('STOR %s' %fname, open(path,'rb')) ftps.close() return 'Upload Started!' except Exception as e: if e.args[0] == 0: return 'Uploaded to FTP!' return 'Upload Failed!'
def ftp_connection(): ftp_conn = None try: logger = get_logger() logger.info("Creating object FTP_TLS()") ftp_conn = FTP_TLS() logger.info("Trying to connect to FTP(%s, %d)", FTP_HOST_NAME, FTP_PORT_NUMBER) ftp_conn.connect(FTP_HOST_NAME, FTP_PORT_NUMBER) ftp_conn.auth() logger.info("auth() 0k") ftp_conn.prot_p() logger.info("prot_p() 0k") logger.info("Trying with FTP_USER_NAME = %s", FTP_USER_NAME) ftp_conn.login(user=FTP_USER_NAME, passwd=FTP_ACCESS_KEY) ftp_conn.set_debuglevel(2) logger.info('Connected') except Exception as e: logger.error('Not connected %s', e) return ftp_conn
def getfilelist(server, port, user, password, db): sqliteconnection = sqlite3.connect(db) sqlitecursor = sqliteconnection.cursor() sqlitecursor.execute('''CREATE TABLE IF NOT EXISTS files (date int, name text, CONSTRAINT 'id_UNIQUE' UNIQUE ('name'))''') sqliteconnection.commit() ftpsconnection = FTP_TLS() ftpsconnection.connect(server, port) ftpsconnection.auth() ftpsconnection.prot_p() ftpsconnection.login(user, password) ftpsconnection.prot_p() rootfiles = ftpsconnection.nlst() for i in range(0,5): episodes = ftpsconnection.nlst(rootfiles[i]) for episode in episodes: sqlitecursor.execute('''INSERT OR IGNORE INTO files VALUES ("%(date)d", "%(folder)s")''' % {'date': time.time(), 'folder': ("/" + rootfiles[i] + "/" + episode) } ) sqliteconnection.commit() sqliteconnection.close() ftpsconnection.quit() ftpsconnection.close()
def ftpDownload(filename, system): from ftplib import FTP_TLS import os ftps = FTP_TLS() ftps.connect('pwcrack.init6.me', '21') ftps.auth() ftps.login('DC214', 'passwordcrackingcontest') ftps.prot_p() ftps.set_pasv(True) local_filename = filename with open(local_filename, 'wb') as f: def callback(data): f.write(data) ftps.retrbinary('RETR %s' % filename, callback) f.close() file_extension = str(filename.split('.')[1]) if file_extension == '7z': status = decompressit(local_filename, system) if status: print "file %s hash been downloaded." % local_filename return True
def connect(self): #初始化 FTP 链接 if self.ftp_ssl: ftp = FTPS() else: ftp = FTP() print('-'*20+self.ftp_name+'-'*20) print('connect '+('ftps' if self.ftp_ssl else 'ftp')+'://'+self.ftp_host+':'+self.ftp_port) try: ftp.connect(self.ftp_host,self.ftp_port) except Exception as e: print (e) print ('connect ftp server failed') sys.exit() try: ftp.login(self.ftp_user,self.ftp_passwd) print ('login ok') except Exception as e:#可能服务器不支持ssl,或者用户名密码不正确 print (e) print ('Username or password are not correct') sys.exit() if self.ftp_ssl: try: ftp.prot_p() except Exception as e: print (e) print ('Make sure the SSL is on ;') print(ftp.getwelcome()) ftp.cwd(self.ftp_webroot) print('current path: '+ftp.pwd()) self.ftp=ftp
def Push( FtpServer, Username, Password, uploadlist = FilesToPut, port = 21, passive = False, StartTls = False ): print "Login to %s:%s using %s:%s (%s%s)"%(FtpServer, port, Username, 'xxx', 'passive' if passive else 'active', '/tls' if StartTls else '') if StartTls: ftp = FTP_TLS() else: ftp = FTP() #ftp.set_debuglevel(2) ftp.connect( FtpServer, port ) ftp.login( Username, Password ) # user anonymous, passwd anonymous@ ftp.set_pasv( passive ) if StartTls: ftp.prot_p() for f in uploadlist: print "uploading %s"%f fp = open( f, 'rb') ftp.storbinary('STOR %s'%os.path.basename(f), fp) # send the file ftp.quit()
def ftp_coon(self): try: self.ftp.close() except Exception as e: pass ftp = FTP() retry_time = 0 while retry_time < 3: try: ftp.connect(self.host, self.port) ftp.login(self.username, self.password) self.ftp = ftp break except Exception as e: self.ftp = None try: ftps = FTP_TLS(self.host) ftps.set_pasv(True) ftps.login(self.username, self.password) ftps.prot_p() self.ftp = ftps break except Exception as e: self.ftp = None finally: if self.ftp: return self.ftp retry_time += 1 time.sleep(5**retry_time)
class ExplicitTLS(Configuration): """ This class includes methods to allow establishing a secure Explicit FTP secure connection to the One Scotland Gazetteer FTP """ def __init__(self): Configuration.__init__(self) self.host = self.get_configuration_for('ftp', 'host') self.port = int(self.get_configuration_for('ftp', 'port')) self.username = self.get_configuration_for('ftp', 'username') self.password = self.get_configuration_for('ftp', 'password') def setup(self): # An FTP subclass which adds TLS support to FTP self.client = FTP_TLS(timeout=10) def connect(self): self.client.connect(host=self.host, port=self.port) def login(self): self.client.login(user=self.username, passwd=self.password) #Make our connection to the server secure (i.e. encrypted) self.client.prot_p() #This is a hack making 'ftplib' use the EPSV network protocol (i.e. an IPv6 connection) instead of the PASV #protocol (i.e. an IPv4). The reason for doing this is that there is a bug in FTP lib which returns the wrong #IP address after connection to the FTP if PASV is used. In contrast if the EPSV protocol is used the FTP IP is #returned correctly allowing further commands to the FTP connection. self.client.af = socket.AF_INET6 return self.client
def connect_ftps(): """ FTP over TLS NOte: do NOT enable 'Require TLS session resumption on data connection when using PROT P', or you will get exception 'ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:727)' or server side will show '450 TLS session of data connection has not resumed or the session does not match the control connection' :return: """ ftps = FTP_TLS() # ftps.set_debuglevel(2) ssl_version_stack = [ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv3] tls_version_stack = [ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLS] is_connected = False for ssl_version in ssl_version_stack + tls_version_stack: # also can use list.extend() or list slice instead of + ftps.ssl_version = ssl_version try: ftps.connect(host=server, port=port) is_connected = True break except socket.timeout: continue except socket.error as e: # such as '10061', '[Errno 10061] Connection refused.' print(str(e), socket.errorTab.get(int(str(e).strip()[7:-1]))) continue else: if not is_connected: raise RuntimeError("No SSL/TLS supported on ftp server, does server misconfigured?") ftps.login(user=username, passwd=password) ftps.prot_p() # 'Force PROT P to encrypt file transfers when using FTP TLS' return ftps
def download_all_in_one_path(targetdir,resultdir,check = True,num = 50): if(os.path.exists(resultdir) == False): os.makedirs(resultdir) ftp = FTP('129.164.179.23') ftp.login() ftp.prot_p() ftp.cwd(targetdir) files = ftp.nlst() target = 'https://heasarc.gsfc.nasa.gov/FTP' + targetdir c = None if(check): c = [] data1 = [] ftp.voidcmd('TYPE I') print('正在获取校验信息........') for i in files: #print(i) data = os.path.join(target,i) print(data) data1.append(data) if(check): c.append(ftp.size(i)) ftp.quit() if(check == False): print('忽略数据大小校验。') print('正在校验...............') down(data1,resultdir,check=c,threadnum = num) print('\n任务下载完成!!!')
def get_session_ftps(host, login=None, password=None, port=21, auth=False, protocol=True): """ Creates connection with FTPS server :param host: host of FTPS server :param login: user's name :param password: password for user :param port: port of FTPS server :param auth: if it is true sets up secure control connection by using TLS/SSL :param protocol: if it is true sets up secure data connection else sets up clear text data connection :return: FTPConnector :type host: str :type login: str :type password: str :type port: int :type auth: bool :type protocol: bool :rtype: FTPConnector """ try: ftp = FTP_TLS() ftp.connect(host, port) ftp.login(login, password) if protocol: ftp.prot_p() else: ftp.prot_c() if auth: ftp.auth() return FTPConnector(ftp) except error_perm, exp: raise FTPConnectorError( exp.message )
def storeFtplib(dataframe, filename="cameliaBalAGKevin.csv", compression = None, toPrint = False): """ function that connects to the remote FTP serveur and upload a pandas dataframe the upload file must be a pandasDataframe and will be written in a csv file. It can be uploaded as a bz2, gz encoded or not encoded at all if it is encoded, the right extension must be present in the name -- IN dataframe : the dataframe to upload (pandas.Dataframe) filename : the filename with its extension to be downloaded from the remote ftp server (string) compression : string that specifies the encoding of the file (string in [None,"gz","bz2"] default: None toPrint : boolean that settles if the function should print its progress and results (boolean) default: False -- OUT flag : boolean that settles if everything was successful (True: no problem, False: an error occured) """ startTime = time.time() if toPrint: print "" print "" print "===========================================" print "=== Connection to the remote FTP server ===" print "===========================================" print "" print "using ftplib" print "loading :",filename print "" ftp = FTP_TLS() # retrieving information about account on ftp server (user, password, host, port) = getAccount() if user==None: print "error : coudn't read the account information" return False # connecting and logging in try: ftp.connect(host,port) ftp.login(user,password) except: print "error : unable to connect to the ftp server" return False # establishing the security protocol ftp.prot_p() if toPrint: print "connected to the FTP server" try: lines = dataframe.to_csv(path_or_buff = None,sep="\t",columns=dataframe.columns) except: print "error : impossible to convert the dataframe into csv lines" return False sio = StringIO.StringIO(lines) ftp.storlines(cmd="STOR "+filename, fp=sio) # try: # ftp.storlines(cmd="STOR "+filename, file=lines) # except: # print "error : impossible to upload the file" # return False interval = time.time() - startTime if toPrint: print 'Dataframe uploaded :', interval, 'sec' return True
def start_ftpes_connection(timeout=30): ftps = FTP_TLS(timeout=timeout) ftps.connect(SETTINGS['exporters.plan2learn.host'], 21) ftps.auth() ftps.prot_p() ftps.login(SETTINGS['exporters.plan2learn.user'], SETTINGS['exporters.plan2learn.password']) return ftps
def ftpconnect(host, port, username, password): from ftplib import FTP_TLS ftps = FTP_TLS(timeout=100) ftps.connect("135.32.1.36", 2121) ftps.auth() ftps.prot_p() ftps.login('ftpuser', 'ftpoptr') return ftp
def send_to_ftp(filepath): ftps = FTP_TLS('ftpes.learn.inf.puc-rio.br') # Define Ftp server ftps.login('learn', 'LRepoAdm18!!') # Login to ftp server ftps.prot_p() # Enable data encryption ftps.retrlines('LIST') # List Directory ftps.cwd('/projeto/01_tarefas_de_geofisica/QualiSismo/CV_results') # Change Directory ftps.storlines("STOR " + filepath, open(filepath,'rb'))
class BackupFTPS(backup_ftp.BackupFTP): def __init__(self, conf): super().__init__(conf) self.ftp = FTP_TLS(timeout=10) self.ftp.encoding = 'utf-8' def _init_connection(self): super()._init_connection() self.ftp.prot_p()
def connect(self): ftps = FTP_TLS(self.host) ftps.login(self.username, self.passwd) # switch to secure data connection.. # IMPORTANT! # Otherwise, only the user and password is encrypted and not all the file data. ftps.prot_p() return ftps
def open_may_tls(*args, **keyargs): """ connect to FTP server with TLS then return new May instance. if you want to close connection, call the instance's method of `close`. """ ftp = FTP_TLS(*args, **keyargs) ftp.prot_p() return May(ftp, firstdir=Path("/"))
def connect(user, password, ip_address='pensieve.usc.edu', port=41590): ftp = FTP_TLS() ftp.ssl_version = ssl.PROTOCOL_SSLv23 ftp.debugging = 2 ftp.connect(ip_address, port) ftp.login(user, password) ftp.prot_p() ftp.retrlines('LIST home') return(ftp)
def connect(self, url, secure): if secure: ftp = FTP_TLS(url) ftp.login() ftp.prot_p() else: ftp = FTP(url) ftp.login() return ftp
def connect(self): self.sock = socket.create_connection((self.ftp_server, self.port), self.timeout) self.af = self.sock.family self.sock = ssl.wrap_socket(self.sock, self.keyfile, self.certfile,ssl_version=ssl.PROTOCOL_TLSv1) self.file = self.sock.makefile('rb') self.welcome = self.getresp() self.login(self.username,self.password) FTP_TLS.prot_p(self) return self.welcome
def connect(): global ftp; if remoteTLS: context = ssl.create_default_context(); ftp = FTP_TLS(remoteHost, remoteUser, remotePassword, acct="", keyfile=None, certfile=None, context=context, timeout=20); ftp.prot_p(); else: ftp = FTP(remoteHost, remoteUser, remotePassword, 20); print(ftp.getwelcome());
def ftp_to_ml(localfile, remote_file): (user, acct, passwd) = netrc.netrc().authenticators('ftps.b2b.ml.com') ftp = FTP_TLS('ftps.b2b.ml.com') remote_dir = 'incoming/gmigefs2' ftp.login(user, passwd, acct) ftp.prot_p() ftp.cwd(remote_dir) local_fd = open(localfile, 'r') ftp.storlines('STOR ' + remote_file, local_fd) local_fd.close()
def download_file_tls(host, user, password, local_path, remote_path, timeout=None): ftp = FTP_TLS(host=host, user=user, passwd=password, timeout=timeout) ftp.prot_p() ftp.retrbinary('RETR {}'.format(remote_path), open(local_path, 'wb').write) ftp.quit()
def send(self): ftp = FTP(self.ftp_destination) ftp.set_debuglevel(2) ftp.ssl_version = ssl.PROTOCOL_TLSv1_2 ftp.login() ftp.cwd("upload") ftp.prot_p() self.zip.close() self.zip_contents.seek(0) ftp.storbinary("STOR {}".format(self.zip_contents.name), self.zip_contents)
def start(self): # create connection for ftpserver, users in self.db.iteritems(): for s_user in users: self.log.log("Connecting to %s: user: %s pass: %s" % (ftpserver, s_user.user, s_user.passwd)) ftp = FTP_TLS(ftpserver) # connect to host, default port ftp.login(user=s_user.user, passwd=s_user.passwd) ftp.prot_p() # switch to secure data connection ftp.retrlines('LIST', self.parseFiles) self.log.log("Done! now quit...") ftp.quit()
def fetch_data_via_ftp(ftp_config, local_directory): """ Get benchmarking data from a remote ftp server. :type ftp_config: config.FTPConfigurationRepresentation :type local_directory: str """ if ftp_config.enabled: # Create local directory tree if it does not exist create_directory_tree(local_directory) # Login to FTP server if ftp_config.use_tls: ftp = FTP_TLS(ftp_config.server) ftp.login(ftp_config.username, ftp_config.password) ftp.prot_p() # Request secure data connection for file retrieval else: ftp = FTP(ftp_config.server) ftp.login(ftp_config.username, ftp_config.password) if not ftp_config.files: # Auto-download all files in directory fetch_data_via_ftp_recursive(ftp=ftp, local_directory=local_directory, remote_directory=ftp_config.directory) else: ftp.cwd(ftp_config.directory) file_counter = 1 file_list_total = len(ftp_config.files) for remote_filename in ftp_config.files: local_filename = remote_filename filepath = os.path.join(local_directory, local_filename) if not os.path.exists(filepath): with open(filepath, "wb") as local_file: try: ftp.retrbinary('RETR %s' % remote_filename, local_file.write) print("[Setup][FTP] ({}/{}) File downloaded: {}". format(file_counter, file_list_total, filepath)) except error_perm: # Error downloading file. Display error message and delete local file print( "[Setup][FTP] ({}/{}) Error downloading file. Skipping: {}" .format(file_counter, file_list_total, filepath)) local_file.close() os.remove(filepath) else: print( "[Setup][FTP] ({}/{}) File already exists. Skipping: {}" .format(file_counter, file_list_total, filepath)) file_counter = file_counter + 1 # Close FTP connection ftp.close()
def file_ftps(host,un,pw,ftp_filename,source_filename): try: ftps = FTP_TLS(host) ftps.auth_tls() ftps.login(un,pw) ftps.prot_p() ftps.storbinary("STOR " + ftp_filename, file(source_filename, "rb")) ftps.quit() except ftplib.all_errors, error: print 'Error:', str(error) logging.info(mydate+ ' - '+'FTPS Error Encountered: '+ str(error)) sys.exit(0)
def ftpUpload(filename): from ftplib import FTP_TLS import os ftps = FTP_TLS() ftps.connect('pwcrack.init6.me', '21') ftps.auth() ftps.login('DC214', 'passwordcrackingcontest') ftps.prot_p() ftps.set_pasv(True) local_file = open(filename, 'rb') ftps.storbinary('STOR '+filename, local_file)
def get_latest_file(): with open('filenames.json') as json_file: files = json.load(json_file) flag = 0 ftp = FTP_TLS(host='olftp.adesa.com', user='******', passwd='aU)kj7Qn8') ftp.prot_p() #ftp.retrlines('LIST') ftp.cwd('outbound/') file_list = [] ftp.retrlines('MLSD', file_list.append) #ftp.dir(file_list.append) max_value = 0 full_max = 0 filename = '' full_file = '' for i in file_list: col = i.split(';') col_max = int(re.search(r'\d+', col[0]).group()) if (col_max > max_value) & ('.txt' in col[-1]): max_value = col_max filename = col[-1].replace(' ', '') if (col_max > full_max) & ('.txt' in col[-1]) & ('FULL' in col[-1]): full_max = col_max full_file = col[-1].replace(' ', '') if (filename != files['inc_file']): localfile = open(filename, 'wb') ftp.retrbinary('RETR ' + filename, localfile.write, 1024) localfile.close() print("Inc file data tranfer complete") flag = 1 else: print("Inc already there") if (full_file != files['full_file']): localfile = open(full_file, 'wb') ftp.retrbinary('RETR ' + full_file, localfile.write, 1024) localfile.close() print("Full file data tranfer complete") flag = 1 else: print("Full already there") if flag == 1: new_names = {'full_file': full_file, 'inc_file': filename} with open('filenames.json', 'w') as outfile: json.dump(new_names, outfile) ftp.quit() return filename, full_file, flag
def upload_file_tls(host, user, password, local_path, remote_path, timeout=None): ftp = FTP_TLS(host=host, user=user, passwd=password, timeout=timeout) ftp.prot_p() local_file = open(local_path, 'rb') ftp.storbinary('STOR {}'.format(remote_path), local_file) local_file.close() ftp.quit()
def Push( FtpServer, Username, Password, uploadlist = FilesToPut, port = 21, passive = False, StartTls = False, Sftp = False ): print "Login to %s:%s using %s:%s (%s%s)"%(FtpServer, port, Username, 'xxx', 'passive' if passive else 'active', '/tls' if StartTls else '') if Sftp: paramiko.util.log_to_file('/tmp/paramiko.log') transport = paramiko.Transport((FtpServer,int(port))) transport.connect(username = Username, password = Password) sftp = paramiko.SFTPClient.from_transport(transport) print "Uploading file" filepath = '../php/basedata.php' localpath = 'basedata.php' sftp.put(filepath, localpath) sftp.close() transport.close() else: if StartTls: ftp = FTP_TLS() else: ftp = FTP() ftp.connect( FtpServer, port ) ftp.login( Username, Password) ftp.set_pasv( passive ) if StartTls: ftp.prot_p() for f in uploadlist: print "uploading %s"%f fp = open( f, 'rb') ftp.storbinary('STOR %s'%os.path.basename(f), fp) # send the file ftp.quit() if __name__ == "__main__": if len(sys.argv) < 5: print >> sys.stderr, "usage %s <server> <port> <username> <password>"%sys.argv[0] exit( 1 ) FtpServer = sys.argv[1] Port = sys.argv[2] Username = sys.argv[3] Passwd = sys.argv[4] Push( FtpServer, Username, Passwd, port = Port ) print >> sys.stderr, "Done"
class FTPS: def __init__(self): self.ftps = FTP_TLS( ) def connect(self): self.ftps.connect('192.168.0.102', 2121) print(self.ftps.getwelcome()) def login(self): self.ftps.login('anderson', 'nosredna89') self.ftps.prot_p() #para fazer a conexação de dados segura def close(self): self.ftps.close()
def ftpDownload(filename): from ftplib import FTP_TLS import os ftps = FTP_TLS() ftps.connect('pwcrack.init6.me', '21') ftps.auth() ftps.login('DC214', 'passwordcrackingcontest') ftps.prot_p() ftps.set_pasv(True) local_filename = filename with open(local_filename, 'wb') as f: def callback(data): f.write(data) ftps.retrbinary('RETR %s' % filename, callback)
def ftp_files(domain, remote_paths, local_paths, direction, secure=True): ftp = FTP_TLS(domain) if secure else FTP(domain) ftp.login(prompt_usr(), prompt_pw()) if secure: ftp.prot_p() for remote_path, local_path in zip(remote_paths, local_paths): if direction.lower() == 'up': ftp.storbinary('STOR ' + remote_path, open(local_path, 'rb')) elif direction.lower() == 'down': ftp.retrbinary('RETR ' + remote_path, open(local_path, 'wb').write) else: raise Exception('Invalid direction: ' + direction) ftp.quit()
def Push( FtpServer, Username, Password, uploadlist = FilesToPut, port = 21): print >> sys.stderr, "Login to %s:%s using %s:%s"%(FtpServer, port, Username, 'xxx') ftp = FTP() ftps = FTP_TLS() ftps.connect(FtpServer,Port) ftps.auth() ftps.login(Username, Password) # login anonymously before securing control channel ftps.prot_p() ftp.set_pasv(False) for f in uploadlist: #print "uploading %s"%f fp = open( f, 'rb') os.path.basename(f) print f ftp.storbinary("STOR,%sx " %(os.path.basename(f),fp)) #ftp.storbinary('STOR, %s',fp %(basename(f)) ) # send the file ftp.quit()
def ftp_backup(dmpdir,dbname): try: # ftp =FTP_TLS() ftp.connect(ftphost,ftpport) ftp.login(ftpuser,ftppass) ftp.prot_p() print "Welcome:",ftp.getwelcome() print ftp.retrlines('LIST') # ftp =FTP() # ftp.connect(ftphost,ftpport) # ftp.login(ftpuser,ftppass) # print "Welcome:",ftp.getwelcome() # print ftp.retrlines('LIST') except Exception,e: print e return
def getfiles(server, port, user, password, db): sqliteconnection = sqlite3.connect(db) sqlitecursor = sqliteconnection.cursor() sqlitecursor.execute('''CREATE TABLE IF NOT EXISTS latest (date int, CONSTRAINT 'id_UNIQUE' UNIQUE ('date'))''') sqliteconnection.commit() sqlitecursor.execute('''SELECT date FROM files WHERE date = (SELECT MAX(date) FROM files) LIMIT 1''') latestfile = sqlitecursor.fetchone() sqlitecursor.execute('''SELECT date FROM latest WHERE date = (SELECT MAX(date) FROM latest) LIMIT 1''') latestfetch = sqlitecursor.fetchone() if latestfetch is None: latestfetch = 0 if latestfetch < latestfile: ftpsconnection = FTP_TLS() ftpsconnection.connect(server, port) ftpsconnection.auth() ftpsconnection.prot_p() ftpsconnection.login(user, password) ftpsconnection.prot_p() sqlitecursor.execute('''SELECT name FROM files WHERE date > %d''' % latestfetch) filestofetch = sqlitecursor.fetchall() for currfile in filestofetch: ftpsconnection.cwd(currfile[0]) filenames = ftpsconnection.nlst() for filename in filenames: print 'Now saving /mnt/folder' + currfile[0] + '/' + filename localfile = open('/mnt/folder' + currfile + '/' + filename, 'wb') ftpsconnection.retrbinary('RETR ' + filename, localfile.write) localfile.close() sqliteconnection.execute('''INSERT OR IGNORE INTO latest VALUES (%d)''' % time.time()) sqliteconnection.commit() sqliteconnection.close() ftpsconnection.quit() ftpsconnection.close()
def ftpUpload(filename, system): from ftplib import FTP_TLS import os if os.path.isfile(filename): zipFilename = compressit(filename, system) ftps = FTP_TLS() ftps.connect('pwcrack.init6.me', '21') ftps.auth() ftps.login('DC214', 'passwordcrackingcontest') ftps.prot_p() ftps.set_pasv(True) local_file = open(zipFilename, 'rb') ftps.storbinary('STOR '+zipFilename, local_file) print "file %s has been uploaded." % zipFilename return True
def connect(velkost_ftp,port): ftp=FTP_TLS(server,meno2,ps,port) ftp.prot_p() ftp.cwd(my_list[2]) print "Posielam subor. Cakajte prosim." obsah=open(file_to_send, 'rb') obsah.close() ftp.storbinary('STOR %s' % file_to_send, open(file_to_send, 'rb')) obsah.close() print "Subor odoslany [OK]" print "Obsah adresara na serveri:" ftp.retrlines("LIST") size_ftp=ftp.nlst() pocet=len(size_ftp) velkost_ftp_subor=size_ftp[pocet-1] #berie posledne pridany subor zo zoznamu ftp.sendcmd("TYPE i") velkost_ftp=ftp.size(velkost_ftp_subor) ftp.close() return velkost_ftp
def startProtocol(port, user, passwd, ip, regex, target_path, args): global num_files, size, mirror, verbose, timeout num_files = 0 size = 0 mirror = args.search verbose = args.verbose timeout = args.timeout try: print(70*'#') print('Trying FTPS server: %s:%s' %(ip, port)) host = FTP_TLS(str(ip), timeout = timeout) host.login(user, passwd) host.prot_p() print('Connected... Downloading files...\n') files = downloadFTPFiles(target_path, host, regex) print('%i files (%s) downloaded... Closing connection...' % (files, convert_bytes(size))) host.close() except ftplib.all_errors as err: print (err)
def get_ftp_data(self, cr, uid, ids, context={}): for chain in self.browse(cr, uid, ids, context=context): config_obj = chain.ftp_config_id try: conn = FTP_TLS(host=config_obj.host, user=config_obj.username, passwd=config_obj.passwd) conn.prot_p() except: conn = FTP(host=config_obj.host, user=config_obj.username, passwd=config_obj.passwd) filenames = conn.nlst() for filename in filenames: input_file = StringIO() conn.retrbinary('RETR %s' % filename, lambda data: input_file.write(data)) input_string = input_file.getvalue() input_file.close() csv_reader = unicode_csv_reader(StringIO(input_string), delimiter=str(chain.separator), quoting=(not chain.delimiter and csv.QUOTE_NONE) or csv.QUOTE_MINIMAL, quotechar=chain.delimiter and str(chain.delimiter) or None, charset=chain.charset) self.import_to_db(cr, uid, ids, csv_reader=csv_reader, context=context) conn.delete(filename) conn.quit() return True
def make_msg(self, args): # make connection conn = None if args.ssl or args.starttls: conn = FTP_TLS() conn.ssl_version = PROTOCOL_TLSv1 else: conn = FTP() if args.verbose: conn.set_debuglevel(1) conn.connect(args.host, args.port, args.timeout) if args.starttls: conn.auth() conn.prot_p() conn.login(args.user, args.password) self.conn = conn return args.message
def ftpDownload(filename, system): from ftplib import FTP_TLS import os ftps = FTP_TLS() ftps.connect('pwcrack.init6.me', '21') ftps.auth() ftps.login('DC214', 'passwordcrackingcontest') ftps.prot_p() ftps.set_pasv(True) local_filename = filename with open(local_filename, 'wb') as f: def callback(data): print "Downloading %s ..." % filename f.write(data) ftps.retrbinary('RETR %s' % filename, callback) f.close() file_extension = str(filename.rsplit('.')[2]) if file_extension == '7z': status = decompressit(local_filename, system) if status: print "file %s has been downloaded." % local_filename
class Sftpimpl(Ftpimpl): def __init__(self): self.log = ProcessLogger() def connectFTP(self,host='',port='',uname='',upass='',ssh_host_key='',acv_pcv=''): if host=='' or port=='' or uname=='' or upass=='': raise FTP_ERROR('Oops: Blank config parameters, Please dont leave any config parameter blank') self.host = host log_id = self.log.processlog(logid=0,f1=False,f2=False,hostnm=host,up_dw='login',typ='new',status='Pending',result='trying to connect...') hostStr = '%s:%s' %(host,port) usrHostStr = '%s@%s' %(uname,hostStr) try: self.ftp = FTP_TLS(hostStr) if acv_pcv == 'Active': self.ftp.set_pasv(False) elif acv_pcv == 'Passive': self.ftp.set_pasv(True) self.ftp.login(uname,upass) self.ftp.prot_p() return_msg = self.ftp.getwelcome() self.log.processlog(logid=log_id,f1=False,f2=False,hostnm=host,up_dw='login',typ='edit',status='Done',result=return_msg) except Exception as e: self.log.processlog(logid=log_id,f1=False,f2=False,hostnm=host,up_dw='login',typ='edit',status='Failed',result=str(e)) raise FTP_ERROR('Connection error: Reasons \n\n(Internet connection)\n(Remote server down)\n(Config settings) ')