-
Notifications
You must be signed in to change notification settings - Fork 5
/
do_dns_update.sh
193 lines (172 loc) · 5.17 KB
/
do_dns_update.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash
# @requires awk, curl, grep, mktemp, sed, tr.
## START EDIT HERE.
do_access_token=""; # paste your DO Personal Access Token here.
curl_timeout="15";
loop_max_records="50";
url_do_api="https://api.digitalocean.com/v2";
url_ext_ip="http://checkip.dyndns.org";
url_ext_ip2="http://ifconfig.me/ip";
update_only=false;
verbose=true;
filename="$(basename $BASH_SOURCE)";
## END EDIT.
# get options.
while getopts "ush" opt; do
case $opt in
u) # update.
update_only=true;
;;
s) # silent.
verbose=false;
;;
h) # help.
echo "Usage: $filename [options...] <record name> <domain>";
echo "Options:";
echo " -h This help text";
echo " -u Updates only. Don't add non-existing";
echo " -s Silent mode. Don't output anything";
echo "Example:";
echo " Add/Update nas.mydomain.com DNS A record with current public IP";
echo " ./$filename -s nas mydomain.com";
echo;
exit 0;
;;
\?)
echo "Invalid option: -$OPTARG (See -h for help)" >&2
exit 1;
;;
esac
done
# validate.
shift $(( OPTIND - 1 ));
do_record="$1";
do_domain="$2";
if [ $# -lt 2 ] || [ -z "$do_record" ] || [ -z "$do_domain" ] ; then
echo "Missing required arguments. (See -h for help)";
exit 1;
elif [ -z "$do_access_token" ] ; then
echo "Missing token. Please edit this script and add your access token first.";
exit 1;
fi
echov()
{
if [ $verbose == true ] ; then
if [ $# == 1 ] ; then
echo "$1";
else
printf "$@";
fi
fi
}
# modified from https://gist.github.com/cjus/1047794#comment-1249451
json_value()
{
local KEY=$1
local num=$2
awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/\042'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n "$num"p
}
get_external_ip()
{
ip_address="$(curl -s --connect-timeout $curl_timeout $url_ext_ip | sed -e 's/.*Current IP Address: //' -e 's/<.*$//' | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')";
if [ -z "$ip_address" ] ; then
ip_address="$(curl -s --connect-timeout $curl_timeout $url_ext_ip2 | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')";
if [ -z "$ip_address" ] ; then
return 1;
fi
else
return 0;
fi
}
# https://developers.digitalocean.com/#list-all-domain-records
get_record()
{
local tmpfile="$(mktemp)";
curl -s --connect-timeout "$curl_timeout" -H "Authorization: Bearer $do_access_token" -X GET "$url_do_api/domains/$do_domain/records" > "$tmpfile"
if [ ! -s "$tmpfile" ] ; then
return 1;
fi
local do_num_records="$(json_value total 1 < $tmpfile)";
if [[ ! "$do_num_records" =~ ^[0-9]+$ ]] || [ "$do_num_records" -gt "$loop_max_records" ] ; then
do_num_records=$loop_max_records;
fi
for i in `seq 1 $do_num_records`
do
record['name']="$(json_value name $i < $tmpfile)";
if [ "${record[name]}" == "$do_record" ] ; then
record['id']="$(json_value id $i < $tmpfile)";
record['data']="$(json_value data $i < $tmpfile)";
if [ ! -z "${record[id]}" ] && [[ "${record[id]}" =~ ^[0-9]+$ ]] ; then
rm -f "$tmpfile";
return 0;
fi
break;
fi
done
rm -f "$tmpfile";
return 1;
}
# https://developers.digitalocean.com/#update-a-domain-record
set_record_ip()
{
local id=$1
local ip=$2
local data=`curl -s --connect-timeout $curl_timeout -H "Content-Type: application/json" -H "Authorization: Bearer $do_access_token" -X PUT "$url_do_api/domains/$do_domain/records/$id" -d'{"data":"'"$ip"'"}'`;
if [ -z "$data" ] || [[ "$data" != *"id\":$id"* ]]; then
return 1;
else
return 0;
fi
}
# https://developers.digitalocean.com/v2/#create-a-new-domain-record
new_record()
{
local ip=$1
local data=`curl -s --connect-timeout $curl_timeout -H "Content-Type: application/json" -H "Authorization: Bearer $do_access_token" -X POST "$url_do_api/domains/$do_domain/records" -d'{"name":"'"$do_record"'","data":"'"$ip"'","type":"A"}'`;
if [ -z "$data" ] || [[ "$data" != *"data\":\"$ip"* ]]; then
return 1;
else
return 0;
fi
}
# start.
echov "* Updating %s.%s: $(date +"%Y-%m-%d %H:%M:%S")\n\n" "$do_record" "$do_domain";
echov "* Fetching external IP from: $url_ext_ip";
get_external_ip;
if [ $? -ne 0 ] ; then
echov "Unable to extract external IP address";
exit 1;
fi
echov "* Fetching Record ID for: $do_record";
just_added=false;
declare -A record;
get_record;
if [ $? -ne 0 ] ; then
if [ $update_only == true ] ; then
echov "Unable to find requested record in DO account";
exit 1;
else
echov "* No record found. Adding: $do_record";
new_record "$ip_address";
if [ $? -ne 0 ] ; then
echov "Unable to add new record";
exit 1;
fi
just_added=true;
fi
fi
if [ $update_only == true ] || [ $just_added != true ] ; then
echov "* Comparing ${record[data]} to $ip_address";
if [ "${record[data]}" == "$ip_address" ] ; then
echov "Record $do_record.$do_domain already set to $ip_address";
exit 1;
fi
echov "* Updating record ${record[name]}.$do_domain to $ip_address";
set_record_ip "${record[id]}" "$ip_address";
if [ $? -ne 0 ] ; then
echov "Unable to update IP address";
exit 1;
fi
fi
echov "\n* IP Address successfully added/updated.\n\n" "";
exit 0;