DigitalOcean dynamic DNS update script in bash (noip/dyndns alternative)

A bash script that retrieves your public IP address and updates a domain record (like a subdomain) using DigitalOcean API v2.

This script is an alternative for dyndns and noip tools, with no dependencies, just a bash script that is less than 20 lines.

How it works:

  • The script retrieves your public IP address
  • checks if it has changed (using a log file)
  • updates a record on DigitalOcean using API v2
  • and writes the date and that new IP in the log file

Use it with a cron job for example and you are done.

I have written a detailed step by step tutorial on how to configure and use the script, with a section on how to open ports on your router too, but if you know what you are doing here is the script:

#!/bin/bash

#################### CHANGE THE FOLLOWING VARIABLES ####################
TOKEN="abcdef1234567890"
DOMAIN="yourdomain.info"
RECORD_ID="111222333"
LOG_FILE="/home/youruser/ips.txt"
########################################################################

CURRENT_IPV4="$(dig +short myip.opendns.com @resolver1.opendns.com)"
LAST_IPV4="$(tail -1 $LOG_FILE | awk -F, '{print $2}')"

if [ "$CURRENT_IPV4" = "$LAST_IPV4" ]; then
    echo "IP has not changed ($CURRENT_IPV4)"
else
    echo "IP has changed: $CURRENT_IPV4"
    echo "$(date),$CURRENT_IPV4" >> "$LOG_FILE"
    curl -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d '{"data":"'"$CURRENT_IPV4"'"}' "https://api.digitalocean.com/v2/domains/$DOMAIN/records/$RECORD_ID"
fi
You rated this helpful.
You reported this tool.
Was this helpful?
Report an issue

0 Comments