感觉网上很多的东西比较零零散散,这次整合一个可以扫描网段的ip地址并爬取IP地址中可以匿名登陆Ftp的数据
- 扫描一个网段,获取其中所有的开放FTP服务的机器的IP地址
- 依次获取每个FTP的文件目录内容,从每个FTP中下载一定量的文件
import platform import sys import os import time import _thread import datetime from ftplib import FTP class FTP_P(FTP): def dirs(self, *args): cmd = 'LIST' templist = [] func = None if args[-1:] and type(args[-1]) != type(''): args, func = args[:-1], args[-1] for arg in args: if arg: cmd = cmd + (' ' + arg) (cmd, ) return templist def download_single_url_ftp(stri): try: ftp = FTP_P() = "utf-8" (stri,21) # 连接的ftp sever和端口 ("anonymous") # 连接的用户名,密码 # prin()) #打印出欢迎信息 (r"/") # 设置FTP当前操作的路径 for i in (): print(i+'') ftpath = '/' localpath = 'D:/data/' ftpDownload(ftp,ftpath,localpath) () # 退出ftp except (ConnectionRefusedError, TimeoutError, WindowsError) as e: print("{}的Ftp连接失败 {}".format(stri,str(e))) print() pass except BaseException as e: print("{}的Ftp连接失败 {}".format(stri,str(e))) print() pass def ftpDownload(ftp, ftpath, localpath): ''' :param ftp: 登陆ftp返回的信息 :param ftpath: ftp中的目标路径 :param localpath: 存放下载文件的本地路径 :return: ''' print('Remote Path: {0}'.format(ftpath)) if not os.(localpath): os.makedirs(localpath)#如果文件不在创建文件 for file in (): print('file:', file) if len((file)) == 0: pass elif file == (file)[0]: print('扫描到文件') ftpDownloadFile(ftp,file, localpath) else: print('扫描到文件夹') path = () + '/' + file local = localpath+'/'+ file (path) ftpDownload(ftp, path, local)#递归 ('..') return True #python 149 def ftpDownloadFile(ftp, ftpfile, localfile): bufsize = 1024 path = os.(localfile,ftpfile) with open(path, 'wb') as fid: print('正在下载:',ftpfile) ('RETR {0}'.format(ftpfile), , bufsize) # 接收服务器文件并写入本地文件 print('下载完毕。') return True def get_os(): os = () if os == "Windows": return "n" else: return "c" def ping_ip(ip_str): cmd = ["ping", "-{op}".format(op=get_os()), "1", ip_str] output = os.popen(" ".join(cmd)).readlines() flag = False for line in list(output): if not line: continue if str(line).upper().find("TTL") >=0:#判断存活时间 flag = True break if flag: print("*** *** *** ip: %s 可以ping通 *** *** ***"%(ip_str)) download_single_url_ftp(ip_str) def find_ip(ip_prefix): for i in range(1,256): ip = ('%s.%s'%(ip_prefix,i)) _(ping_ip, (ip,)) ) # ping_ip(ip) if __name__ == "__main__": startTime = da() print("start time %s"%())) net=[1] args = "".join(("211.71."+net+".1"))#211.71.149 ip_prefix = '.'.join('.')[:-1])#211.71.149 find_ip(ip_prefix) endTime = da() print("end time %s"%())) print("total takes :",(endTime - startTime).seconds)