扫描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表是一个 网段并发

   

相关文章
相关标签/搜索