Script to Update a Digital Ocean A Record
March 7, 2015
I've been using a subdomain to point to my home IP address for years now. It's great, but pretty annoying when my IP address changes randomly. I decided I wanted a bash script to keep my Digital Ocean A Record updated with my external IP.
I've setup a cronjob on my media server with the following code. Now it updates DigitalOcean hourly with my external IP without me having to worry about it.
#!/bin/bash
# Your domain ID
domain_id="yourdomain.com"
# The subdomain of your A Record
subdomain="yoursubdomain"
# The numeric record to update (you can find it by checking the HTML on the DNS edit page)
record_id="XXXXX"
# Your API Key
api_key="XXXXX"
##### All the work happens below here... don't change #####
ip="$(curl http://ipecho.net/plain)"
echo content="$(curl -H "Authorization: Bearer $api_key" -H "Content-Type: application/json" \
-d '{"name": "'"$subdomain"'", "data": "'"$ip"'"}' \
-X PUT "https://api.digitalocean.com/v2/domains/$domain_id/records/$record_id")"
Credit to this GitHub post.