掃描ip的全部端口

 

 

 

 

 

import openpyxl
from openpyxl.utils import coordinate_from_string,column_index_from_string
import socket
import threading

ports=[] #獲取全部的要掃描的端口,並加入列表
wb=openpyxl.load_workbook("F:\\test\\test.xlsx")
sheets=wb.get_sheet_names()
count=len(sheets)
ws_port=wb.get_sheet_by_name('port')


for row in ws_port.iter_rows(min_row=2,min_col=1,max_col=1):
port=[port.value for port in row]
ports.append(port[0]) ###獲取要掃描的端口

def scan_ip(row,ports): #掃描ip 每一個ip 掃描端口 方便多進程
for ip in row:
port_list = [] ##掃描到的端口
location = ip.coordinate ##獲取座標位置
xy = coordinate_from_string(location)
x = column_index_from_string(xy[0]) ##獲取行號
y = xy[1] ##獲取列號
ip = ip.value # 獲取單元格的值
scan_port(ip,ports,port_list)
P = ",".join(port_list) ##將列表轉換成字符串
sheet.cell(row=y, column=x + 1).value = P ##將掃描的端口寫入到ip後的單元格


def scan_port(ip,ports,port_list): #掃描端口
for port in ports:
conn=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
conn.settimeout(3)
try:
conn.connect((ip,port))
print('{} {} is ok'.format(ip,port))
port=str(port)
port_list.append(port) #將端口加入列表
except Exception as e:
print('{} {}is unreachable'.format(ip,port))



if __name__ == '__main__':

for i in range(1,count):
sheet=wb.get_sheet_by_name(sheets[i])
threads = []
for row in sheet.iter_rows(min_row=2, min_col=1, max_col=1): ##遍歷行 每一行就一個單元格,因此將單元格的for循環,放單函數裏,能夠併發多進程
th = threading.Thread(target=scan_ip,args=(row,ports))
th.start()
threads.append(th)
for th in threads:
th.join()

wb.save('F:\\test\\test_new.xlsx')


  能夠填寫多個網段  每一個sheet表是一個 網段併發

   

相關文章
相關標籤/搜索