#!/usr/bin/env bash set -euo pipefail CIDR="${1:-}" have() { command -v "$1" >/dev/null 2>&1; } if ! have nmap; then echo "FEHLER: nmap fehlt. Install: brew install nmap" exit 1 fi ip="" if have ipconfig; then ip="$(ipconfig getifaddr en0 2>/dev/null || true)" if [ -z "$ip" ]; then ip="$(ipconfig getifaddr en1 2>/dev/null || true)"; fi fi if [ -z "$CIDR" ]; then if [ -n "$ip" ]; then base="$(echo "$ip" | awk -F. '{print $1"."$2"."$3".0"}')" CIDR="${base}/24" else CIDR="192.168.1.0/24" fi fi OUTDIR="$(cd "$(dirname "$0")" && pwd)/lan-scan-$(date +%F_%H-%M-%S)" mkdir -p "$OUTDIR" XML="$OUTDIR/scan.xml" JSON="$OUTDIR/scan.json" CSV="$OUTDIR/scan.csv" echo "Scan läuft: $CIDR" nmap -sn -n "$CIDR" -oX "$XML" >/dev/null python3 - <<'PY' "$XML" "$CIDR" "$JSON" "$CSV" import sys, json, xml.etree.ElementTree as ET, datetime, csv xml_path, cidr, json_path, csv_path = sys.argv[1:5] root = ET.parse(xml_path).getroot() hosts = [] for h in root.findall("host"): st = h.find("status") if st is None or st.get("state") != "up": continue ip = "" mac = "" vendor = "" for a in h.findall("address"): if a.get("addrtype") == "ipv4": ip = a.get("addr","") if a.get("addrtype") == "mac": mac = a.get("addr","") vendor = a.get("vendor","") name = "" hn = h.find("hostnames") if hn is not None: first = hn.find("hostname") if first is not None: name = first.get("name","") hosts.append({"ip": ip, "name": name, "mac": mac, "vendor": vendor}) payload = { "ok": True, "cidr": cidr, "scanned_at": datetime.datetime.now().isoformat(timespec="seconds"), "count": len(hosts), "hosts": hosts } with open(json_path, "w", encoding="utf-8") as f: json.dump(payload, f, ensure_ascii=False, indent=2) with open(csv_path, "w", encoding="utf-8", newline="") as f: w = csv.writer(f, delimiter=";") w.writerow(["IP","Hostname","MAC","Vendor"]) for x in hosts: w.writerow([x["ip"], x["name"], x["mac"], x["vendor"]]) print("Fertig:") print(" ", json_path) print(" ", csv_path) PY echo "Done."