Using the Digital Ocean API for Dynamic DNS

Digital Ocean’s v2 API has some great features, and it’s super easy to use.

Some background information on why I decided to post this: At home I have some IP cameras and servers that I need to access remotely on a daily basis, and as my home internet connection is delivered using a dynamic IP; I knew I had to either purchase a static IP from my provider or use a ‘Dynamic DNS’ service.

This is where Digital Ocean’s API comes in: The API gives you the ability to dynamically update a given sub-domain’s A record with your home Internet connection’s IP address. This means I have a free way to always ensure I have a static sub-domain and that I won’t have to pay extra in order to have a static IP address from my Internet service provider.

The easiest way to get this up-and-running is to run the API commands as a Cron job. In my case I’ve setup a Cron job which is run every hour.

In the steps below I have provided some example code snippets and Bash script. This has been tested this on Ubuntu 16.04 LTS 64-Bit and no issues have have been seen.

  1. Ensure you have a domain already setup within Digital Ocean and your name servers match what Digital Ocean has supplied. More info about this can be found on their community tutorials here.
  2. Setup the sub-domain you wish to act as your home Internet address (such as homenetwork.exampledomain.co.uk) within the ‘Networking’ area of your Digital Ocean control panel.
  3. Generate a new API token within the ‘API’ section of the control panel: click ‘API’ then click the ‘Generate New Token’ button. Give your token a name (this can be anything) and ensure Read and Write scopes are selected. Make a note of your token key, this will be used next.
  4. You will now need to find the record ID of the sub-domain which you have just created. You can do this by running this code in your terminal, replace EXAMPLEDOMAIN.CO.UK with your domain, not the sub-domain:
    curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer YOURAPIKEYHERE" "https://api.digitalocean.com/v2/domains/EXAMPLEDOMAIN.CO.UK/records"

    If you’d like the output to be easier to read use this command:

    curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer YOURAPIKEYHERE" "https://api.digitalocean.com/v2/domains/EXAMPLEDOMAIN.CO.UK/records" | python -m json.tool
  5. Find the sub-domain and it’s ID, the result should look similar to this:
    '{
    "data": "123.456.0.789",
    "id": 19247136,
    "name": "homenetwork",
    "port": null,
    "priority": null,
    "type": "A",
    "weight": null
    },'

    Make a note of the ID as it will be used later.

  6. Now create a file named ‘public-ip-update.sh’ somewhere on your server to hold the bash script.Enter this code into the file:
    #!/bin/bash
    #Author: Ryan Fitton (ryanfitton.co.uk)
    # Version 2.0
    # 12/11/2016
    
    #Service to get current public IP
    CURRENT_IP="`curl -s http://whatismyip.akamai.com/`" #Get the public IP address of this server
    
    #Digital Ocean API configuration
    #This will tell Digital Ocean to update the DNS record for homenetwork.exampledomain.co.uk
    #API Token
    API_TOKEN=YOURAPIKEYHERE
    
    #Domain and Record ID
    DOMAIN_NAME=EXAMPLEDOMAIN.CO.UK
    DOMAIN_RECORD_ID=YOURDOMAINRECORDID
    DOMAIN_RECORD_VALUE=${CURRENT_IP}
    
    #-----------------No need to edit past this point-----------------
    #Update Digital Ocean API
    #Update record
    curl -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer ${API_TOKEN}" -d '{"data":"'"${DOMAIN_RECORD_VALUE}"'"}' "https://api.digitalocean.com/v2/domains/${DOMAIN_NAME}/records/${DOMAIN_RECORD_ID}"
    
    #Debugging
    #View domains
    #curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer ${API_TOKEN}" "https://api.digitalocean.com/v2/domains"
    
    #View domain records
    #curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer ${API_TOKEN}" "https://api.digitalocean.com/v2/domains/${DOMAIN_NAME}/records"
    
    #Page 2
    #curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer ${API_TOKEN}" "https://api.digitalocean.com/v2/domains/${DOMAIN_NAME}/records?page=2"
  7. Fill in your details where these are mentioned:
    • YOURAPIKEYHERE
    • EXAMPLEDOMAIN.CO.UK
    • YOURDOMAINRECORDID
  8. Save this file and exit.
  9. Now setup a Cron job using crontab, enter this command: ‘crontab –e’
    And use this code to run the public-ip-update.sh every hour:

    0 * * * * /PATHTOYOURFILE/public-ip-update.sh

    You can push through an update immediately to test by running:

    'bash /PATHTOYOURFILE/public-ip-update.sh'

    If it runs successfully you will see an output similar to this:

    '{"domain_record":{"id":12345678,"type":"A","name":"homenetwork","data":"123.456.0.789","priority":null,"port":null,"weight":null}}'

You should now be able to view any IP address changes in the ‘Networking’ area within the Digital Ocean control panel. Once you have confirmed the API is working, you’ll now be able to to connect to your home network using the sub-domain you have assigned, although remember to open any ports on your router in order to access certain parts of your network.

The code snipptets and Bash script within this tutorial are free for you to use in your own projects. If you come up with any suggestions or improvements, please contact me or post a comment below.

There are no comments, be the first. Use the form below to add your comment

Post a Comment