多条告白如次剧本只需引入一次
运用python模块nmap,读取excel数据对多个ip端口探活扫描
我一个响指下来,每部分都要少一个外肾。。。
1、工欲善其事必先利其器。安置python-nmap模块。
pip list #cmd或powershell下 察看已安置模块,至于pip吩咐情况变量,本人处置 pip install python-nmap
编写大略的nmap单端口跑一下看看功效。'''即使安置了python-nmap模块还不许运用,那就自行安置nmap图形化界面,而后增添到情况变量里,如许就ok了,我的即是如许;'''import nmap np = nmap.PortScanner() a = np.scan(hosts='42.247.22.192',ports='80',arguments='-v -n -T4')print(a)归来截止: (要害的场合我已标红,重要即是看归来端口状况和效劳名)
{'nmap': {'command_line': 'nmap -oX - -p 80 -v -n -T4 42.247.22.192', 'scaninfo': {'tcp': {'method': 'syn', 'services': '80'}}, 'scanstats': {'timestr': 'Thu Jun 10 11:31:57 2021', 'elapsed': '1.72', 'uphosts': '1', 'downhosts': '0', 'totalhosts': '1'}}, 'scan': {'42.247.22.192': {'hostnames': [{'name': '', 'type': ''}], 'addresses': {'ipv4': '42.247.22.192'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'syn-ack'}, 'tcp': {80: {'state': 'open', 'reason': 'syn-ack', 'name': 'http','product': '', 'version': '', 'extrainfo': '', 'conf': '3', 'cpe': ''}}}}}2、下面临获得的数据整治一下输入,让他看上去简略少许import nmap np = nmap.PortScanner() a = np.scan(hosts='42.247.22.192',ports='80',arguments='-v -n -T4')ip = '42.247.22.192'for i in a['scan'][ip]['tcp'].keys(): state = a['scan'][ip]['tcp'][i]['state'] name = a['scan'][ip]['tcp'][i]['name'] print(ip,i,state,name)归来截止: 42.247.22.192 80 open http看着干净多了。
反面对于excel读取和线程的代码我径直贴了,即日工作多不细说了。。
3、运用python挪用excel模块和线程模块,读取excel数据,对多条数据举行扫描。
import nmapimport threadingfrom openpyxl import load_workbookfrom xlwt import Workbookwk = Workbook(encoding='utf-8')wsheet = wk.add_sheet('Worksheet')co = {}ls = []def read_excel_file(): wb = load_workbook('test/diqu.xlsx') #读取excel文献 # sheets = wb.get_sheet_names() # print(sheets) sheet = wb['暴出面财产全量'] # print(sheet) m = sheet['G'] #读取excelG列,我的G列是 ip:port,例:1.1.1.1:80 for cell in m: #这个for轮回用来分隔ip和端口,存到co字典 # print(cell.value) mn = cell.value.split(':') if mn[0] in co: co[mn[0]].append(mn[1]) else: try: co[mn[0]] = [mn[1]] except: co[mn[0]] = []def thread(ip_port): # 树立线程 thread_num = threading.Semaphore(20) # 树立线程数 thread_list = [] for IP, port in ip_port.items(): # 创造线程 t = threading.Thread(target=nmap_ping_scan, args=(IP, port, thread_num,)) thread_list.append(t) # print(t) for t in thread_list: # 发端线程 t.start() for t in thread_list: # 等候线程 t.join() print('线程中断')def nmap_ping_scan(ip,port,thread_num): #运用nmap扫描,截止惠存ls列表 global ls strport = ','.join(ports for ports in port) thread_num.acquire() # 线程锁 try: nm = nmap.PortScanner() global result np = nm.scan(hosts=ip,ports=strport,arguments="-v -n -T4") for i in np['scan'][ip]['tcp'].keys(): state = np['scan'][ip]['tcp'][i]['state'] name = np['scan'][ip]['tcp'][i]['name'] ls.extend([[ip,i,state,name]]) # print(ip,i,state) except Exception as e: # print(e) pass thread_num.release()def excel_write(ls): #把ls列表的数据生存到新的excel中 try: for u in range(len(ls)): p = 0 for k in ls[u]: wsheet.write(u,p,k) p += 1 # print(u,p,k) except: passif __name__ == '__main__': #步调启用 read_excel_file(