-
Notifications
You must be signed in to change notification settings - Fork 1
/
update-godaddy.sh
executable file
·82 lines (76 loc) · 2.11 KB
/
update-godaddy.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
#!/bin/bash -ex
# Default values
type=A
name=@
ttl=600
while [[ $# -gt 1 ]] ; do
case $1 in
-k|--key)
key=$2
shift
;;
-s|--secret)
secret=$2
shift
;;
-d|--domain)
domain=$2
shift
;;
-t|--type)
type=$2
shift
;;
-n|--name)
name=$2
shift
;;
-l|--ttl)
ttl=$2
shift
;;
*)
;;
esac
shift
done
[ -n "$key" ] || { echo 'Missing key' ; exit 1 ; }
[ -n "$secret" ] || { echo 'Missing secret' ; exit 1 ; }
[ -n "$domain" ] || { echo 'Missing domain' ; exit 1 ; }
cached_ip=/tmp/current_ip
chech_url=http://api.ipify.org
echo -n "Checking current public IP from '${chech_url}'..."
public_ip=$(curl -kLs ${chech_url})
if [ $? -eq 0 ] && [[ "${public_ip}" =~ [0-9]{1,3}\.[0-9]{1,3} ]];then
echo "Public IP : ${public_ip}!"
else
echo "Fail! ${public_ip}"
exit 1
fi
if [ "$(cat ${cached_ip} 2>/dev/null)" != "${public_ip}" ] ; 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 | sed -r 's/.+data":"(.+)","t.+/\1/g' 2>/dev/null)
if [ $? -eq 0 ] && [ "${check}" = "${public_ip}" ] ; then
echo -n ${check} > ${cached_ip}
echo -e "unchanged!\nCurrent public IP (${check}) matches GoDaddy records. No update required!"
else
echo -en "changed!\nUpdating '${domain}' to ${check}..."
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\":\"${public_ip}\",\"ttl\":${ttl}}" 2>/dev/null)
if [ $? -eq 0 ] && [ "${update}" = "{}" ] ; then
echo -n ${public_ip} > ${cached_ip}
echo "Success!"
else
echo "Fail! ${update}"
exit 1
fi
fi
else
echo "Current public IP matches cached IP recorded (${public_ip}). No update required!"
fi
exit $?