def get_ftp_list(): ftp = FTP_TLS() ftp.connect(host, port) ftp.set_debuglevel(2) ftp.login("account_id", "account_password") files, dir = get_list_ftp(ftp, "/") return files, dir
def upload_to_ftp(root_dir, upload_path): ftp = FTP_TLS() ftp.connect(host, port) ftp.set_debuglevel(2) ftp.login("account_id", "account_password") files, directories = get_list_local(upload_path) directory_name = os.path.dirname(upload_path) directory_name = os.path.basename(directory_name) root_dir = root_dir + directory_name + "/" directories.insert(0, "") for directory in directories: path = directory.replace(upload_path, "").replace("\\", "/") path = root_dir + path # for fName in ftp.nlst(): # print(fName) # if folderName in ftp.nlst(): # continue try: ftp.mkd(path) except: continue print("Make directory is " + path) for file in files: path = file.replace(upload_path, "").replace("\\", "/") with open(file, "rb") as localfile: path = root_dir + path ftp.storbinary('STOR ' + path, localfile) print("Upload file is " + path) print("done upload")
def find_versions_above( mirror: Dict[str, str], target_version: Version ) -> Generator[Mapping[str, Union[str, Version]], None, None]: print("Using mirror:", mirror['host']) tls = get_use_tls() if tls: ftp = FTP_TLS(mirror['host']) else: ftp = FTP(mirror['host']) ftp.login(user=mirror.get('user', None), passwd=mirror.get('passwd', ''), acct=mirror.get('acct', '')) ftp.cwd(mirror['path']) for filename, attrs in sorted(ftp.mlsd(), key=lambda tup: tup[0]): if not attrs["type"] == "file": continue if not filename.endswith(".tar.gz"): continue pkgver = get_package_version(filename) if not pkgver: continue if pkgver > target_version: fileinfo = mirror.copy() fileinfo["filename"] = filename fileinfo["version"] = pkgver fileinfo["tls"] = tls yield fileinfo
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
class ftpAccess(): def __init__(self, url): self.ftp = FTP_TLS(url) self.ftp.login( "anonymous", "anonymous", secure=False) # login anonymously before securing control channel def GetFtpFile(self, InPath, filename, downloaddir): outfile = downloaddir + filename ensure_dir(downloaddir) self.ftp.cwd(InPath) self.ftp.retrbinary("RETR " + filename, open(outfile, 'wb').write) def GetFileListFtp(self, pathname): self.ftp.cwd(pathname) ret = list() out = self.ftp.retrlines('LIST', addline) # list directory content securely # search json files temp = entry.split(' ') for filename in filenamelist: try: a = self.ftp.size(filename) print("{} - {}".format(filename, a)) ret.append([filename, a]) except: print("{} - xxx".format(filename)) pass return ret
def downloadXmlTranscripts(self): ftp = FTP_TLS() ftp.debugging = 2 ftp.connect(self.ftp_server,990) print ftp ftp.login(self.ftp_user,self.ftp_pass) print(ftp)
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 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)
def connect(): ftp = FTP_TLS() ftp.ssl_version = ssl.PROTOCOL_TLSv1_2 ftp.debugging = 2 ftp.connect("localhost", 2121) ftp.login("developer", "password") return ftp
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 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 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 _get_ftp(self): try: username, password = self._get_auth() if self._tls: ftp = FTP_TLS(self._uri, timeout=CONNECT_TIMEOUT) else: ftp = FTP(self._uri, timeout=CONNECT_TIMEOUT) ftp.login(username, password) if self._tls: ftp.prot_d() if not catch(ftp.cwd, self._path): if self._path.startswith("/"): ftp.cwd("/") components = self._path.split("/") for component in components: if not catch(ftp.cwd, component): ftp.mkd(component) ftp.cwd(component) return ftp except Exception: log.exception() log.warning( "[FTP] failed to establish server connection, disabled") self._disabled = True return None
def send_to_ftp(self, start=None, stop=None): now = self.datetime_localized(fields.Datetime.now(self)) start = start or now.replace(hour=0, minute=0, second=0) stop = stop or start + timedelta(days=1) host = self.env.user.company_id.ftp_host port = self.env.user.company_id.ftp_port user = self.env.user.company_id.ftp_user passwd = self.env.user.company_id.ftp_passwd ftp_tls = False ftp = FTP_TLS() if ftp_tls else FTP() try: ftp.connect(host, port) ftp.login(user, passwd) except: _logger.error('Unable to reach FTP server') else: stocklocations = self.get_stocklocations_file() mtsskus = self.get_mtsskus_file() transactions = self.get_transactions_file(str(start), str(stop)) status = self.get_status_file() ftp.storbinary('STOR ' + stocklocations[0], BytesIO(stocklocations[1])) ftp.storbinary('STOR ' + mtsskus[0], BytesIO(mtsskus[1])) ftp.storbinary('STOR ' + transactions[0], BytesIO(transactions[1])) ftp.storbinary('STOR ' + status[0], BytesIO(status[1])) finally: ftp.close()
def connect_ftp(): #Connect to the server ftp = FTP_TLS() ftp.connect(SERVER, PORT) ftp.login(USER, PASS) return ftp
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 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 _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 __connectTls(self, host, user, password): ftp_tls = FTP_TLS(host) ftp_tls.login(user, password) ftp_tls.prot_p() return ftp_tls
def connect(box_path): ftp = FTP_TLS('ftp.box.com') # ftp.debugging = 2 ftp.login('*****@*****.**', 'cJ19870915@#') ftp.cwd(box_path[0]) print("Connected to Box FTP") return ftp
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 download(filepath): try: ftp = FTP_TLS(theserver) ftp.login(userper,thepast) #ftp.retrlines("LIST") #Get name of the file from the filepath #If path is C:\Users\Solotov\Downloads\Tash.txt then retrieve Tasha.txt # from the path name filename = os.path.basename(filepath) path = filepath.replace(filename,'') #Keep original filename filenametokeep = filename local_filename = os.path.join(r''+path+filename) downloadfile = filenametokeep local_path = local_filename if 'cc.xml' not in filenametokeep: remote_path = thename+downloadfile else: remote_path = downloadfile lf = open(local_filename, "wb") ftp.retrbinary("RETR " + remote_path, lf.write, 8*1024) lf.close() ftp.close() f = open('C:/hog/Downloads','w+') f.write('Download of ' + filename + ' Successfull') f.close() sftp('Downloads','C:/hog/Downloads') except Exception as e: ohno = e
def connect(self, context): ui_props = context.scene.editAsset ftp = FTP_TLS() ftp.connect('ftp.luxcorerender.org', 21) ftp.login(ui_props.username, ui_props.password) return ftp
def send_chunks(self): if self.final_chunks is None: return ERR if self.tls_flag: if self.auth_flag: ftp_obj = FTP_TLS(host=self.server, user=self.creds[0], passwd=self.creds[1]) else: ftp_obj = FTP_TLS(host=self.server) else: if self.auth_flag: ftp_obj = FTP(host=self.server, user=self.creds[0], passwd=self.creds[1]) else: ftp_obj = FTP(host=self.server) try: ftp_obj.login() sys.stdout.write("\t[+]\tConnected to server %s.\n" % self.server) except: sys.stderr.write("\t[-]\tCould not login to the server.\n") return ERR for chunk in self.final_chunks: ftp_obj.mkd(chunk) time.sleep(SLEEP) ftp_obj.quit() sys.stdout.write("\t[+]\tWrote %s(+1) folders.\n" % (len(self.final_chunks) - 1)) return OKAY
def conexionFTP(): print "conexionFTP" global reIntento, rutaFtp,tiempoErr,hostFtp,passFtp,userFtp,portFtp,infoERR #request = urllib.request.Request('http://'+hostFtp) #response = urllib.request.urlopen(request) estatusCftp=False if response.getcode() == 200: try: ftp = FTP_TLS() ftp.connect(hostFtp,portFtp) ftp.login(userFtp, passFtp) ftp.cwd(rutaFtp) estatusCftp = True except Exception as e: if infoERR == True:logger.warning(formErro) logger.warning('Error al conectar al servidor ftp '+str(e)) else: logger.warning('Error al conectar al servidor Error de internet '+str(e)) for i in range(reIntento): if conexionFTP()['ftp']:main() else:logger.warning("reconecion internet intento "+str(i));slp(tiempoErr)# return {'ftp':ftp,'estatusCftp':estatusCftp}
def main(): global ftp_client scan_arguments() ftp_client = FTP(host) try: ftp_client.login(username, password) except ftplib.all_errors as e: print "ERROR: cannot login with username '{0}' and relative password.\nMessage returned from server:".format(username) print e return try: ftp_client.cwd(remote_dir) except ftplib.all_errors as e: print "ERROR: emote directory '{0}' not existing.\nMessage returned from server:".format(remote_dir) print e return else: files = ftp_client.nlst() print_directory_content(files) setup_folder() download_files(remote_dir, files) if compress: create_zip(local_dir) try: ftp_client.close() print "!!!!! OPERATION COMPLETED SUCCESSFULLY !!!!!" except ftplib.all_errors as e: print "ERROR: cannot close the connection properly.\nMessage from server:" print e
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任务下载完成!!!')
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 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 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 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 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 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 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 send_chunks(self): if self.final_chunks is None: return ERR if self.tls_flag: if self.auth_flag: ftp_obj = FTP_TLS(host=self.server, user=self.creds[0], passwd=self.creds[1]) else: ftp_obj = FTP_TLS(host=self.server) else: if self.auth_flag: ftp_obj = FTP(host=self.server, user=self.creds[0], passwd=self.creds[1]) else: ftp_obj = FTP(host=self.server) try: ftp_obj.login() sys.stdout.write("\t[+]\tConnected to server %s.\n" % self.server) except: sys.stderr.write("\t[-]\tCould not login to the server.\n") return ERR for chunk in self.final_chunks: ftp_obj.mkd(chunk) time.sleep(SLEEP) ftp_obj.quit() sys.stdout.write("\t[+]\tWrote %s(+1) folders.\n" % (len(self.final_chunks)-1)) return OKAY
def _connect(server: str): global _connection_cache if server not in _connection_cache: if server == 'in_server': logger.debug(f'Connection to "in_server" requested') conn_specs = config().ftp.in_server elif server == 'out_server': logger.debug(f'Connection to "out_server" requested') conn_specs = config().ftp.out_server else: raise ValueError(f'Unknown server "{server}"') logger.debug(f'conn specs = {conn_specs}') if conn_specs.secure: ftp = FTP_TLS(conn_specs.host) ftp.auth() else: ftp = FTP(conn_specs.host) # ftp.connect(conn_specs.host, conn_specs.port) logger.debug(f'Connected to "{server}". Logging in...') ftp.login(conn_specs.username, conn_specs.password) logger.debug(f'Established connection to "{server}". Saving to cache') _connection_cache[server] = ftp return ftp else: ftp = _connection_cache[server] ftp.cwd('/') logger.debug(f'Recycling connection to "{server}"') return 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 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 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 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 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 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 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 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 download(downloaded, user, passwd, all_files=False, filename=None): # Connect to the MAPS ftp server over FTPS ftps = FTP_TLS('ftps.tsi.telecom-paristech.fr') print 'Connected to MAPS FTP over TLS.' try: ftps.login(user=user, passwd=passwd) ftps.cwd('maps') except error_perm: print "Incorrect username/password" ; quit ftps.retrlines('LIST *.zip', get_file_list) if filename is not None: if not in_downloads(files, filename): print 'File not found' ; return print 'Downloading', filename res = ftps.retrbinary('RETR '+filename, open('./downloads/'+filename, 'wb').write) ftps.close() return [(filename, 0)] if len(files) == len(downloaded): print "All MAPS files downloaded. Continuing." return if all_files: for f, s in files: if not in_downloads(downloaded, f): print "Downloading", f, "of size", s, "bytes" res = ftps.retrbinary('RETR '+f, open('./downloads/'+f, 'wb').write) elif filename is None: f, s = random.choice(files) while in_downloads(downloaded, f): f, s = random.choice(files) print "Downloading", f, "of size", s, "bytes" res = ftps.retrbinary('RETR '+f, open('./downloads/'+f, 'wb').write) ftps.close() if all_files: return files return [(f, s)]
def doLogin(self, user, passwd, proxy=None): #Installing SOCK proxy if proxy: import socket try: import socks except ImportError: logging.critical("sock module not installed.") return ip, port = proxy.split(":") socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, ip, int(port)) socket.socket = socks.socksocket try: ftps = FTP_TLS(self.address, timeout=20) #Connecting except socket.error as errno: logging.debug(errno) return "PROXY FAIL" try: ftps.login(user, passwd) #Try to login except Exception: logging.debug("{0}:{1} failed.".format(user, passwd)) return None logging.info("Valid user found. {0}:{1}".format(user, passwd)) if self.options.get("output_file"): try: output_file = open(self.options.get("output_file"), "a") output_file.write("{0}:{1}".format(user, passwd)) output_file.close() except IOError as errno: logging.warning(errno) finally: return user, passwd
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
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) ')
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
from ftplib import FTP_TLS ftps = FTP_TLS('127.0.0.1:8021','user','12345',keyfile='privateKey.key') ftps.login() # login anonymously before securing control channel ftps.prot_p() # switch to secure data connection print ftps.retrlines('LIST')