-
Notifications
You must be signed in to change notification settings - Fork 1
/
godaddy_ddns.sh
executable file
·136 lines (124 loc) · 5.04 KB
/
godaddy_ddns.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
# GoDaddy.sh v1.0 by Nazar78 @ TeaNazaR.com
###########################################
# Simple DDNS script to update GoDaddy's DNS. Just schedule every 5mins in crontab.
# With options to run scripts/programs/commands on update failure/success.
#
# Requirements:
# - Bash - On LEDE/OpenWRT, opkg install bash
# - curl CLI - On Debian, apt-get install curl
# - jq - On Debian, apt-get install jq
#
# History:
# v1.0 - 20160513 - 1st release.
#
# PS: Feel free to distribute but kindly retain the credits (-:
###########################################
# Begin settings
# Get the Production API key/secret from https://developer.godaddy.com/keys/.
# Ensure it's for "Production" as first time it's created for "Test".
Key=$(snapctl get apikey)
if [ "$Key" = "" ]; then
echo "Missing Godaddy API Key - Please Get the Production API key/secret from https://developer.godaddy.com/keys/"
exit -1
fi
Secret=$(snapctl get apisecret)
if [ "$Secret" = "" ]; then
echo "Missing Godaddy API Secret - Please Get the Production API key/secret from https://developer.godaddy.com/keys/"
exit -1
fi
# Domain to update.
Domain=$(snapctl get domain.name)
if [ "$Domain" = "" ]; then
echo "Use snap set $SNAP_NAME domain.name=YOUR_Domain"
exit -1
fi
# Advanced settings - change only if you know what you're doing :-)
# Record type, as seen in the DNS setup page, default A.
Type=$(snapctl get domain.type)
if [ "$Type" = "" ]; then
echo "domain.type is unset, use default value domain.type=A"
Type=A
fi
# Record name, as seen in the DNS setup page, default @.
Name=$(snapctl get domain.subdomain)
if [ "$Name" = "" ]; then
echo "domain.subdomain is unset, use default value domain.subdomain=@"
Name=@
fi
# Time To Live in seconds, minimum default 600 (10mins).
# If your public IP seldom changes, set it to 3600 (1hr) or more for DNS servers cache performance.
TTL=$(snapctl get domain.ttl)
if [ "$TTL" = "" ]; then
echo "domain.ttl is unset, use default value domain.ttl=600"
TTL=600
fi
# Writable path to last known Public IP record cached. Best to place in tmpfs.
CachedIP=$SNAP_DATA/current_ip
# External URL to check for current Public IP, must contain only a single plain text IP.
# Default http://api.ipify.org.
CheckURL=http://api.ipify.org
# Optional scripts/programs/commands to execute on successful update. Leave blank to disable.
# This variable will be evaluated at runtime but will not be parsed for errors nor execution guaranteed.
# Take note of the single quotes. If it's a script, ensure it's executable i.e. chmod 755 ./script.
# Example: SuccessExec='/bin/echo "$(date): My public IP changed to ${PublicIP}!">>/var/log/GoDaddy.sh.log'
SuccessExec=''
# Optional scripts/programs/commands to execute on update failure. Leave blank to disable.
# This variable will be evaluated at runtime but will not be parsed for errors nor execution guaranteed.
# Take note of the single quotes. If it's a script, ensure it's executable i.e. chmod 755 ./script.
# Example: FailedExec='/some/path/something-went-wrong.sh ${Update} && /some/path/email-script.sh ${PublicIP}'
FailedExec=''
# End settings
Curl=$(/usr/bin/which curl 2>/dev/null)
Touch=$(/usr/bin/which touch 2>/dev/null)
[ "${Curl}" = "" ] &&
echo "Error: Unable to find 'curl CLI'." && exit 1
[ -z "${Key}" ] || [ -z "${Secret}" ] &&
echo "Error: Requires API 'Key/Secret' value." && exit 1
[ -z "${Domain}" ] &&
echo "Error: Requires 'Domain' value." && exit 1
[ -z "${Type}" ] && Type=A
[ -z "${Name}" ] && Name=@
[ -z "${TTL}" ] && TTL=600
[ "${TTL}" -lt 600 ] && TTL=600
${Touch} ${CachedIP} 2>/dev/null
[ $? -ne 0 ] && echo "Error: Can't write to ${CachedIP}." && exit 1
[ -z "${CheckURL}" ] && CheckURL=http://api.ipify.org
echo -n "Checking current 'Public IP' from '${CheckURL}'..."
PublicIP=$(${Curl} -kLs ${CheckURL})
if [ $? -eq 0 ] && [[ "${PublicIP}" =~ [0-9]{1,3}\.[0-9]{1,3} ]];then
echo "${PublicIP}!"
else
echo "Fail! ${PublicIP}"
eval ${FailedExec}
exit 1
fi
if [ "$(cat ${CachedIP} 2>/dev/null)" != "${PublicIP}" ];then
echo -n "Checking '${Domain}' IP records from 'GoDaddy'..."
Check=$(${Curl} -kLsH"Authorization: sso-key ${Key}:${Secret}" \
-H"Content-type: application/json" \
https://api.godaddy.com/v1/domains/${Domain}/records/${Type}/${Name} \
2>/dev/null|jq -r '.[0].data')
if [ $? -eq 0 ] && [ "${Check}" = "${PublicIP}" ];then
echo -n ${Check}>${CachedIP}
echo -e "unchanged!\nCurrent 'Public IP' matches 'GoDaddy' records. No update required!"
else
echo -en "changed!\nUpdating '${Domain}' ${Check} -> ${PublicIP}..."
Update=$(${Curl} -kLsXPUT -H"Authorization: sso-key ${Key}:${Secret}" \
-H"Content-type: application/json" \
https://api.godaddy.com/v1/domains/${Domain}/records/${Type}/${Name} \
-d "[{\"data\":\"${PublicIP}\",\"ttl\":${TTL}}]" 2>/dev/null)
if [ $? -eq 0 ] && [ "${Update}" = "" ];then
echo -n ${PublicIP}>${CachedIP}
echo "Success!"
eval ${SuccessExec}
else
echo "Fail! ${Update}"
eval ${FailedExec}
exit 1
fi
fi
else
echo "Current 'Public IP' matches 'Cached IP' recorded. No update required!"
fi
exit $?