import openpyxl from openpyxl.utils import coordinate_from_string,column_index_from_string import socket import threading ports=[] #获取所有的要扫描的端口,并加入列表 port_list = [] ##扫描到的端口 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 write_port_value(sheet,ports,port_list): #将扫描到的端口写入到ip的后一列 for row in sheet.iter_rows(min_row=2,min_col=1,max_col=1): ##遍历行 for ip in row: ##遍历行的单元格 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) ##扫描端口 th = threading.Thread(target=scan_port, args=(ip,ports,port_list)) th.start() threads.append(th) P = ",".join(port_list) ##将列表转换成字符串 sheet.cell(row=y,column=x+1).value=P ##将扫描的端口写入到ip后的单元格 port_list = [] 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__‘: threads = [] print(sheets,count) for i in range(1,count): print(i) sheet=wb.get_sheet_by_name(sheets[i]) write_port_value(sheet,ports,port_list) for th in threads: th.join() wb.save(‘F:\\test\\test_new.xlsx‘)
可以填写多个网段 每个sheet表是一个 网段
原文地址:https://www.cnblogs.com/xiao-ge/p/11024561.html
时间: 2024-10-12 02:29:53