Add netscan push endpoint: PVE1 nmap scan every 3min populates network devices

This commit is contained in:
2026-05-30 03:20:06 +00:00
parent 2faeb5498a
commit 62c7878615
3 changed files with 71 additions and 55 deletions
+3 -54
View File
@@ -312,60 +312,9 @@ function collect_all(): array {
}
// ── Network Device Scan (nmap via PVE1) ───────────────────────────────
try {
$nmapRaw = shell_exec(
"sshpass -p 'Joker1974!!!' ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 " .
"root@10.48.200.90 'nmap -sn --send-ip 10.48.200.0/24 2>/dev/null' 2>/dev/null"
);
if ($nmapRaw) {
$discovered = [];
$cur = null;
foreach (explode("\n", $nmapRaw) as $line) {
$line = trim($line);
if (preg_match('/Nmap scan report for (?:(\S+) \()?(\d+\.\d+\.\d+\.\d+)\)?/', $line, $m)) {
if ($cur) $discovered[] = $cur;
$cur = ['hostname' => ($m[1] && $m[1] !== $m[2]) ? $m[1] : null, 'ip' => $m[2], 'mac' => null, 'vendor' => null];
} elseif ($cur && preg_match('/MAC Address: ([0-9A-Fa-f:]{17}) \(([^)]+)\)/i', $line, $m)) {
$cur['mac'] = strtolower($m[1]);
$cur['vendor'] = $m[2] !== 'Unknown' ? $m[2] : null;
}
}
if ($cur) $discovered[] = $cur;
$discoveredIPs = [];
foreach ($discovered as $d) {
$discoveredIPs[] = $d['ip'];
JarvisDB::execute(
'INSERT INTO network_devices (ip, mac, hostname, status, last_seen)
VALUES (?,?,?,"online",NOW())
ON DUPLICATE KEY UPDATE mac=VALUES(mac), hostname=COALESCE(VALUES(hostname),hostname),
status="online", last_seen=NOW()',
[$d['ip'], $d['mac'], $d['hostname'] ?? $d['vendor']]
);
if ($d['vendor']) {
JarvisDB::execute(
'UPDATE network_devices SET device_type=? WHERE ip=? AND (device_type IS NULL OR device_type="")',
[$d['vendor'], $d['ip']]
);
}
}
if (!empty($discoveredIPs)) {
$ph = implode(',', array_fill(0, count($discoveredIPs), '?'));
JarvisDB::execute(
"UPDATE network_devices SET status='offline'
WHERE ip NOT IN ($ph) AND last_seen < DATE_SUB(NOW(), INTERVAL 10 MINUTE)",
$discoveredIPs
);
}
$results['nmap_scan'] = 'ok (' . count($discovered) . ' devices found)';
} else {
$results['nmap_scan'] = 'skipped (PVE1 unreachable)';
}
} catch (Exception $e) {
$results['nmap_scan'] = 'error: ' . $e->getMessage();
}
// Network device scan is handled by PVE1 cron (/usr/local/bin/jarvis-netscan.sh)
// which POSTs nmap results to /api/netscan every 3 minutes.
$results['nmap_scan'] = 'handled by PVE1 push (jarvis-netscan.sh)';
return $results;
}