Python3批量掃描端口的例子-創(chuàng)新互聯(lián)

用法

創(chuàng)新互聯(lián)建站一直通過網(wǎng)站建設(shè)和網(wǎng)站營(yíng)銷幫助企業(yè)獲得更多客戶資源。 以"深度挖掘,量身打造,注重實(shí)效"的一站式服務(wù),以網(wǎng)站建設(shè)、網(wǎng)站制作、移動(dòng)互聯(lián)產(chǎn)品、全網(wǎng)整合營(yíng)銷推廣服務(wù)為核心業(yè)務(wù)。十年網(wǎng)站制作的經(jīng)驗(yàn),使用新網(wǎng)站建設(shè)技術(shù),全新開發(fā)出的標(biāo)準(zhǔn)網(wǎng)站,不但價(jià)格便宜而且實(shí)用、靈活,特別適合中小公司網(wǎng)站制作。網(wǎng)站管理系統(tǒng)簡(jiǎn)單易用,維護(hù)方便,您可以完全操作網(wǎng)站資料,是中小公司快速網(wǎng)站建設(shè)的選擇。

本腳本用于批量掃描端口

1.在同目錄下創(chuàng)建輸入文件,屬性inputFile為輸入文件名

2.屬性th為線程數(shù)

3.屬性port為探測(cè)的目標(biāo)端口

4.屬性arg為默認(rè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'}

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)站名稱:Python3批量掃描端口的例子-創(chuàng)新互聯(lián)
本文URL:http://bm7419.com/article34/dpcspe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、品牌網(wǎng)站建設(shè)營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站收錄、靜態(tài)網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站