"""
    调度模块
"""
# from lvsocutil.mongodb import MDB
import re
import subprocess
import shlex
import sys
import os

def is_domain_target(target):
    if target.split(".")[-1] in ["com", "org", "net", "cn"]:
        return True
    return False

def is_c_target(target):
    p = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)/\d{1,2}$')
    if p.search(target):
        return True
    return False

def is_ip_target(target):
    p = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')
    if p.match(target):
        return True
    return False

# print(is_c_target("1.1.1.1/24"))

class Scheduler(object):
    # 加载目标
    def __init__(self):
        self.init_context()

    def init_context(self):
        if os.path.exists("report/data.db"):
            os.remove("report/data.db")
        if os.path.exists("report/report.xls"):   
            os.remove("report/report.xls")

    def load_target(self):
        # mdb = MDB()
        # target = mdb.PentestTarget.find_one({'status':None})
        # if target:
        #     self.target = target['target']
        targets = []
        with open("target.txt") as f:
            for l in f:
                targets.append(l.strip())
        # 根据第一条记录来决定用什么方法执行
        self.targets = targets
        
    def c_task(self):
        print("开始C段漏洞扫描")
        print("\n".join(self.targets) + "\n")
        # 准备目标文件
        with open("out/target.ip", "w") as f:
            f.write("\n".join(self.targets) + "\n")
        # 开始扫描
        nmap_path = "plugins/nmap"
        p = subprocess.Popen(shlex.split("sh {}/scan_nmap.sh scan_c out/target.ip".format(nmap_path)),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("python {}/parse_nmap.py scan_c".format(nmap_path)),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("sh main/main_hydra.sh"),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("sh main/main_ncrack.sh"),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("sh main/main_wascan.sh"),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("python plugins/report/sqlite2xls.py"),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("python plugins/report/send_mail.py"),close_fds=True, bufsize=-1)
        p.communicate()

    def ip_task(self):
        print("开始ip漏洞扫描")
        # 准备目标文件
        
        with open("out/target.ip", "w") as f:
            f.write("\n".join(self.targets) + "\n")
        # 开始扫描
        p = subprocess.Popen(shlex.split("sh main/main_nmap_ips.sh"),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("sh main/main_hydra.sh"),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("sh main/main_ncrack.sh"),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("sh main/main_wascan.sh"),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("python plugins/report/sqlite2xls.py"),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("python plugins/report/send_mail.py"),close_fds=True, bufsize=-1)
        p.communicate()
    
    def domain_task(self):
        pass

    def schedule(self):
        if is_domain_target(self.targets[0]):
           print("domain target")
        elif is_c_target(self.targets[0]):
           print("c target")
           self.c_task()
        elif is_ip_target(self.targets[0]):
           print("ip target")
           self.ip_task()

if __name__ == "__main__":
    scheduler = Scheduler()
    scheduler.load_target()
    scheduler.schedule()


862218eb03d474dd85ea4503f604836d83b2f4c7
<mask token>


class Scheduler(object):

    def __init__(self):
        self.init_context()
    <mask token>

    def load_target(self):
        targets = []
        with open('target.txt') as f:
            for l in f:
                targets.append(l.strip())
        self.targets = targets

    def c_task(self):
        print('开始C段漏洞扫描')
        print('\n'.join(self.targets) + '\n')
        with open('out/target.ip', 'w') as f:
            f.write('\n'.join(self.targets) + '\n')
        nmap_path = 'plugins/nmap'
        p = subprocess.Popen(shlex.split(
            'sh {}/scan_nmap.sh scan_c out/target.ip'.format(nmap_path)),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('python {}/parse_nmap.py scan_c'.
            format(nmap_path)), close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_hydra.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_ncrack.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_wascan.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split(
            'python plugins/report/sqlite2xls.py'), close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split(
            'python plugins/report/send_mail.py'), close_fds=True, bufsize=-1)
        p.communicate()

    def ip_task(self):
        print('开始ip漏洞扫描')
        with open('out/target.ip', 'w') as f:
            f.write('\n'.join(self.targets) + '\n')
        p = subprocess.Popen(shlex.split('sh main/main_nmap_ips.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_hydra.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_ncrack.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_wascan.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split(
            'python plugins/report/sqlite2xls.py'), close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split(
            'python plugins/report/send_mail.py'), close_fds=True, bufsize=-1)
        p.communicate()

    def domain_task(self):
        pass

    def schedule(self):
        if is_domain_target(self.targets[0]):
            print('domain target')
        elif is_c_target(self.targets[0]):
            print('c target')
            self.c_task()
        elif is_ip_target(self.targets[0]):
            print('ip target')
            self.ip_task()


<mask token>

<mask token>


def is_ip_target(target):
    p = re.compile(
        '^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$'
        )
    if p.match(target):
        return True
    return False


class Scheduler(object):

    def __init__(self):
        self.init_context()

    def init_context(self):
        if os.path.exists('report/data.db'):
            os.remove('report/data.db')
        if os.path.exists('report/report.xls'):
            os.remove('report/report.xls')

    def load_target(self):
        targets = []
        with open('target.txt') as f:
            for l in f:
                targets.append(l.strip())
        self.targets = targets

    def c_task(self):
        print('开始C段漏洞扫描')
        print('\n'.join(self.targets) + '\n')
        with open('out/target.ip', 'w') as f:
            f.write('\n'.join(self.targets) + '\n')
        nmap_path = 'plugins/nmap'
        p = subprocess.Popen(shlex.split(
            'sh {}/scan_nmap.sh scan_c out/target.ip'.format(nmap_path)),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('python {}/parse_nmap.py scan_c'.
            format(nmap_path)), close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_hydra.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_ncrack.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_wascan.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split(
            'python plugins/report/sqlite2xls.py'), close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split(
            'python plugins/report/send_mail.py'), close_fds=True, bufsize=-1)
        p.communicate()

    def ip_task(self):
        print('开始ip漏洞扫描')
        with open('out/target.ip', 'w') as f:
            f.write('\n'.join(self.targets) + '\n')
        p = subprocess.Popen(shlex.split('sh main/main_nmap_ips.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_hydra.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_ncrack.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_wascan.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split(
            'python plugins/report/sqlite2xls.py'), close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split(
            'python plugins/report/send_mail.py'), close_fds=True, bufsize=-1)
        p.communicate()

    def domain_task(self):
        pass

    def schedule(self):
        if is_domain_target(self.targets[0]):
            print('domain target')
        elif is_c_target(self.targets[0]):
            print('c target')
            self.c_task()
        elif is_ip_target(self.targets[0]):
            print('ip target')
            self.ip_task()


<mask token>

<mask token>


def is_c_target(target):
    p = re.compile(
        '^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)/\\d{1,2}$'
        )
    if p.search(target):
        return True
    return False


def is_ip_target(target):
    p = re.compile(
        '^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$'
        )
    if p.match(target):
        return True
    return False


class Scheduler(object):

    def __init__(self):
        self.init_context()

    def init_context(self):
        if os.path.exists('report/data.db'):
            os.remove('report/data.db')
        if os.path.exists('report/report.xls'):
            os.remove('report/report.xls')

    def load_target(self):
        targets = []
        with open('target.txt') as f:
            for l in f:
                targets.append(l.strip())
        self.targets = targets

    def c_task(self):
        print('开始C段漏洞扫描')
        print('\n'.join(self.targets) + '\n')
        with open('out/target.ip', 'w') as f:
            f.write('\n'.join(self.targets) + '\n')
        nmap_path = 'plugins/nmap'
        p = subprocess.Popen(shlex.split(
            'sh {}/scan_nmap.sh scan_c out/target.ip'.format(nmap_path)),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('python {}/parse_nmap.py scan_c'.
            format(nmap_path)), close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_hydra.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_ncrack.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_wascan.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split(
            'python plugins/report/sqlite2xls.py'), close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split(
            'python plugins/report/send_mail.py'), close_fds=True, bufsize=-1)
        p.communicate()

    def ip_task(self):
        print('开始ip漏洞扫描')
        with open('out/target.ip', 'w') as f:
            f.write('\n'.join(self.targets) + '\n')
        p = subprocess.Popen(shlex.split('sh main/main_nmap_ips.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_hydra.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_ncrack.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_wascan.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split(
            'python plugins/report/sqlite2xls.py'), close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split(
            'python plugins/report/send_mail.py'), close_fds=True, bufsize=-1)
        p.communicate()

    def domain_task(self):
        pass

    def schedule(self):
        if is_domain_target(self.targets[0]):
            print('domain target')
        elif is_c_target(self.targets[0]):
            print('c target')
            self.c_task()
        elif is_ip_target(self.targets[0]):
            print('ip target')
            self.ip_task()


<mask token>

<mask token>


def is_domain_target(target):
    if target.split('.')[-1] in ['com', 'org', 'net', 'cn']:
        return True
    return False


def is_c_target(target):
    p = re.compile(
        '^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)/\\d{1,2}$'
        )
    if p.search(target):
        return True
    return False


def is_ip_target(target):
    p = re.compile(
        '^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$'
        )
    if p.match(target):
        return True
    return False


class Scheduler(object):

    def __init__(self):
        self.init_context()

    def init_context(self):
        if os.path.exists('report/data.db'):
            os.remove('report/data.db')
        if os.path.exists('report/report.xls'):
            os.remove('report/report.xls')

    def load_target(self):
        targets = []
        with open('target.txt') as f:
            for l in f:
                targets.append(l.strip())
        self.targets = targets

    def c_task(self):
        print('开始C段漏洞扫描')
        print('\n'.join(self.targets) + '\n')
        with open('out/target.ip', 'w') as f:
            f.write('\n'.join(self.targets) + '\n')
        nmap_path = 'plugins/nmap'
        p = subprocess.Popen(shlex.split(
            'sh {}/scan_nmap.sh scan_c out/target.ip'.format(nmap_path)),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('python {}/parse_nmap.py scan_c'.
            format(nmap_path)), close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_hydra.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_ncrack.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_wascan.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split(
            'python plugins/report/sqlite2xls.py'), close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split(
            'python plugins/report/send_mail.py'), close_fds=True, bufsize=-1)
        p.communicate()

    def ip_task(self):
        print('开始ip漏洞扫描')
        with open('out/target.ip', 'w') as f:
            f.write('\n'.join(self.targets) + '\n')
        p = subprocess.Popen(shlex.split('sh main/main_nmap_ips.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_hydra.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_ncrack.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split('sh main/main_wascan.sh'),
            close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split(
            'python plugins/report/sqlite2xls.py'), close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split(
            'python plugins/report/send_mail.py'), close_fds=True, bufsize=-1)
        p.communicate()

    def domain_task(self):
        pass

    def schedule(self):
        if is_domain_target(self.targets[0]):
            print('domain target')
        elif is_c_target(self.targets[0]):
            print('c target')
            self.c_task()
        elif is_ip_target(self.targets[0]):
            print('ip target')
            self.ip_task()


<mask token>

"""
    调度模块
"""
# from lvsocutil.mongodb import MDB
import re
import subprocess
import shlex
import sys
import os

def is_domain_target(target):
    if target.split(".")[-1] in ["com", "org", "net", "cn"]:
        return True
    return False

def is_c_target(target):
    p = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)/\d{1,2}$')
    if p.search(target):
        return True
    return False

def is_ip_target(target):
    p = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')
    if p.match(target):
        return True
    return False

# print(is_c_target("1.1.1.1/24"))

class Scheduler(object):
    # 加载目标
    def __init__(self):
        self.init_context()

    def init_context(self):
        if os.path.exists("report/data.db"):
            os.remove("report/data.db")
        if os.path.exists("report/report.xls"):   
            os.remove("report/report.xls")

    def load_target(self):
        # mdb = MDB()
        # target = mdb.PentestTarget.find_one({'status':None})
        # if target:
        #     self.target = target['target']
        targets = []
        with open("target.txt") as f:
            for l in f:
                targets.append(l.strip())
        # 根据第一条记录来决定用什么方法执行
        self.targets = targets
        
    def c_task(self):
        print("开始C段漏洞扫描")
        print("\n".join(self.targets) + "\n")
        # 准备目标文件
        with open("out/target.ip", "w") as f:
            f.write("\n".join(self.targets) + "\n")
        # 开始扫描
        nmap_path = "plugins/nmap"
        p = subprocess.Popen(shlex.split("sh {}/scan_nmap.sh scan_c out/target.ip".format(nmap_path)),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("python {}/parse_nmap.py scan_c".format(nmap_path)),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("sh main/main_hydra.sh"),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("sh main/main_ncrack.sh"),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("sh main/main_wascan.sh"),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("python plugins/report/sqlite2xls.py"),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("python plugins/report/send_mail.py"),close_fds=True, bufsize=-1)
        p.communicate()

    def ip_task(self):
        print("开始ip漏洞扫描")
        # 准备目标文件
        
        with open("out/target.ip", "w") as f:
            f.write("\n".join(self.targets) + "\n")
        # 开始扫描
        p = subprocess.Popen(shlex.split("sh main/main_nmap_ips.sh"),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("sh main/main_hydra.sh"),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("sh main/main_ncrack.sh"),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("sh main/main_wascan.sh"),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("python plugins/report/sqlite2xls.py"),close_fds=True, bufsize=-1)
        p.communicate()
        p = subprocess.Popen(shlex.split("python plugins/report/send_mail.py"),close_fds=True, bufsize=-1)
        p.communicate()
    
    def domain_task(self):
        pass

    def schedule(self):
        if is_domain_target(self.targets[0]):
           print("domain target")
        elif is_c_target(self.targets[0]):
           print("c target")
           self.c_task()
        elif is_ip_target(self.targets[0]):
           print("ip target")
           self.ip_task()

if __name__ == "__main__":
    scheduler = Scheduler()
    scheduler.load_target()
    scheduler.schedule()

