forked from fatbattk/DigitalOceanDynDNS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdo_dns_update.sh
executable file
·217 lines (192 loc) · 6.51 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/bin/bash
# @requires jq, curl, grep, mktemp, sed, tr.
: ${XDG_CONFIG_DIRS:=~/.config}
do_access_token="${XDG_CONFIG_DIRS}"/do_dns_update/access_token
curl_timeout="15";
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 [ ! -r "$do_access_token" ]; then
echo "Missing token. Please paste it into $do_access_token first."
exit 1
# insist on secure permissions: only the owner of the file should be able to read or write it.
# we do a bitwise AND to mask out the g=rw and o=rw bits from the permissions;
# if masking those out gives 0, then we're good
# note the leading 0s! That makes these numbers octal!
# XXX if we're concerned about writes then this also needs to check permissions on all parent directories too.
elif [ $(($(stat -c "%#0a" $do_access_token) & 0066)) -ne 0 ]; then
echo "Error: $do_access_token has insecure permissions."
exit 1
fi
# read in access token
strip_comments()
{
sed 's/#.*//' | # strip comments
sed 's/^\s*//' | # strip leading whitespace
sed 's/\s*$//' | # strip trailing whitespace
sed '/^$/d' # strip empty lines -- which, after stripping leading/trailing whitespace, covers blank lines too
}
do_access_token="$(cat $do_access_token | strip_comments | head -n 1)";
echov()
{
if [ $verbose == true ] ; then
if [ $# == 1 ] ; then
echo "$1";
else
printf "$@";
fi
fi
}
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()
{
next_page="$url_do_api/domains/$do_domain/records"
while [ ! -z $next_page ]; do
local tmpfile="$(mktemp)";
curl -s --connect-timeout "$curl_timeout" -H "Authorization: Bearer $do_access_token" -X GET "$next_page" > "$tmpfile"
if [ ! -s "$tmpfile" ] ; then
return 1;
fi
# "// empty" makes jq output "" instead of "null" when we get to the end of the list,
# and -r makes it output strings as shell strings instead of wrapped in quotes as json
# which makes it fit in better with shell. See https://github.com/sftedolan/jq/issues/354
next_page="$(jq -r ".links.pages.next // empty" < $tmpfile)";
# XXX pre-counting the loop is probably unecessary; we should be able to get
# jq to give us items in a single pass; probably even to do most of the logic internally,
# which would parse faster.
local do_num_records="$(jq ".meta.total // empty" < $tmpfile)";
if [[ ! "$do_num_records" =~ ^[0-9]+$ ]] ; then
echo "Warning: no DNS record count found in DO API reply." >&2
continue;
fi
for i in `seq 1 $do_num_records`
do
record['name']="$(jq -r ".domain_records[$i].name // empty" < $tmpfile)";
if [ "${record[name]}" == "$do_record" ] ; then
record['id']="$(jq -r ".domain_records[$i].id // empty" < $tmpfile)";
record['data']="$(jq -r ".domain_records[$i].data // empty" < $tmpfile)";
if [ ! -z "${record[id]}" ] && [[ "${record[id]}" =~ ^[0-9]+$ ]] ; then
rm -f "$tmpfile";
return 0;
fi
break;
fi
done
rm -f "$tmpfile";
done
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;