summaryrefslogtreecommitdiff
path: root/linode-ddns.sh
blob: d3a9923819c355d4bd410bcd4f11d893357dea69 (plain) (blame)
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
#!/usr/bin/env bash
#
# Copyright 2022 David Vazgenovich Shakaryan
#
# LINODE_TOKEN=<token> linode-ddns.sh <domain> <record name>

IP_RESOLVER='https://ifconfig.co'

DOMAIN="${1}"
RECORD="${2}"

die() {
	[[ -n "${@}" ]] && echo "${@}" >&2
	exit 1
}

lincurl() {
	curl -sfH "Authorization: Bearer ${LINODE_TOKEN}" \
		"https://api.linode.com/v4/domains/${1}" \
		"${@:2}"
}

ip="$(curl -sf4 "${IP_RESOLVER}")" || die 'IP lookup failed'

res="$(lincurl)" || die 'Domains lookup failed'
domain_id="$(jq -er --arg domain "${DOMAIN}" \
	'first(.data[] | select(.domain == $domain)) | .id' \
	<<< "${res}")" || die 'Domain not found'

res=$(lincurl "${domain_id}/records") || die 'Records lookup failed'
record_id="$(jq -er --arg name "${RECORD}" \
	'first(.data[] | select(.type == "A" and .name == $name)) | .id' \
	<<< "${res}")" || die 'Record not found'

res="$(lincurl "${domain_id}/records/${record_id}")" \
	|| die 'Record lookup failed'
old_ip="$(jq -r '.target' <<< "${res}")"

if [[ "${old_ip}" == "${ip}" ]]; then
	echo "IP unchanged from ${ip}"
	exit
fi

res="$(lincurl "${domain_id}/records/${record_id}" \
	-H 'Content-Type: application/json' \
	-X PUT -d "{\"target\":\"${ip}\"}")" || die 'Record update failed'
new_ip="$(jq -r '.target' <<< "${res}")"

echo "IP changed from ${old_ip} to ${new_ip}"