Skip to content

Add a local-only UniFi client and device export script - #14

Merged
ShaneCurcuru merged 1 commit into
mainfrom
claude/project-thread-v60p25
Nov 20, 2026
Merged

ShaneCurcuru merged 1 commit into
mainfrom
claude/project-thread-v60p25

Conversation

@ShaneCurcuru

Copy link
Copy Markdown
Contributor

Requested by Shane · project thread

What

scripts/unifi_export.py: a standalone script (not Domus app code) that exports every client and UniFi network device known to a local UniFi OS console, such as a UDM Pro, as CSV or JSON, with every field the API returns.

Source of the ask: Shane's message in the project thread. It may later feed an import of selected network devices into Domus, but nothing here touches src/domus.

Reading of the ask

  • Local only. The linked Site Manager API is the cloud API (api.ui.com), so I used the console's local Network API instead (/api/auth/login, then /proxy/network/api/self/sites, s/<site>/stat/sta, rest/user, stat/device), which returns far more fields than the cloud API and needs only a local username and password.
  • Where it lives. scripts/, so ruff, pyright strict, and pytest already cover it. Happy to move it to a separate tools/ directory if you'd rather keep scripts/ for dev/CI helpers.
  • Default output is clients plus UniFi devices (--kind all); --include-offline adds remembered clients that are not connected now.

Local-only and security safeguards

  • --host must be an IP literal in 10/8, 172.16/12, 192.168/16, 169.254/16, fc00::/7, or fe80::/10. Hostnames are refused so no DNS query is ever made; public, loopback, CGNAT, and documentation ranges are refused (ipaddress.is_private alone was too loose).
  • Uses http.client directly: no proxy env vars, no redirects followed, no other host contacted, no telemetry.
  • Sockets are opened with a hop limit (--max-hops, default 1), so a console behind another router will not answer; our own source address must also be local.
  • TLS: --fingerprint pins the console's self-signed cert (SHA-256, constant-time compare); --show-fingerprint prints it without sending credentials; --ca-file (+ --tls-name) or an explicit --insecure are the alternatives. TLS 1.2 minimum.
  • Credentials: --username/--password, or ~/unifi.txt (key = value: host, username, password, fingerprint), else a getpass prompt. Warns if the file is group/world readable or owned by another user. Passwords never appear in output, errors, or repr; bad credential lines are reported by number, not echoed.
  • Secret fields (x_authkey, x_vwirekey, x_aes_gcm, passphrases, PSKs, tokens) are [REDACTED] unless --no-redact.
  • CSV cells starting with = + - @ are prefixed with ' (client hostnames are attacker-controlled). Output files are created mode 0600. Responses over 64 MB are refused.
  • Read-only: the only non-GET calls are login and logout.

Dependencies

None added: standard library only.

Tests

tests/test_unifi_export.py, 63 tests with a fake transport, fake sockets, and fake TLS; no network. ruff check, ruff format --check, pyright (strict), and the full pytest suite pass (536 passed, 1 skipped as before).

I also ran the real HTTPSTransport end to end against a throwaway local HTTPS server with a self-signed cert (outside the test suite): pinned fingerprint succeeds, a wrong fingerprint and an unpinned self-signed cert both fail with the guidance messages.

Not done / not verified

  • Not run against a real UDM Pro. Field names and endpoints follow the documented classic local API; please try --show-fingerprint then a CSV export on your network.
  • The official Network Integration API (/proxy/network/integration/v1, API-key auth) is not used: it needs an API key rather than username/password and returns fewer fields. It could be added as an option later.
  • UniFi cloud (SSO) accounts with 2FA cannot log in locally; a local console account (View Only role is enough) is needed.

Posted by Claude on behalf of @ShaneCurcuru


Generated by Claude Code

scripts/unifi_export.py logs in to a UniFi OS console (such as a UDM Pro)
with a local account and writes every client and UniFi device the Network
API returns as CSV or JSON. Standard library only; it talks only to the
console's private or link-local IP address, never resolves names, ignores
proxy settings, sets a hop limit, and pins the self-signed certificate by
fingerprint. Secret fields are redacted and CSV cells are guarded against
formula injection.

Posted by Claude on behalf of @ShaneCurcuru

Generated-by: Claude Code
@ShaneCurcuru ShaneCurcuru self-assigned this Nov 20, 2026
@ShaneCurcuru
ShaneCurcuru marked this pull request as ready for review November 20, 2026 23:15
@ShaneCurcuru
ShaneCurcuru merged commit a91db8f into main Nov 20, 2026
7 checks passed
@ShaneCurcuru
ShaneCurcuru deleted the claude/project-thread-v60p25 branch November 20, 2026 23:15
@ShaneCurcuru

Copy link
Copy Markdown
Contributor Author

Sufficient for basic purposes; returns a LOT of columns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants