Skip to content

Instantly share code, notes, and snippets.

@nwesterhausen
Created September 30, 2020 23:31
Show Gist options
  • Save nwesterhausen/805df999d95806feab0c37bdf0f31009 to your computer and use it in GitHub Desktop.
Save nwesterhausen/805df999d95806feab0c37bdf0f31009 to your computer and use it in GitHub Desktop.
Dynamic DNS using Digital Ocean DNS
#!/bin/bash
# Short script using the digital ocean API to update an A record with an IPV4 address
# from a specificed device.
#
# I simply have cron run this every 30m. Ideally I wanted to have it run when openvpn
# had to reconnect and either (a) change device or (b) change IP.
#
# Pre-requisites:
# 1. DNS set up through Digital Ocean for this domain
# 2. API Token created with write access
# 3. Create a DNS record for this host ahead of time, you need the record ID to update
# a record.
DO_TOKEN=__API__WRITE__ACCESS__
DOMAIN_NAME=dyn.example.com #FQDN you are putting your host under
HOST_NAME=drop-ovpn #Host name for the record
RECORD_ID=1111 #ID of the host record
DEV=$(ip tuntap show | cut -d: -f1) #Change to ETH0/ETH1 etc. This was for a VPN adapter
IF_IP=$(ip address show $DEV | awk ' FNR==3 {print substr($2,0, length($2) - 3) }')
# Run this to get record_ids for your DOMAIN_NAME from above.
#curl -X GET "https://api.digitalocean.com/v2/domains/$DOMAIN_NAME/records" -H "Authorization: Bearer $DO_TOKEN"
curl -X PUT "https://api.digitalocean.com/v2/domains/$DOMAIN_NAME/records/$RECORD_ID" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $DO_TOKEN" \
-d "{\"type\":\"A\",\"name\":\"$HOST_NAME\",\"data\":\"$IF_IP\"}"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment