From 42d3901dd50f2d7eaaba8e4dcc80fcf5499cd8ad Mon Sep 17 00:00:00 2001 From: Lab 8916100448256 Date: Thu, 27 Feb 2020 14:48:28 +0100 Subject: [PATCH] First version of files --- README.md | 33 ++++++++++++++- etc/cron.d/live-dns-update | 4 ++ etc/live-dns/live-dns.conf | 3 ++ install.sh | 11 +++++ usr/local/bin/live-dns-update.sh | 73 ++++++++++++++++++++++++++++++++ 5 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 etc/cron.d/live-dns-update create mode 100644 etc/live-dns/live-dns.conf create mode 100644 install.sh create mode 100755 usr/local/bin/live-dns-update.sh diff --git a/README.md b/README.md index 5406e60..bede6dd 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,32 @@ -# gandi-dynamic-dns +# Gandi Dynamic DNS + +Cron job to auto update a DNS record when WAN IP changes using Gandi Live DNS API on Debian GNU/Linux + +## To install + +``` +git clone https://gitlab.com/lab.8916100448256/gandi-dynamic-dns.git ./gandi-dynamic-dns +cd ./gandi-dynamic-dns +sudo bash ./install.sh +``` + +Then configure DNS record info and API key in file /etc/live-dns/live-dns.conf + + +## How it works +### /etc/cron.d/live-dns-update +Cron job definition to check for WAN IP change and call Live DNS API to update the DNS record if necessary. By default runs the check/update script 2 times per hour, at every hour past 12 and 42 minutes. + +### /etc/live-dns/live-dns.conf +Configuration file for the check/update script +Needs to contains 3 environment variable definitions ++ DOMAIN="domain.tld" : Replace domain.tld with your domain name ++ RECORD="host" : Replace host with the sub domain you want to update when WAN IP changes ++ APIKEY="Your-API-Key" : Replace with you API key generated on the Gandi web interface. + + +### /usr/local/bin/live-dns-update.sh +This the script that is called by the cron job. + +This script is based on the sample provided on Gandi GitHub : [mywanip.sh](https://github.com/Gandi/api-examples/blob/master/bash/livedns/mywanip.sh) -Cron job to auto update a DNS record when WAN IP changes using Gandi Live DNS API on Debian GNU/Linux \ No newline at end of file diff --git a/etc/cron.d/live-dns-update b/etc/cron.d/live-dns-update new file mode 100644 index 0000000..f8791a4 --- /dev/null +++ b/etc/cron.d/live-dns-update @@ -0,0 +1,4 @@ +# /etc/cron.d/live-dns-update crontab fragment for updating +# DNS zone records with WAN IP address + +12,42 * * * * root bash /usr/local/bin/live-dns-update.sh diff --git a/etc/live-dns/live-dns.conf b/etc/live-dns/live-dns.conf new file mode 100644 index 0000000..835d8a9 --- /dev/null +++ b/etc/live-dns/live-dns.conf @@ -0,0 +1,3 @@ +DOMAIN="domain.tld" +RECORD="host" +APIKEY="Your-API-Key" diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..6ea7d6e --- /dev/null +++ b/install.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +cp usr/local/bin/live-dns-update.sh /usr/local/bin/live-dns-update.sh +chmod +x /usr/local/bin/live-dns-update.sh + +mkdir /etc/live-dns +cp etc/live-dns/live-dns.conf /etc/live-dns/live-dns.conf +chmod 400 /etc/live-dns/live-dns.conf + +cp etc/cron.d/live-dns-update /etc/cron.d/live-dns-update + diff --git a/usr/local/bin/live-dns-update.sh b/usr/local/bin/live-dns-update.sh new file mode 100755 index 0000000..45c5ccd --- /dev/null +++ b/usr/local/bin/live-dns-update.sh @@ -0,0 +1,73 @@ +#!/bin/bash +# +# Updates DNS zone records using Gandi's LiveDNS API. +# Before you use this script you need to configure record details and API key in +# the file `/etc/live-dns/live-dns.conf` +# +# This script is meant to be run by cron. +# through a crontab placed in /etc/cron.d/live-dns-update +# +# This script is based on the sample provided here : +# https://github.com/Gandi/api-examples/blob/master/bash/livedns/mywanip.sh +# + +# Include configuration file +source /etc/live-dns/live-dns.conf +#echo Domain : $DOMAIN +#echo Record : $RECORD +#echo APIKey : $APIKEY + +API="https://dns.api.gandi.net/api/v5/" +IP_SERVICE="http://me.gandi.net" + + +# Get WAN IP as seend from outside +WAN_IP4=$(curl -s4 $IP_SERVICE) +WAN_IP6=$(curl -s6 $IP_SERVICE) +echo WAN IPv4 : $WAN_IP4 | logger +echo WAN IPv6 : $WAN_IP6 | logger + +if [[ -z "$WAN_IP4" && -z "$WAN_IP6" ]]; then + echo "Something went wrong. Can not get your IP from $IP_SERVICE " | logger + exit 1 +fi + +# Get IP from DNS +DNS_IP4=$(dig +short @ns1.gandi.net -t A $RECORD.$DOMAIN) +DNS_IP6=$(dig +short @ns1.gandi.net -t AAAA $RECORD.$DOMAIN) + +echo DNS IPv4 : $DNS_IP4 | logger +echo DNS IPv6 : $DNS_IP6 | logger + +# Update IPV4 DNS record if IP adresses mismatch +if [[ ! -z "$WAN_IP4" ]]; then + if [[ "$WAN_IP4" != "$DNS_IP4" ]] + then + echo Updating IPv4 DNS records for $RECORD.$DOMAIN | logger + DATA='{"rrset_values": ["'$WAN_IP4'"]}' + IP4_RES=$(curl -s -XPUT -d "$DATA" \ + -H"X-Api-Key: $APIKEY" \ + -H"Content-Type: application/json" \ + "$API/domains/$DOMAIN/records/$RECORD/A" 2>&1) + echo Update of IPv4 DNS record : $IP4_RES | logger + else + echo No need to update IPv4 DNS record for $RECORD.$DOMAIN | logger + fi +fi + + +# Update IPV6 DNS record if IP adresses mismatch +if [[ ! -z "$WAN_IP6" ]]; then + if [[ "$WAN_IP6" != "$DNS_IP6" ]] + then + echo Updating IPv6 DNS records for $RECORD.$DOMAIN | logger + DATA='{"rrset_values": ["'$WAN_IP6'"]}' + IP6_RES=$(curl -s -XPUT -d "$DATA" \ + -H"X-Api-Key: $APIKEY" \ + -H"Content-Type: application/json" \ + "$API/domains/$DOMAIN/records/$RECORD/AAAA" 2>&1) + echo Update of IPv6 DNS record : $IP6_RES | logger + else + echo No need to update IPv6 DNS record for $RECORD.$DOMAIN | logger + fi +fi