2.8 KiB
mikrotik-netbox-dns-sync
Keeps the static DNS entries of a MikroTik RouterOS device in sync with NetBox: every IPv4 address in SUBNET that has a dns_name in NetBox becomes an A record in /ip/dns/static on the router.
Each run:
- reads the IP addresses under
SUBNETfrom the NetBox REST API (/api/ipam/ip-addresses/?parent=...); - reads the router's static DNS table over the RouterOS API (API-SSL, port 8729 by default);
- computes the plan (unchanged, add, update name/address/TTL, remove) for the records it manages;
- applies it and sorts the managed records by IP.
Only static A records whose address falls inside SUBNET are managed. Everything else in the table is left alone, unless MANAGE_ALL_STATIC=true. If NetBox returns no DNS records the run aborts without touching the router, so a NetBox outage or a wrong filter cannot wipe the table.
Python 3 standard library only, no dependencies.
Configuration
Everything comes from environment variables, see example.env.
| Variable | Required | Default | Description |
|---|---|---|---|
NETBOX_URL |
yes | NetBox base URL | |
NETBOX_TOKEN |
yes | NetBox API token; nbt_... v2 tokens use Bearer, legacy ones Token |
|
NETBOX_VERIFY_SSL |
no | true |
Verify the NetBox certificate |
SUBNET |
yes | IPv4 prefix to sync, e.g. 192.0.2.0/24 |
|
MIKROTIK_HOST |
yes | Router hostname or IP | |
MIKROTIK_PORT |
no | 8729 |
API-SSL port |
MIKROTIK_USER |
yes | RouterOS user | |
MIKROTIK_PASSWORD |
yes | RouterOS password | |
MIKROTIK_VERIFY_SSL |
no | false |
Verify the router certificate |
DNS_TTL |
no | 5m |
TTL of the managed records, RouterOS syntax |
MANAGE_ALL_STATIC |
no | false |
Manage (and remove) every static record, not only those in SUBNET |
DRY_RUN |
no | false |
Log the plan without changing the router |
TIMEOUT |
no | 15 |
Network timeout in seconds |
LOG_FILE |
no | Also log to this file | |
LOG_TO_CONSOLE |
no | true |
Log to stdout |
Booleans accept true/false, yes/no, on/off, 1/0.
Permissions
NetBox: a token with read access to ipam.ipaddress is enough.
RouterOS: a dedicated user restricted to the API:
/user group add name=netbox-dns-sync policy=api,read,write
/user add name=netbox-dns-sync group=netbox-dns-sync password=... address=<sync host>
/ip service set api-ssl disabled=no certificate=<cert> address=<sync host>
Usage
cp example.env .env
chmod 600 .env
$EDITOR .env
set -a; . ./.env; set +a
python3 netbox_dns_sync.py
example.env ships with DRY_RUN=true: check the plan in the output, then set it to false.
To run it periodically see deployments/mikrotik-netbox-dns-sync, which packages it on the cron image.