def shares(targetPath, conn, regex): global num_files, size shareList = conn.listShares() num_files = 0 size = 0 if verbose == 0: print("Downloading files...") for r in shareList: if targetPath and not os.path.isdir(os.path.join(targetPath, r.name)): try: makedir(os.path.join(targetPath, r.name)) except os.error as err: if verbose > 0: print(err) return downloadSMBFiles("\\", os.path.join(targetPath,r.name), conn, regex, r.name) print('%i files (%s) downloaded. Closing connection...' % (num_files, convert_bytes(size)))
def shares(targetPath, conn, regex): global num_files, size shareList = conn.listShares() num_files = 0 size = 0 if verbose == 0: print("Downloading files...") for r in shareList: if targetPath and not os.path.isdir(os.path.join(targetPath, r.name)): try: makedir(os.path.join(targetPath, r.name)) except os.error as err: if verbose > 0: print(err) return downloadSMBFiles("\\", os.path.join(targetPath, r.name), conn, regex, r.name) print('%i files (%s) downloaded. Closing connection...' % (num_files, convert_bytes(size)))
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 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 downloadFTPFiles(destination, host, regex): global num_files, size pwd = host.pwd() if destination and not os.path.isdir(destination): try: makedir(destination) except os.error as err: if verbose > 0: print(err) return subdirs = [] listing = [] host.retrlines('LIST', listing.append) for line in listing: prop = line.split(None, 8) if len(prop) < 6: continue filename = prop[-1].lstrip() #is symlink? if (prop[0][0]) is 'l': continue if filename is ('.' or '..'): continue #is directory? if (prop[0][0]) is 'd': subdirs.append(filename) continue fullname = os.path.join(destination, filename) tempname = os.path.join(destination, filename + '.tmp') try: os.unlink(tempname) except os.error: pass try: fp = open(tempname, 'wb') except IOError as err: if verbose > 0: print("Can't create %r: %s" % (tempname, err)) continue fullpath = '' if pwd is not '/': fullpath = '%s/%s' % (pwd, filename) else: fullpath = '/%s' % filename try: if not mirror: if verbose > 2: print('Checking file: %r' % fullpath) if regex.match(filename): if verbose > 2: print('***Found match...') filesize = host.size(filename) if verbose > 1: print('Downloading file: %s.....[%s]' % (fullpath, convert_bytes(filesize))) host.retrbinary('RETR ' + filename, fp.write, 8 * 1024) size += filesize num_files += 1 else: if verbose > 2: print('No match...') fp.close() os.remove(tempname) else: filesize = host.size(filename) if verbose > 1: print('Downloading file: %s.....[%s]' % (fullpath, convert_bytes(filesize))) host.retrbinary('RETR ' + filename, fp.write, 8 * 1024) size += filesize num_files += 1 except ftplib.error_perm as err: if verbose > 1: print('%s: %s' % (fullpath, err)) fp.close() os.remove(tempname) continue fp.close() try: os.unlink(fullname) except os.error: pass try: os.rename(tempname, fullname) except os.error: continue for subdir in subdirs: localsub = os.path.join(destination, subdir) pwd = host.pwd() try: if verbose > 2: print('Changing current directory to: %r' % (pwd + subdir)) host.cwd(subdir) except ftplib.error_perm as err: if verbose > 0: print('%s\n%s' % (subdir, err)) continue else: downloadFTPFiles(localsub, host, regex) host.cwd('..') if host.pwd() != pwd: break #Delete empty directories try: os.rmdir(localsub) except OSError: pass return num_files
def downloadSMBFiles(smbpath, projPath, conn, regex, share): global num_files, size cwd = "" if smbpath[:1] is "\\": cwd = (smbpath[1:]) #trim first \ cwd = os.path.join(projPath,cwd) if cwd and not os.path.isdir(cwd): try: makedir(cwd) except os.error as err: if verbose > 0: print(err) return subdirs = [] try: listing = conn.listPath(share, smbpath) for l in listing: filename = l.filename if l.isDirectory: if filename is ".": continue if filename == "..": continue #Using 'is' didn't work subdirs.append(filename) continue else: relativeRemotePath = os.path.join(smbpath, l.filename) relativeLocalPath = os.path.join(share, relativeRemotePath[1:]) tempname = relativeLocalPath+'.tmp' try: os.unlink(tempname) except os.error: pass try: fp = open(tempname, 'wb') except IOError as err: if verbose > 0: print ("Can't create %r: %s" % (tempname, err)) continue try: if not mirror: if verbose > 2: print('Checking file: %r' % relativeRemotePath) if regex.match(filename): print('***Found match...***') if verbose > 0: print("Downloading file: %s.....[%s]" % (relativeRemotePath, convert_bytes(l.file_size))) file_attributes, filesize = conn.retrieveFile(share, relativeRemotePath, fp) fp.close() size += filesize num_files+=1 else: if verbose > 2: print('Not matched...') fp.close() os.remove(tempname) else: if verbose > 1: print("Downloading file: %s.....[%s]" % (relativeRemotePath, convert_bytes(l.file_size))) try: file_attributes, filesize = conn.retrieveFile(share, relativeRemotePath, fp) size += filesize fp.close() except smb_structs.OperationFailure as err: if verbose > 1: print(err.message) fp.close() os.remove(tempname) continue num_files+=1 except OSError as err: if verbose > 0: print ('%s: %s' %(relativeRemotePath, err)) fp.close() os.remove(tempname) continue try: os.unlink(relativeRemotePath) except os.error: pass try: os.rename(tempname, relativeLocalPath) except os.error: continue for subdir in subdirs: localsub = os.path.join(cwd, subdir) newdest = smbpath+subdir+"\\" if verbose > 2: print('Changing current directory to: %s' % (share+newdest)) downloadSMBFiles(newdest, projPath, conn, regex, share) #Delete empty directories try: os.rmdir(localsub) except OSError: pass return num_files except smb_structs.OperationFailure as err: #print(err) return
def downloadFTPFiles(destination, host, regex): global num_files, size pwd = host.pwd() if destination and not os.path.isdir(destination): try: makedir(destination) except os.error as err: if verbose > 0: print(err) return subdirs = [] listing = [] host.retrlines('LIST', listing.append) for line in listing: prop = line.split(None, 8) if len(prop) < 6: continue filename = prop[-1].lstrip() #is symlink? if (prop[0][0]) is 'l': continue if filename is ('.' or '..'): continue #is directory? if (prop[0][0]) is 'd': subdirs.append(filename) continue fullname = os.path.join(destination, filename) tempname = os.path.join(destination, filename+'.tmp') try: os.unlink(tempname) except os.error: pass try: fp = open(tempname, 'wb') except IOError as err: if verbose > 0: print ("Can't create %r: %s" % (tempname, err)) continue fullpath='' if pwd is not '/': fullpath = '%s/%s' %(pwd, filename) else: fullpath = '/%s' % filename try: if not mirror: if verbose > 2: print('Checking file: %r' % fullpath) if regex.match(filename): if verbose > 2: print('***Found match...') filesize = host.size(filename) if verbose > 1: print('Downloading file: %s.....[%s]' % (fullpath, convert_bytes(filesize))) host.retrbinary('RETR ' + filename, fp.write, 8*1024) size += filesize num_files+=1 else: if verbose > 2: print('No match...') fp.close() os.remove(tempname) else: filesize = host.size(filename) if verbose > 1: print('Downloading file: %s.....[%s]' % (fullpath, convert_bytes(filesize))) host.retrbinary('RETR ' + filename, fp.write, 8*1024) size += filesize num_files+=1 except ftplib.error_perm as err: if verbose > 1: print ('%s: %s' %(fullpath, err)) fp.close() os.remove(tempname) continue fp.close() try: os.unlink(fullname) except os.error: pass try: os.rename(tempname, fullname) except os.error: continue for subdir in subdirs: localsub = os.path.join(destination, subdir) pwd = host.pwd() try: if verbose > 2: print('Changing current directory to: %r' % (pwd+subdir)) host.cwd(subdir) except ftplib.error_perm as err: if verbose > 0: print('%s\n%s' %(subdir, err)) continue else: downloadFTPFiles(localsub, host, regex) host.cwd('..') if host.pwd() != pwd: break #Delete empty directories try: os.rmdir(localsub) except OSError: pass return num_files
def downloadSMBFiles(smbpath, projPath, conn, regex, share): global num_files, size cwd = "" if smbpath[:1] is "\\": cwd = (smbpath[1:]) #trim first \ cwd = os.path.join(projPath, cwd) if cwd and not os.path.isdir(cwd): try: makedir(cwd) except os.error as err: if verbose > 0: print(err) return subdirs = [] try: listing = conn.listPath(share, smbpath) for l in listing: filename = l.filename if l.isDirectory: if filename is ".": continue if filename == "..": continue #Using 'is' didn't work subdirs.append(filename) continue else: relativeRemotePath = os.path.join(smbpath, l.filename) relativeLocalPath = os.path.join(share, relativeRemotePath[1:]) tempname = relativeLocalPath + '.tmp' try: os.unlink(tempname) except os.error: pass try: fp = open(tempname, 'wb') except IOError as err: if verbose > 0: print("Can't create %r: %s" % (tempname, err)) continue try: if not mirror: if verbose > 2: print('Checking file: %r' % relativeRemotePath) if regex.match(filename): print('***Found match...***') if verbose > 0: print("Downloading file: %s.....[%s]" % (relativeRemotePath, convert_bytes(l.file_size))) file_attributes, filesize = conn.retrieveFile( share, relativeRemotePath, fp) fp.close() size += filesize num_files += 1 else: if verbose > 2: print('Not matched...') fp.close() os.remove(tempname) else: if verbose > 1: print("Downloading file: %s.....[%s]" % (relativeRemotePath, convert_bytes(l.file_size))) try: file_attributes, filesize = conn.retrieveFile( share, relativeRemotePath, fp) size += filesize fp.close() except smb_structs.OperationFailure as err: if verbose > 1: print(err.message) fp.close() os.remove(tempname) continue num_files += 1 except OSError as err: if verbose > 0: print('%s: %s' % (relativeRemotePath, err)) fp.close() os.remove(tempname) continue try: os.unlink(relativeRemotePath) except os.error: pass try: os.rename(tempname, relativeLocalPath) except os.error: continue for subdir in subdirs: localsub = os.path.join(cwd, subdir) newdest = smbpath + subdir + "\\" if verbose > 2: print('Changing current directory to: %s' % (share + newdest)) downloadSMBFiles(newdest, projPath, conn, regex, share) #Delete empty directories try: os.rmdir(localsub) except OSError: pass return num_files except smb_structs.OperationFailure as err: #print(err) return