921891b27a
Three scripts that surface hosts on the fleet's networks that aren't
already tracked under servers/*/. Goal: spot servers that need management
coverage (inventory, backup, monitoring) without wandering the LAN by
hand.
discover-fortigate.sh SSH to a FortiGate admin, run
`execute dhcp lease-list all`, emit TSV
(IP, MAC, hostname, vdom, source).
SSH was picked over the REST API for now
because it needs no API-token plumbing. The
parser is defensive about FortiOS output
format drift (multiple VDOM sections,
optional hostname). API variant can replace
it when the extra robustness is worth the
token setup.
discover-unifi.sh Cookie-auth REST call against a UniFi
Controller. Tries /api/auth/login (UniFi OS
/ UDM / Cloud Key Gen2+) first; falls back
to legacy /api/login for self-hosted
controllers. Output is the same TSV shape
as the FortiGate script so the two mix.
Needs UNIFI_USER / UNIFI_PASS env and jq.
discover-gaps.sh Consumes one or more TSVs from the sources
above. Builds the set of managed IPs from
servers/*/ssh-target (plus a grep of README
files for documented IPs) and prints any
discovered IPs not in that set.
Exit code is 1 if gaps found — suitable for
cron alerting.
Common pipeline:
scripts/discover-fortigate.sh ana-fw.phasefinal.com > /tmp/ana.tsv
scripts/discover-fortigate.sh nh3-gw.phasefinal.com > /tmp/nh3.tsv
UNIFI_USER=admin UNIFI_PASS=… scripts/discover-unifi.sh esh-uc.esteban.net > /tmp/esh.tsv
scripts/discover-gaps.sh /tmp/ana.tsv /tmp/nh3.tsv /tmp/esh.tsv
First-time use probably needs:
- SSH access configured to each FortiGate (admin login, key preferred)
- UniFi user with read access (the built-in API read-only role works)
- `jq` installed on the runner (for UniFi script)
81 lines
2.3 KiB
Bash
Executable File
81 lines
2.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# discover-unifi.sh — pull client list from a UniFi Controller (UDM / UniFi OS).
|
|
#
|
|
# Uses the UniFi REST API:
|
|
# POST /api/login {username, password}
|
|
# GET /proxy/network/api/s/<site>/stat/sta
|
|
#
|
|
# UniFi OS cookie-auth variant (post-2021 devices / UDM / Cloud Key Gen2+).
|
|
# For legacy standalone controllers (self-hosted UniFi Network software on
|
|
# Linux), the endpoint is /api/login and /api/s/<site>/stat/sta directly
|
|
# (no /proxy/network prefix). Script tries UniFi OS first, falls back.
|
|
#
|
|
# Usage:
|
|
# UNIFI_USER=admin UNIFI_PASS=... scripts/discover-unifi.sh <controller-host>
|
|
#
|
|
# Optional env:
|
|
# UNIFI_SITE default: default
|
|
# UNIFI_PORT default: 443
|
|
#
|
|
# Output: TSV on stdout, one client per line:
|
|
# IP MAC HOSTNAME AP_ALIAS SOURCE
|
|
|
|
set -euo pipefail
|
|
|
|
if [ -z "${1:-}" ]; then
|
|
echo "usage: UNIFI_USER=admin UNIFI_PASS=… $(basename "$0") <controller-host>" >&2
|
|
exit 2
|
|
fi
|
|
|
|
CONTROLLER="$1"
|
|
PORT="${UNIFI_PORT:-443}"
|
|
SITE="${UNIFI_SITE:-default}"
|
|
BASE="https://${CONTROLLER}:${PORT}"
|
|
|
|
: "${UNIFI_USER:?UNIFI_USER env var required}"
|
|
: "${UNIFI_PASS:?UNIFI_PASS env var required (never put this in shell history — use a password-manager integration)}"
|
|
|
|
COOKIE_JAR=$(mktemp)
|
|
trap 'rm -f "$COOKIE_JAR"' EXIT
|
|
|
|
login() {
|
|
local path="$1"
|
|
curl -sk -c "$COOKIE_JAR" -X POST "$BASE$path" \
|
|
-H 'Content-Type: application/json' \
|
|
-d "{\"username\":\"$UNIFI_USER\",\"password\":\"$UNIFI_PASS\"}" \
|
|
-o /dev/null -w '%{http_code}'
|
|
}
|
|
|
|
# Try UniFi OS endpoint first, fall back to legacy
|
|
CODE=$(login /api/auth/login)
|
|
if [ "$CODE" = "200" ]; then
|
|
STATS_PATH="/proxy/network/api/s/${SITE}/stat/sta"
|
|
else
|
|
CODE=$(login /api/login)
|
|
if [ "$CODE" = "200" ]; then
|
|
STATS_PATH="/api/s/${SITE}/stat/sta"
|
|
else
|
|
echo "error: UniFi login failed (tried /api/auth/login and /api/login, got HTTP $CODE)" >&2
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
json=$(curl -sk -b "$COOKIE_JAR" "$BASE$STATS_PATH")
|
|
|
|
if ! command -v jq >/dev/null 2>&1; then
|
|
echo "error: jq not installed (apt install jq)" >&2
|
|
exit 2
|
|
fi
|
|
|
|
# Standardize to TSV. Each client record has: ip, mac, hostname, name (alias), ap_mac.
|
|
jq -r --arg src "unifi:${CONTROLLER}" '
|
|
.data[] |
|
|
[
|
|
(.ip // "-"),
|
|
(.mac // "-"),
|
|
(.hostname // .name // "-"),
|
|
(.ap_mac // "-"),
|
|
$src
|
|
] | @tsv
|
|
' <<<"$json"
|