Last active
January 7, 2023 16:12
-
-
Save jkiv/cd40e4a5ed0b41492b4bdfa3b0533b23 to your computer and use it in GitHub Desktop.
Linode Dynamic DNS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Updates Linode DNS records to the host's external IP, if the record differs. | |
# * Depends on curl(1) and jq(1). | |
# * Requires API key with read/write Domain permission. | |
# * Presumes IPv4 (A record). | |
# | |
# Author: jkiv (https://github.com/jkiv) | |
# | |
# Linode Configuration | |
LINODE_API_TOKEN='<api_token>' | |
LINODE_DOMAIN_ID='<domain_id>' | |
LINODE_DOMAIN_RECORD_IDS=('<record_id_1>' '<record_id_2>') | |
# Get Current IP (uses opendns.com) | |
CURRENT_IP=$(dig @resolver4.opendns.com myip.opendns.com +short) | |
# Update each record for given domain | |
for RECORD_ID in ${LINODE_DOMAIN_RECORD_IDS[@]}; do | |
# Grab current state of DNS record | |
CURRENT_RECORD=$(\ | |
curl -s \ | |
-H "Authorization: Bearer ${LINODE_API_TOKEN}" \ | |
-X GET "https://api.linode.com/v4/domains/${LINODE_DOMAIN_ID}/records/${RECORD_ID}"\ | |
) | |
RECORD_IP=$(echo "${CURRENT_RECORD}" | jq -r .target) | |
RECORD_NAME=$(echo "${CURRENT_RECORD}" | jq -r .name) | |
# Only update if IP address has changed | |
if [ "${RECORD_IP}" != "${CURRENT_IP}" ]; then | |
echo "Updating '${RECORD_NAME}' (${RECORD_ID}): ${RECORD_IP} -> ${CURRENT_IP}" | |
# Update target field | |
CURRENT_RECORD=$(echo "${CURRENT_RECORD}" | jq '. | .target="[remote_addr]"') | |
# Push update to Linode | |
curl -s \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: Bearer ${LINODE_API_TOKEN}" \ | |
-X PUT -d "${CURRENT_RECORD}"\ | |
"https://api.linode.com/v4/domains/${LINODE_DOMAIN_ID}/records/${RECORD_ID}" | jq . | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment