import schedule import subprocess import time import os from datetime import datetime BASE_DIR = os.path.dirname(os.path.abspath(__file__)) ENV_DIR= os.path.join(BASE_DIR, "venv", "bin", "python3") def log(message): print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {message}") def run_host_discovery(): log(" Running Host Discovery...") try: subprocess.run([ENV_DIR, os.path.join(BASE_DIR, "Host_Discovery", "networkdiscovery.py")], check=True) log(" Host Discovery completed") except subprocess.CalledProcessError as e: log(f" Host Discovery failed: {e}") def run_port_scan(): active_hosts_file = os.path.join(BASE_DIR, "data", "active_hosts.csv") if not os.path.exists(active_hosts_file) or os.path.getsize(active_hosts_file) == 0: log(" Skipping Port Scan: active_hosts.csv not found or is empty.") return log(" Running Port Scan...") try: subprocess.run(["sudo", ENV_DIR, os.path.join(BASE_DIR, "Host_Discovery", "portscanner.py")], check=True) log("Port Scan completed") except subprocess.CalledProcessError as e: log(f" Port Scan failed: {e}") def run_vulnerability_scan(): active_hosts_file = os.path.join(BASE_DIR, "data", "active_hosts.csv") if not os.path.exists(active_hosts_file) or os.path.getsize(active_hosts_file) == 0: log("Skipping Vulnerability Scan: active_hosts.csv not found or is empty.") return log(" Running Vulnerability Scan Sequence...") scripts = [ "createTargets.py", "taskmaker.py", "starttask.py", "getreports.py", "generate_reports.py" ] for script in scripts: path = os.path.join(BASE_DIR, "Vunerability_Scanner/", script) try: subprocess.run([ENV_DIR, path], check=True) log(f" Finished {script}") except subprocess.CalledProcessError as e: log(f" Error in {script}: {e}") break # Schedule tasks schedule.every().hour.at(":00").do(run_host_discovery) schedule.every().hour.at(":05").do(run_port_scan) schedule.every(11).hours.at(":10").do(run_vulnerability_scan) log(" Scheduler is running...") # Run initial discovery and scan log(" Running initial startup scan sequence...") run_host_discovery() run_port_scan() run_vulnerability_scan() while True: schedule.run_pending() time.sleep(30)