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

Visit site

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
Was this helpful?
 
2 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

This would be perfect for servers if it took the variables as inputs and had some error handling…

Sweet! You just saved me some time! Thanks!

Publish your Tool on Community

Have you created an Integration, API Wrapper, Service, or other Tool that helps developers build on DigitalOcean? Help users find it by listing it in Community Tools.

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel