Skip to main content

Dynamic DNS Using Gandi

·661 words·4 mins

Updating Gandi DNS Using the API and a Shell Script #

In this tutorial, we will show you how to update your Gandi DNS records using the Gandi API and a shell script. This approach is useful for those who have dynamic IP addresses and need to keep their DNS records up-to-date.

By using the Gandi API and a shell script, you can automate the process of updating your DNS records, ensuring that your website or application is always available at the correct IP address.

In this tutorial, we will walk you through the process of creating a shell script to update your DNS records, and scheduling the script to run automatically.

Read on to learn how to update your Gandi DNS records using the API and a shell script.

Further Reading If you’re interested in automating dynamic DNS updates with the Gandi API and Docker, you might find my follow-up article Automate Dynamic DNS Updates with Gandi API and Docker helpful. This article goes into more detail on how to set up the Docker container, including how to build the Docker image, run the container, and schedule tasks using cron. It also covers best practices for running Docker containers in production environments. Check out the article for more information and step-by-step instructions.

Dynamic DNS #

Dynamic DNS is a way to associate a changing Dynamic IP address (usually residential xDSL connections) to a static domain name (DNS record)

This allows you to connect to home.example.com -> DNS Lookup & Resolution -> 203.0.113.78. The DNS entry for home.example.com is updated automatically at set intervals or when an IP address change is detected.

Dynamic DNS Providers #

There are a number of Dynamic DNS providers that can be used, a well known provider is https://dyn.com/, but unfortunately some of these services come with a cost.

Gandi Live DNS #

https://www.gandi.net/en provides a Live DNS Service

LiveDNS is Gandi’s upcoming DNS platform, a completely new service that offers its own API and its own nameservers.

The new platform offers powerful features to manage DNS Zone templates that you can integrate into your own workflow. Features include bulk record management, association with multiple domains, versioning and rollback.

Implementation #

The below instructions will show you how to create a Dynamic DNS system using a single script and Gandi’s LiveDNS.

Prerequisites #

Make sure you have the following applications installed:

Bash Script #

Create a bash script and put it under “/usr/local/bin/dyndns_update.sh”. (This can of course be kept wherever you want)

Add the API key you got from the Gandi Account Panel.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/bash
# This script gets the external IP of your systems then connects to the Gandi
# LiveDNS API and updates your dns record with the IP.

# Gandi LiveDNS API KEY
API_KEY="............"

# Domain hosted with Gandi
DOMAIN="example.com"

# Subdomain to update DNS
SUBDOMAIN="dynamic"

# Get external IP address
EXT_IP=$(curl -s ifconfig.me)  

#Get the current Zone for the provided domain
CURRENT_ZONE_HREF=$(curl -s -H "X-Api-Key: $API_KEY" https://dns.api.gandi.net/api/v5/domains/$DOMAIN | jq -r '.zone_records_href')

# Update the A Record of the subdomain using PUT
curl -D- -X PUT -H "Content-Type: application/json" \
        -H "X-Api-Key: $API_KEY" \
        -d "{\"rrset_name\": \"$SUBDOMAIN\",
             \"rrset_type\": \"A\",
             \"rrset_ttl\": 1200,
             \"rrset_values\": [\"$EXT_IP\"]}" \
        $CURRENT_ZONE_HREF/$SUBDOMAIN/A

Run The Script #

I would set this script to run via crontab every 30 minutes. This ensures with an IP change the Dynamic DNS would only be out of date for a maximum of 30 minutes.

Edit crontab with the following command

[root@server ~]# crontab -e

Add the following lines to run the script every 30 minutes.

*/30 * * * * /bin/bash /usr/local/bin/dyndns_update.sh

Once the script runs it should update the dynamic.example.com dns entry with the external IP that was found by the script.