def add_ip(task_name, task_ips, task_ports, task_type, cron, white_ip=""): mongo_task = Mongo.coll['tasks'] if mongo_task.find_one({ "name": task_name, "task_status": { "$ne": "finish" } }): # 有没完成的任务就不插入新任务了 return False create_time = datetime.now() ips = format_ip(task_ips) try: Log().info("开始插入数据") pipe = redis_web.pipe() insert_result = mongo_task.insert_one({ "name": task_name, "ip": task_ips, "port": task_ports, "diff_result": { "diff": 0 }, "task_status": "ready", "create_time": create_time, "task_type": task_type, "cron": cron }) nmapscan_key = "scan_" + str(insert_result.inserted_id) white_ip = get_white_ip(white_ip) ips = set(ips) - set(white_ip) for ip in ips: sub_task_dict = { "base_task_id": str(insert_result.inserted_id), "ip": ip, "port": task_ports, "task_status": "ready" } pipe.lpush(nmapscan_key, dict2str(sub_task_dict)) # mongo_task.update_one({"_id": insert_result.inserted_id}, {"$set": {"sub_task": sub_task_list}}) if insert_result: pipe.execute() Log().info("任务【%s】的数据插入完毕" % task_name) mongo_task.update_one({"_id": insert_result.inserted_id}, {"$set": { "task_status": "running" }}) return True except: Log().exception("插入数据失败") return False
def dict2str(dictionary): try: if type(dictionary) == str: return dictionary return json.dumps(dictionary) except TypeError as e: Log().exception("conv dict failed : %s" % e)
def send_mail(subject, contents, host, use_ssl, sender, pwd, email_address): try: message = Message(From=sender, To=email_address, charset="utf-8") message.Subject = subject message.Html = contents if sender[sender.find("@") + 1:] in host: sender = sender[:sender.find("@")] mailer = Mailer(host=host, use_ssl=use_ssl, usr=sender, pwd=pwd) mailer.send(message, debug=False) Log().info("sender:%s,to=%s" % (sender, email_address)) except Exception as e: Log().exception(e) return False return True
def send_mail(subject, contents, host, use_ssl, sender, pwd, email_address): try: message = Message(From=sender, To=email_address, charset="utf-8") message.Subject = subject message.Html = contents if host in ["smtp.qiye.aliyun.com", "smtp.mxhichina.com"]: mailer = Mailer(host=host, use_ssl=use_ssl, usr=sender, pwd=pwd) else: mailer = Mailer(host=host, use_ssl=use_ssl, usr=sender[:sender.find("@")], pwd=pwd) mailer.send(message, debug=False) Log().info("sender:%s,to=%s" % (sender, email_address)) except Exception as e: Log().exception(e) return False return True
# -*- coding: utf-8 -*- import json, struct, socket import re import requests from datetime import datetime from log_handle import Log from app import Mongo from app import redis_web try: from mailer import Mailer from mailer import Message except ImportError: Log().warning("error Missing Mailer") def send_mail(subject, contents, host, use_ssl, sender, pwd, email_address): try: message = Message(From=sender, To=email_address, charset="utf-8") message.Subject = subject message.Html = contents mailer = Mailer(host=host, use_ssl=use_ssl, usr=sender[:sender.find("@")], pwd=pwd) mailer.send(message, debug=False) Log().info("sender:%s,to=%s" % (sender, email_address))