diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-12-08 14:37:38 -0800 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-12-08 14:37:38 -0800 |
commit | 27c3a59dc75c2bf4ec90b2ef752939124618dffa (patch) | |
tree | bafb9095592895a4e5d571164359a81f11ebf00a /linode-ddns.sh | |
download | linode-ddns-27c3a59dc75c2bf4ec90b2ef752939124618dffa.tar.gz linode-ddns-27c3a59dc75c2bf4ec90b2ef752939124618dffa.tar.xz |
initial import
Diffstat (limited to 'linode-ddns.sh')
-rwxr-xr-x | linode-ddns.sh | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/linode-ddns.sh b/linode-ddns.sh new file mode 100755 index 0000000..d3a9923 --- /dev/null +++ b/linode-ddns.sh @@ -0,0 +1,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}" |