-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgandi-dynamic-dns.sh
executable file
·97 lines (86 loc) · 2.43 KB
/
gandi-dynamic-dns.sh
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
#
# Automatically update a Gandi DNS A record to your current public IP address using Gandi's LiveDNS API
#
# Usage: ./gandi-dynamic-dns.sh www.example.com
#
# CHECK: curl is installed
if [ ! command -v curl &> /dev/null ]
then
echo "curl could not be found"
exit 1
fi
# CHECK: jq is installed
if [ ! command -v jq &> /dev/null ]
then
echo "jq could not be found"
exit 1
fi
# CHECK: FQDN argument is present
if [ -z "$1" ]
then
echo "Fully qualified domain name not present"
exit 1
fi
# CHECK: valid FQDN is submitted
if [ -z $(echo $1 | grep -P '(?=^.{1,254}$)(^(?>(?!\d+\.)[a-zA-Z0-9_\-]{1,63}\.?)+(?:[a-zA-Z]{2,})$)') ]
then
echo "Invalid fully qualified domain name"
exit 1
fi
# SET: required information
API_KEY=$(cat $(dirname $0)/api_key.secret)
HOST=$(echo $1 | cut -d"." -f1)
DOMAIN=$(echo $1 | cut -d"." -f1 --complement)
GET_IP_WEBSITE="https://ifconfig.co/"
# FUNCTION: check for a valid ip address
function valid_ip()
{
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
# Get current IP address
if valid_ip $(curl -s -4 $GET_IP_WEBSITE)
then
IP_NOW=$(curl -s -4 $GET_IP_WEBSITE)
else
# Exiting due to invalid IP address
echo "Invalid IP address from $GET_IP_WEBSITE"
exit 1
fi
# Get Gandi DNS IP address
if valid_ip $(curl -s -H "Authorization: Apikey $API_KEY" -X GET https://api.gandi.net/v5/livedns/domains/$DOMAIN/records/$HOST/A | jq -r '.rrset_values[0]')
then
IP_GANDI=$(curl -s -H "Authorization: Apikey $API_KEY" -X GET https://api.gandi.net/v5/livedns/domains/$DOMAIN/records/$HOST/A | jq -r '.rrset_values[0]')
else
# Exiting due to invalid IP address
echo "Invalid IP address from Gandi API"
exit 1
fi
# Check if the IP addresses match and change DNS entry if the don't
if [ $IP_NOW == $IP_GANDI ]
then
# They are the same, exiting
echo "No update required"
exit 0
else
curl -s -H "Authorization: Apikey $API_KEY" -H "Content-Type: application/json" -d '{"rrset_values": ["'${IP_NOW}'"], "rrset_ttl": 1800}' -X PUT https://api.gandi.net/v5/livedns/domains/"${DOMAIN}"/records/"${HOST}"/A > /dev/null
if [ "$?" -ne 0 ]
then
echo "There was a problem running the LiveDNS update command"
exit 1
else
echo "DNS record updated"
fi
fi