Python3 批量掃描端口的例子
用法
本腳本用于批量掃描端口
1.在同目錄下創(chuàng)建輸入文件,屬性inputFile為輸入文件名
2.屬性th為線程數(shù)
3.屬性port為探測(cè)的目標(biāo)端口
4.屬性arg為默認(rèn)的命令
不推薦掃描全端口,速度非常慢.腳本中有常見(jiàn)端口,可直接使用
默認(rèn)命令下端口對(duì)應(yīng)服務(wù)可能存在不準(zhǔn)確的情況,可加-sV對(duì)版本進(jìn)行詳細(xì)探測(cè),或?qū)Ρ灸_本的輸出使用此腳本進(jìn)行專門的探測(cè)
代碼
#依賴python-nmap,openpyxl包 import nmap import openpyxl import IPy from multiprocessing import Pool as ThreadPool # 同目錄下創(chuàng)建一個(gè)input.txt,放入ip地址列(從xlsx中直接粘貼即可) expath='' inputFile='2001_3000.txt' th=20 #掃描端口 #port='1521,1158,2100,3306,1433,1434,5000,5432,27017,6379,11211,80,8080,443,8000,8081,7001,9080,9090,22,23,10027,3389,5631,111,6000,135,445,139,21,25,110,69,161,162,53,5901,5902,2601-2609 ' port='1-65535' #默認(rèn)命令 arg='-Pn -T4' #輸出文件名 import time time_rq = time.strftime('%Y-%m-%d', time.localtime(time.time())) outPutFile = time_rq + '2001_3000.txt' def readfile(path): # 返回url列表 file = open(path, 'r',encoding='utf-8') urlList=[] for eachline in file.readlines(): #獲得地址段 url=eachline.rsplit()[0] # add=IPy.IP(url)#地址段列表 # for u in add: # ip=u.__str__(); # urlList.append(ip) urlList.append(url); file.close() # 造infoList infoList=[] for url in urlList: info={'hostname':url,'port':port,'arg':arg} infoList.append(info) return infoList def scan(info): hostName=info['hostname'] port=info['port'] print(hostName+'掃描開始') scanner=nmap.PortScanner() scanner.scan(hosts=hostName,ports=port,arguments=arg) # print(res) out=open(outPutFile,'a+',encoding='utf-8') # out.write() state=scanner[hostName].state() tcp_ports=scanner[hostName].all_tcp() print(hostName+' '+state) for ports in tcp_ports: if scanner[hostName]['tcp'][ports]['state'] =='open' : st=hostName+' '+str(ports)+' '+scanner[hostName]['tcp'][ports]['name']+' '+scanner[hostName]['tcp'][ports]['state']+'\n' out.write(st) print(st) # print(hostName) out.close() def makeEx(): print('----------------開始寫入-------------------') wb=openpyxl.load_workbook(expath) sheet=wb.active c=open(outPutFile,'r',encoding='utf-8') i=1 for line in c.readlines(): linelist=line.split() ip=linelist[0] port=linelist[1] name=linelist[2] status=linelist[3] for j in range(4): if j==3: n=linelist[j] n=n.rsplit() n=n[0] sheet.cell(row=i, column=j + 1, value=n) else: sheet.cell(row=i,column=j+1,value=linelist[j]) i=i+1 c.close() wb.save(expath) print('----------------寫入完成-------------------') if __name__ == '__main__': print('----------------掃描開始-------------------') start=time.time() pool = ThreadPool(th) infolist=readfile(inputFile) pool.map(scan,infolist) pool.close() pool.join() finish=time.time() print('----------------掃描完成-------------------') t=finish-start print('用時(shí) %f'%t) out = open(outPutFile, 'a+', encoding='utf-8') out.write('\n用時(shí) %f'%t) # makeEx() # info={'hostname':'135.233.115.55','port':'22,23,21,80,3389,5901,5902','arg':'-Pn -sV -T4'}
輸入文件示例
61.128.108.94 61.128.111.246 61.128.122.111 222.83.20.18 61.128.108.67 120.70.226.131
輸出文件示例
106.124.93.76 22 ssh open 106.124.93.76 23 telnet open 106.124.93.76 8000 http-alt open 106.124.93.83 23 telnet open
以上這篇Python3 批量掃描端口的例子就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python 利用棧和隊(duì)列模擬遞歸的過(guò)程
這篇文章主要介紹了python 利用棧和隊(duì)列模擬遞歸的過(guò)程,文中并通過(guò)兩段代碼給大家介紹了下遞歸和非遞歸的區(qū)別,需要的朋友可以參考下2018-05-05python實(shí)現(xiàn)最長(zhǎng)公共子序列
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)最長(zhǎng)公共子序列的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05對(duì)Python模塊導(dǎo)入時(shí)全局變量__all__的作用詳解
今天小編就為大家分享一篇對(duì)Python模塊導(dǎo)入時(shí)全局變量__all__的作用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01python實(shí)現(xiàn)創(chuàng)建新列表和新字典,并使元素及鍵值對(duì)全部變成小寫
今天小編就為大家分享一篇python實(shí)現(xiàn)創(chuàng)建新列表和新字典,并使元素及鍵值對(duì)全部變成小寫,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01python中tkinter的應(yīng)用:修改字體的實(shí)例講解
今天小編就為大家分享一篇python中tkinter的應(yīng)用:修改字體的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07