diff options
| author | zhangchengwei <[email protected]> | 2022-07-14 17:46:56 +0800 |
|---|---|---|
| committer | zhangchengwei <[email protected]> | 2022-07-14 17:46:56 +0800 |
| commit | 67b76148cfc5867b129163e9a24b1e27d1bfc8cc (patch) | |
| tree | ea191393852437890aa5434ef44bd138ae1c7ab2 | |
| parent | 58fb70bc3d52f987f0b52d67db2a8304a6fea04e (diff) | |
添加捕包逻辑
| -rw-r--r-- | control/business.py | 98 |
1 files changed, 69 insertions, 29 deletions
diff --git a/control/business.py b/control/business.py index aa9b832..0a6ad26 100644 --- a/control/business.py +++ b/control/business.py @@ -85,8 +85,8 @@ def kill_all(instructNo_setup_json_abs_path, tsglogger): for i in child_pid_list: child_child_process = subprocess.Popen("pgrep -P {}".format(i), stdout=subprocess.PIPE, shell=True, universal_newlines=True) child_child_list0 = child_child_process.communicate()[0].split() - if len(child_child_list0) == 0: - continue + if len(child_child_list0) == 0: #为了删除捕包的进程 + child_child_list0 = ["9999999999"] child_child_list.append([i, child_child_list0[0]]) #print("子子进程:{}".format(child_child_list)) tsglogger.info("子子进程:{}".format(child_child_list)) @@ -94,20 +94,26 @@ def kill_all(instructNo_setup_json_abs_path, tsglogger): #kill掉子子的进程 if len(child_child_list) > 0: for child_pid in child_child_list: - if len(child_pid) != 2: + if child_pid[-1] == "9999999999": #为了删除捕包的进程 + kill_child_process = subprocess.Popen(['sudo','kill','-2', child_pid[0] ], stdout=subprocess.PIPE,universal_newlines=True) + time.sleep(1) + kill_child_process = subprocess.Popen(['sudo','kill','-2', child_pid[0] ], stdout=subprocess.PIPE,universal_newlines=True) continue + child_child_pid_info = subprocess.Popen("sudo ps -ef |grep {}".format(child_pid[-1]), stdout=subprocess.PIPE,universal_newlines=True, shell=True) child_child_pid_info_str = child_child_pid_info.stdout.read() tsglogger.info(child_child_pid_info_str) #tsglogger.info(child_child_list) kill_child_child_process = subprocess.Popen(['sudo','kill','-2', child_pid[-1] ], stdout=subprocess.PIPE,universal_newlines=True) - time.sleep(0.8) + time.sleep(1) output = kill_child_child_process.communicate() kill_child_child_process = subprocess.Popen(['sudo','kill','-2', child_pid[-1] ], stdout=subprocess.PIPE,universal_newlines=True) - time.sleep(0.8) + time.sleep(1) + kill_child_child_process = subprocess.Popen(['sudo','kill','-2', child_pid[-1] ], stdout=subprocess.PIPE,universal_newlines=True) + time.sleep(1) kill_child_process = subprocess.Popen(['sudo','kill','-2', child_pid[0] ], stdout=subprocess.PIPE,universal_newlines=True) - time.sleep(0.8) + time.sleep(1) kill_child_process = subprocess.Popen(['sudo','kill','-2', child_pid[0] ], stdout=subprocess.PIPE,universal_newlines=True) @@ -336,7 +342,15 @@ def execute_combine_capture_packet(robot_command, tsglogger): d2 = re.search("--output\s.*?xml", robot_command).group(0) tsglogger.debug(f"解析到到的d2信息:{d2}") d22 = d2.lstrip("--output ").rstrip("xml") - source_file_info = d11 + "/" + d22 + "pcap" # 捕包文件绝对路径+名称 + d22 = d22.rstrip(".") + d_name = re.search("--test.*\.robot", robot_command).group(0) + d_name_list = d_name.split() + d_name_2 = d_name_list[2].rstrip(".robot") + d_name_2 = d_name_2.replace("/", "_") + d_name_1 = d_name_list[1] + d_name_3 = "{}_{}_{}.".format(d22,d_name_2, d_name_1) #report_output2022071311391002500112003285_9_gap_tsg_api_case_policies_security_ssl_allow-fqdn-exactly-famousSites. + + source_file_info = d11 + "/" + d_name_3 + "pcap" # 捕包文件绝对路径+名称 /opt/test/automation/tasks/2022071409285202500112116766/capturepacket/report_output2022071409285202500112116766_9_gap_tsg_api_case_policies_security_ssl_allow-fqdn-exactly-famousSites.pcap if not os.path.exists(d11): #判断捕包路径是否存在 os.makedirs(d11) #ip = "192.168.50.29" #ip如果没有流量 则不会捕包 @@ -345,9 +359,14 @@ def execute_combine_capture_packet(robot_command, tsglogger): #ip = "192.168.42.97" #eth = "eth0" + ip = "192.168" tsglogger.debug(f"捕包网卡信息:{eth}") tsglogger.debug(f"捕包的ip信息:{ip}") #tsglogger.error("测试信息。。。") + + + """ + scapy捕包,会出现未知错误 try: dpkt = AsyncSniffer(iface=eth, filter="host {}".format(ip), prn=lambda x:wrpcap(source_file_info, x, append=True)) except Exception as e: @@ -356,6 +375,13 @@ def execute_combine_capture_packet(robot_command, tsglogger): else: dpkt.start() time.sleep(0.3) + """ + + #使用linux中tcpdump捕包命令 + dcpdump_cmd = "tcpdump -i {} host {} -w {}".format(eth, ip, source_file_info) + dpkt = subprocess.Popen(dcpdump_cmd, shell=True, stdout=subprocess.PIPE) + time.sleep(1) + # capture_command = "tcpdump -i eth1 host 192.168.50.57 -w ss.pcap " # capture_command1 = capture_command.replace("eth1", eth).replace("192.168.50.57", ip).replace("ss.pcap", source_file_info) # print(capture_command1) @@ -365,7 +391,7 @@ def execute_combine_capture_packet(robot_command, tsglogger): tsglogger.info(f"线程:{threading.current_thread().name},开启捕包......") tsglogger.info(f"数据包信息:{eth}--{ip}--{source_file_info}") tsglogger.info(f"结束函数:{execute_combine_capture_packet.__name__}") - return dpkt + return [dpkt, [source_file_info]] def execute_commond_of_robot(robot_command, f_dict, tsglogger): @@ -682,37 +708,45 @@ def callback_rest_result_report(instructNo, tsglogger): def callbaock_upload_result_file(file_path, instructNo, tsglogger): """ - 回传报告接口 + 回传报告接口,也可以回传捕包文件 :param file_path: 文件绝对路径 [report_path, log_path, xml_path] 合并结果的形式为列表,则直接使用 :param instructNo: 唯一标识 :return: """ tsglogger.info(f"开始函数:{callbaock_upload_result_file.__name__}") - tsglogger.info(file_path) - tsglogger.info(instructNo) + tsglogger.info(file_path) #['/opt/test/automation/tasks/2022071409285202500112116766/report.html', '/opt/test/automation/tasks/2022071409285202500112116766/log.html', '/opt/test/automation/tasks/2022071409285202500112116766/output_2022071409285202500112116766.xml'] + tsglogger.info(instructNo) #2022071409285202500112116766 config = configparser.ConfigParser() #从配置文件中拿到路径ip global config_abs_path config.read(config_abs_path) fusion_host = config.get("interface_info", "fusion_host") #req_header = {"Content-Type": "multipart/form-data"} #python上传表单或文件时,不要添加content-type请求体信息,添加了会报错的 - #从传入的参数中,拿到文件绝对路径 - report_path = file_path[0] - report_path = re.sub("report.html", "report_{}.html".format(instructNo), report_path) - log_path = file_path[1] - log_path = re.sub("log.html", "log_{}.html".format(instructNo), log_path) - xml_path = file_path[2] + + if len(file_path) == 1: #回传捕包文件逻辑处理 + #dpk = [dpkt, [source_file_info]] + # /opt/test/automation/tasks/2022071409285202500112116766/capturepacket/report_output2022071409285202500112116766_9_gap_tsg_api_case_policies_security_ssl_allow-fqdn-exactly-famousSites.pcap + packet_path = file_path[0] + data_list = [{"file":(None, packet_path, "String")}] #为了和下面逻辑一致,此处也使用列表表示 + else: #回传报表文件逻辑处理 + #从传入的参数中,拿到文件绝对路径 + report_path = file_path[0] + report_path = re.sub("report.html", "report_{}.html".format(instructNo), report_path) + log_path = file_path[1] + log_path = re.sub("log.html", "log_{}.html".format(instructNo), log_path) + xml_path = file_path[2] + # files = { + # "file":[(report_path, open(report_path, "rb"), "MultipartFile"), #"text/html" + # (log_path, open(log_path, "rb"), "MultipartFile"), #"text/html" + # (xml_path, open(xml_path, "rb"), "MultipartFile") #"text/html" + # ] + # } + data_list = [{"file":(None, report_path, "String")}, {"file":(None, log_path, "String")}, {"file":(None, xml_path, "String")}] + #data_list = [{"file":report_path}, {"file":log_path}, {"file":xml_path}] + tsglogger.info(f"过程参数,data_list:{data_list}") + uploadlocal_url = fusion_host + "/callback/uploadlocal/{}".format(instructNo) tsglogger.info("测试用例的总的结果文件提交fusion接口") tsglogger.info(uploadlocal_url) - # files = { - # "file":[(report_path, open(report_path, "rb"), "MultipartFile"), #"text/html" - # (log_path, open(log_path, "rb"), "MultipartFile"), #"text/html" - # (xml_path, open(xml_path, "rb"), "MultipartFile") #"text/html" - # ] - # } - data_list = [{"file":(None, report_path, "String")}, {"file":(None, log_path, "String")}, {"file":(None, xml_path, "String")}] - #data_list = [{"file":report_path}, {"file":log_path}, {"file":xml_path}] - tsglogger.info(f"过程参数,data_list:{data_list}") n = 0 for data in data_list: while n < 3: @@ -1085,7 +1119,7 @@ mobile_case_used_list = [] mobile_info_block_count = 0 mobile_case_used_count = 0 mobile_case_sum_count = 0 -def capture_packet_and_execute_robot(robot_command, client_info, f_dict, common_configration_variable_list, tsglogger): +def capture_packet_and_execute_robot(robot_command, client_info, f_dict, common_configration_variable_list, instructNo, tsglogger): """ 结合捕包函数和执行测试命令函数,为多线程做准备 :param robot_command: 执行测试任务命令 @@ -1191,10 +1225,16 @@ def capture_packet_and_execute_robot(robot_command, client_info, f_dict, common_ if str(pactureParm) == "1": #判断是否捕包 dpkt = execute_combine_capture_packet(robot_command, tsglogger) + execute_commond_of_robot(robot_command, f_dict, tsglogger) if str(pactureParm) == "1": - dpkt.stop() + dpkt[0].kill() #dpk = [dpkt, [source_file_info]] tsglogger.info(f"线程:{threading.current_thread().name}:完成捕包") + #回传捕包数据文件 + tsglogger.info(f"线程:{threading.current_thread().name}:开始回传捕包数据文件") + callbaock_upload_result_file(dpkt[1], instructNo, tsglogger) + tsglogger.info(f"线程:{threading.current_thread().name}:完成回传捕包数据文件") + client_info_dict[threading_name][1] = 1 #将设备资源重新设置为可以状态 tsglogger.info(f"结束函数:{capture_packet_and_execute_robot.__name__}") return client_info @@ -1229,7 +1269,7 @@ def mult_thread_pool_capture_test_report(all_client_equipment_execute_command_li for i in range(len(task[0])): p_capture_and_test_task = p_executor_mult_and_test_pool.submit(capture_packet_and_execute_robot, task[0][i], task[1][client_count], f_dict, - common_configration_variable_list, tsglogger) + common_configration_variable_list, instructNo, tsglogger) p_executor_mult_and_test_pool.max_workers = 2 p_task_list.append(p_capture_and_test_task) |
