First version of files

This commit is contained in:
Lab 8916100448256 2020-02-27 14:48:28 +01:00
parent fc1451d793
commit 42d3901dd5
5 changed files with 122 additions and 2 deletions

View file

@ -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)

View file

@ -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

View file

@ -0,0 +1,3 @@
DOMAIN="domain.tld"
RECORD="host"
APIKEY="Your-API-Key"

11
install.sh Normal file
View file

@ -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

View file

@ -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