Add scan concurrency and login delay sliders
Serialised logins now sleep `login_delay` seconds between each SSH auth to prevent AD/LDAP lockout. Both max sessions (1-10) and login delay (0-15s) are configurable via UI sliders in the header and passed as JSON to all scan endpoints. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -34,10 +34,12 @@ def _reschedule(interval_minutes):
|
||||
logger.info(f"Auto-scan scheduled every {interval_minutes} minutes")
|
||||
|
||||
|
||||
def _trigger_scan_background(dept: str = None):
|
||||
def _trigger_scan_background(dept: str = None, workers: int = 5, login_delay: int = 3):
|
||||
"""Run scan in a background thread. dept=None → all switches."""
|
||||
if not scanner.scan_state["running"]:
|
||||
t = threading.Thread(target=scanner.run_scan, kwargs={"dept": dept}, daemon=True)
|
||||
t = threading.Thread(target=scanner.run_scan,
|
||||
kwargs={"dept": dept, "workers": workers, "login_delay": login_delay},
|
||||
daemon=True)
|
||||
t.start()
|
||||
|
||||
|
||||
@@ -55,12 +57,20 @@ def index():
|
||||
return render_template("index.html")
|
||||
|
||||
|
||||
def _scan_params():
|
||||
body = request.get_json(silent=True) or {}
|
||||
workers = max(1, min(10, int(body.get("workers", 5))))
|
||||
login_delay = max(0, min(30, int(body.get("login_delay", 3))))
|
||||
return workers, login_delay
|
||||
|
||||
|
||||
@app.route("/api/scan", methods=["POST"])
|
||||
def api_scan():
|
||||
"""Scan all active switches (no dept filter)."""
|
||||
if scanner.scan_state["running"]:
|
||||
return jsonify({"error": "Scan already running"}), 409
|
||||
_trigger_scan_background(dept=None)
|
||||
workers, login_delay = _scan_params()
|
||||
_trigger_scan_background(dept=None, workers=workers, login_delay=login_delay)
|
||||
return jsonify({"status": "started", "dept": None})
|
||||
|
||||
|
||||
@@ -74,7 +84,8 @@ def api_scan_clear():
|
||||
conn.execute("DELETE FROM links")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
_trigger_scan_background(dept=None)
|
||||
workers, login_delay = _scan_params()
|
||||
_trigger_scan_background(dept=None, workers=workers, login_delay=login_delay)
|
||||
return jsonify({"status": "started", "cleared": True})
|
||||
|
||||
|
||||
@@ -83,7 +94,8 @@ def api_scan_elec():
|
||||
"""Scan only ELEC department switches."""
|
||||
if scanner.scan_state["running"]:
|
||||
return jsonify({"error": "Scan already running"}), 409
|
||||
_trigger_scan_background(dept="ELEC")
|
||||
workers, login_delay = _scan_params()
|
||||
_trigger_scan_background(dept="ELEC", workers=workers, login_delay=login_delay)
|
||||
return jsonify({"status": "started", "dept": "ELEC"})
|
||||
|
||||
|
||||
@@ -92,7 +104,8 @@ def api_scan_gw():
|
||||
"""Scan only GW department switches."""
|
||||
if scanner.scan_state["running"]:
|
||||
return jsonify({"error": "Scan already running"}), 409
|
||||
_trigger_scan_background(dept="GW")
|
||||
workers, login_delay = _scan_params()
|
||||
_trigger_scan_background(dept="GW", workers=workers, login_delay=login_delay)
|
||||
return jsonify({"status": "started", "dept": "GW"})
|
||||
|
||||
|
||||
|
||||
+65
-2
@@ -151,6 +151,44 @@ var Xr=function(e){if(!(this instanceof Xr))return new Xr(e);this.id="Thenable/1
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.scan-settings-wrap {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
background: var(--surface2);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 8px;
|
||||
padding: 6px 12px;
|
||||
font-size: 12px;
|
||||
color: var(--text-dim);
|
||||
}
|
||||
.scan-settings-wrap input[type=range] {
|
||||
-webkit-appearance: none;
|
||||
width: 70px;
|
||||
height: 4px;
|
||||
background: var(--border);
|
||||
border-radius: 2px;
|
||||
outline: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
.scan-settings-wrap input[type=range]::-webkit-slider-thumb {
|
||||
-webkit-appearance: none;
|
||||
width: 13px; height: 13px;
|
||||
border-radius: 50%;
|
||||
background: var(--accent);
|
||||
cursor: pointer;
|
||||
}
|
||||
.scan-settings-wrap .setting-val {
|
||||
min-width: 22px;
|
||||
color: var(--text);
|
||||
font-weight: 600;
|
||||
}
|
||||
.scan-settings-sep {
|
||||
width: 1px; height: 16px;
|
||||
background: var(--border);
|
||||
margin: 0 4px;
|
||||
}
|
||||
|
||||
.btn {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
@@ -424,6 +462,16 @@ var Xr=function(e){if(!(this instanceof Xr))return new Xr(e);this.id="Thenable/1
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="scan-settings-wrap">
|
||||
<span>Sessions</span>
|
||||
<input type="range" id="workersSlider" min="1" max="10" value="5" oninput="document.getElementById('workersVal').textContent=this.value">
|
||||
<span class="setting-val" id="workersVal">5</span>
|
||||
<div class="scan-settings-sep"></div>
|
||||
<span>Login delay</span>
|
||||
<input type="range" id="delaySlider" min="0" max="15" value="3" oninput="document.getElementById('delayVal').textContent=this.value+'s'">
|
||||
<span class="setting-val" id="delayVal">3s</span>
|
||||
</div>
|
||||
|
||||
<button class="btn btn-primary" id="scanBtn" onclick="triggerScan()">
|
||||
<svg width="14" height="14" fill="none" stroke="currentColor" stroke-width="2.5" viewBox="0 0 24 24">
|
||||
<path d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"/>
|
||||
@@ -810,16 +858,31 @@ async function clearAndRescan() {
|
||||
if (!confirm('This will delete all switch and link data, then run a full scan. Continue?')) return;
|
||||
const btn = document.getElementById('clearScanBtn');
|
||||
btn.disabled = true;
|
||||
const res = await fetch('/api/scan/clear', { method: 'POST' });
|
||||
const res = await fetch('/api/scan/clear', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(scanSettings()),
|
||||
});
|
||||
if (res.status === 409) { alert('Scan already running'); btn.disabled = false; return; }
|
||||
startPolling();
|
||||
}
|
||||
|
||||
function scanSettings() {
|
||||
return {
|
||||
workers: parseInt(document.getElementById('workersSlider').value),
|
||||
login_delay: parseInt(document.getElementById('delaySlider').value),
|
||||
};
|
||||
}
|
||||
|
||||
async function triggerScan(dept) {
|
||||
const url = dept === 'elec' ? '/api/scan/elec'
|
||||
: dept === 'gw' ? '/api/scan/gw'
|
||||
: '/api/scan';
|
||||
const res = await fetch(url, { method: 'POST' });
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(scanSettings()),
|
||||
});
|
||||
if (res.status === 409) { alert('Scan already running'); return; }
|
||||
startPolling();
|
||||
}
|
||||
|
||||
+2
-2
@@ -26,7 +26,7 @@ scan_state = {
|
||||
}
|
||||
|
||||
|
||||
def run_scan(dept: str = None):
|
||||
def run_scan(dept: str = None, workers: int = 5, login_delay: int = 3):
|
||||
"""
|
||||
Full scan: fetch switch IPs from NocoDB → SSH all → parse → store → export.
|
||||
dept: None = all active switches, "ELEC" or "GW" = dept-filtered.
|
||||
@@ -105,7 +105,7 @@ def run_scan(dept: str = None):
|
||||
"error": result.get("error", "Unknown error")
|
||||
})
|
||||
|
||||
scan_all_switches(switches, progress_callback=on_progress)
|
||||
scan_all_switches(switches, progress_callback=on_progress, max_workers=workers, login_delay=login_delay)
|
||||
|
||||
try:
|
||||
merge_duplicate_switches()
|
||||
|
||||
+9
-4
@@ -2,7 +2,9 @@
|
||||
import re
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from functools import partial
|
||||
from netmiko import ConnectHandler, NetmikoTimeoutException, NetmikoAuthenticationException
|
||||
from config import SSH_USERNAME, SSH_PASSWORD, SSH_PORT, SSH_TIMEOUT, DEVICE_TYPE
|
||||
from parser import (parse_lldp_neighbors, parse_mgmt_ip_from_interfaces,
|
||||
@@ -15,7 +17,7 @@ logger = logging.getLogger(__name__)
|
||||
_login_lock = threading.Semaphore(1)
|
||||
|
||||
|
||||
def connect_and_query(ip):
|
||||
def connect_and_query(ip, login_delay=3):
|
||||
device = {
|
||||
"device_type": DEVICE_TYPE,
|
||||
"host": ip,
|
||||
@@ -43,6 +45,8 @@ def connect_and_query(ip):
|
||||
with _login_lock:
|
||||
conn = ConnectHandler(**device)
|
||||
_pt.Transport._preferred_keys = _orig_preferred_keys
|
||||
if login_delay > 0:
|
||||
time.sleep(login_delay)
|
||||
|
||||
try:
|
||||
hostname = conn.find_prompt().replace('#', '').replace('>', '').strip()
|
||||
@@ -175,13 +179,14 @@ def _aruba_firmware(version_output):
|
||||
|
||||
# ── Scan orchestration ────────────────────────────────────────────────────────
|
||||
|
||||
def scan_all_switches(ip_list, progress_callback=None, max_workers=5):
|
||||
def scan_all_switches(ip_list, progress_callback=None, max_workers=5, login_delay=3):
|
||||
results = []
|
||||
total = len(ip_list)
|
||||
done = 0
|
||||
|
||||
_scan = partial(connect_and_query, login_delay=login_delay)
|
||||
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
||||
future_to_ip = {executor.submit(connect_and_query, ip): ip for ip in ip_list}
|
||||
future_to_ip = {executor.submit(_scan, ip): ip for ip in ip_list}
|
||||
|
||||
for future in as_completed(future_to_ip):
|
||||
ip = future_to_ip[future]
|
||||
@@ -189,7 +194,7 @@ def scan_all_switches(ip_list, progress_callback=None, max_workers=5):
|
||||
result = future.result()
|
||||
except Exception as e:
|
||||
result = {"success": False, "ip": ip, "error": str(e)}
|
||||
import time; time.sleep(2) # avoid RADIUS lockout
|
||||
time.sleep(2) # avoid RADIUS lockout between result processing
|
||||
|
||||
results.append(result)
|
||||
done += 1
|
||||
|
||||
Reference in New Issue
Block a user