def vul_handler(module, ip, taskid): log.info(" start " + module + " ") # 准备进入实际脚本开始检查 task_suc, scan_status, scan_result = VulRun.check(module, ip) log.info("end {}, {}, {}".format(module, ip, " scan...")) if task_suc: task_suc = "FINISH" else: task_suc = "FAIL" ScanResult.sendmsg(taskid, task_suc, scan_status, scan_result, socket.gethostname())
def receive(): log.info("receive......") while True: try: task = redishelper.getonetask() taskid = task['id'] plugin_url = task['url'] module_name = task['name'] ip = task['ip'] if not os.path.exists( os.path.join(os.path.abspath(__file__), 'scripts', module_name + '.py')): try: r = requests.get(plugin_url, timeout=10) if r.status_code == 200: with open( os.path.join( os.path.dirname( os.path.abspath("__file__")), 'scripts', module_name + ".py"), "wb") as f: f.write(r.content) else: log.error("下载脚本失败:{}".format(r)) ScanResult.sendmsg(taskid, "FAIL", False, "下载脚本失败:{}".format(r), node=socket.gethostname()) continue except Exception as re: log.error("收取消息后,处理失败:{},\ndetail-{}".format( re, traceback.format_exc())) ScanResult.sendmsg(taskid, "FAIL", False, "下载脚本失败:{}".format(re), node=socket.gethostname()) continue ScanResult.sendmsg(taskid, "RUNNING", False, "", node=socket.gethostname()) # 返回running run_proc = multiprocessing.Process(target=vul_handler, args=(module_name, ip, taskid)) run_proc.start() # 等待结束 while True: run_proc.join(timeout=60) if run_proc.is_alive(): if redishelper.needstop(taskid): log.info("has task cancle event. {}".format(taskid)) run_proc.terminate() run_proc.join() log.info("{} : task cancel........") ScanResult.sendmsg(taskid, "CANCEL", False, "", socket.gethostname()) else: log.info("不需要提前结束,继续等待检查完成......") continue else: break except Exception as err: log.error("task basic_consume error:", traceback.format_exc()) ScanResult.sendmsg(taskid, "FAIL", False, str(err), socket.gethostname()) time.sleep(30)