Show total IPs, ETA, and live masscan progress ticker
This commit is contained in:
+37
-2
@@ -1,7 +1,8 @@
|
||||
import json
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
import xml.etree.ElementTree as ET
|
||||
from pathlib import Path
|
||||
|
||||
@@ -14,6 +15,12 @@ def check_deps() -> list[str]:
|
||||
return missing
|
||||
|
||||
|
||||
def _fmt_elapsed(seconds: int) -> str:
|
||||
m, s = divmod(seconds, 60)
|
||||
h, m = divmod(m, 60)
|
||||
return f"{h}h{m:02d}m{s:02d}s" if h else f"{m}m{s:02d}s"
|
||||
|
||||
|
||||
def masscan_sweep(cidrs: list[str], ports: list[int], rate: int, output_dir: Path) -> list[dict]:
|
||||
if not cidrs or not ports:
|
||||
return []
|
||||
@@ -32,8 +39,26 @@ def masscan_sweep(cidrs: list[str], ports: list[int], rate: int, output_dir: Pat
|
||||
"--wait", "3",
|
||||
]
|
||||
|
||||
stop = threading.Event()
|
||||
start = time.time()
|
||||
|
||||
def _progress():
|
||||
while not stop.is_set():
|
||||
found = 0
|
||||
if out_file.exists():
|
||||
try:
|
||||
found = out_file.read_text(errors="replace").count('"ip"')
|
||||
except Exception:
|
||||
pass
|
||||
elapsed = _fmt_elapsed(int(time.time() - start))
|
||||
print(f"\r [{elapsed}] masscan running — {found} hosts found so far ", end="", flush=True)
|
||||
stop.wait(timeout=4)
|
||||
|
||||
t = threading.Thread(target=_progress, daemon=True)
|
||||
t.start()
|
||||
|
||||
try:
|
||||
proc = subprocess.Popen(cmd, stderr=subprocess.DEVNULL)
|
||||
proc = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
proc.wait(timeout=3600)
|
||||
except subprocess.TimeoutExpired:
|
||||
proc.kill()
|
||||
@@ -41,10 +66,20 @@ def masscan_sweep(cidrs: list[str], ports: list[int], rate: int, output_dir: Pat
|
||||
except KeyboardInterrupt:
|
||||
proc.kill()
|
||||
proc.wait()
|
||||
stop.set()
|
||||
t.join(timeout=2)
|
||||
print()
|
||||
raise
|
||||
except FileNotFoundError:
|
||||
stop.set()
|
||||
t.join(timeout=2)
|
||||
print()
|
||||
raise RuntimeError("masscan not found — install it first")
|
||||
|
||||
stop.set()
|
||||
t.join(timeout=2)
|
||||
print()
|
||||
|
||||
if not out_file.exists():
|
||||
return []
|
||||
|
||||
|
||||
Reference in New Issue
Block a user