Skip to content

Instantly share code, notes, and snippets.

@julianxhokaxhiu
Last active August 28, 2020 13:03
Show Gist options
  • Save julianxhokaxhiu/c72a95f01a1985206e174c9079346fe6 to your computer and use it in GitHub Desktop.
Save julianxhokaxhiu/c72a95f01a1985206e174c9079346fe6 to your computer and use it in GitHub Desktop.
Push IP machine on boot over PowerDNS via REST API

To make it work:

  1. systemctl enable systemd-networkd-wait-online.service

  2. Copy update-dns-record.service into /etc/systemd/system and run chmod 0755 /etc/systemd/system/update-dns-record.service

  3. Copy update-dns-via-api.sh into /root/.systemd/ and run chmod 0755 /root/.systemd/update-dns-via-api.sh

  4. Finally adjust update-dns-via-api.sh wth the right APIKEY value

Notes: api.dns.lan should point to your PowerDNS IP

[Unit]
Description=Update DNS Domain entry with my current IP
Wants=network-online.target
After=network.target network-online.target
[Service]
ExecStart=/bin/bash /root/.systemd/update-dns-via-api.sh
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
#!/bin/bash
# Get needed vars
HOSTNAME="`hostname`"
IP="`ifconfig | grep '192.168' | cut -d: -f2 | awk '{print $2}'`"
APIKEY="apiKey"
# One time for "domain.lan"
curl -X PATCH \
--data '{"rrsets": [ {"name": "'"${HOSTNAME}"'.lan", "type": "A", "changetype": "REPLACE", "records": [ {"content": "'"${IP}"'", "disabled": false, "name": "'"${HOSTNAME}"'.LAN", "ttl": 60, "type": "A" } ] } ] }' \
-H 'X-API-Key: '"${APIKEY}"'' \
http://api.dns.lan:8080/servers/localhost/zones/lan
# And one time for "*.domain.lan"
curl -X PATCH \
--data '{"rrsets": [ {"name": "*.'"${HOSTNAME}"'.lan", "type": "A", "changetype": "REPLACE", "records": [ {"content": "'"${IP}"'", "disabled": false, "name": "*.'"${HOSTNAME}"'.LAN", "ttl": 60, "type": "A" } ] } ] }' \
-H 'X-API-Key: '"${APIKEY}"'' \
http://api.dns.lan:8080/servers/localhost/zones/lan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment