用法
本腳本用于批量掃描端口
1.在同目錄下創(chuàng)建輸入文件,屬性inputFile為輸入文件名
2.屬性th為線程數(shù)
3.屬性port為探測的目標(biāo)端口
4.屬性arg為默認(rèn)的命令
不推薦掃描全端口,速度非常慢.腳本中有常見端口,可直接使用
默認(rèn)命令下端口對應(yīng)服務(wù)可能存在不準(zhǔn)確的情況,可加-sV對版本進(jìn)行詳細(xì)探測,或?qū)Ρ灸_本的輸出使用 此腳本 進(jìn)行專門的探測
代碼
#依賴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 批量掃描端口的例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

