Skip to content

Instantly share code, notes, and snippets.

@jalberto
Created December 9, 2022 15:48
Show Gist options
  • Save jalberto/484be001a671be2c7a973b611c7dcf8d to your computer and use it in GitHub Desktop.
Save jalberto/484be001a671be2c7a973b611c7dcf8d to your computer and use it in GitHub Desktop.
Cloudflare dyn DNS updater with curl & jq
AUTH_KEY='CF AUTH TOKEN KEY'
EMAIL_ADDRESS='CF email'
DNS_ZONE_NAME='domain.name'
DNS_RECORD_NAME="hola.domain.name"
DNS_ZONE=$(curl -sX GET "https://api.cloudflare.com/client/v4/zones?name=${DNS_ZONE_NAME}" -H "Content-Type:application/json" -H "Authorization:Bearer ${AUTH_KEY}" -H "X-Auth-Email:${EMAIL_ADDRESS}" | jq -r '.result[0].id')
DNS_RECORD=$(curl -sX GET "https://api.cloudflare.com/client/v4/zones/${DNS_ZONE}/dns_records?name=${DNS_RECORD_NAME}" -H "Content-Type:application/json" -H "Authorization:Bearer ${AUTH_KEY}" -H "X-Auth-Email:${EMAIL_ADDRESS}" | jq -r '.result[0].id')
CURRENT_DNS_VALUE=$(curl -sX GET "https://api.cloudflare.com/client/v4/zones/${DNS_ZONE}/dns_records/${DNS_RECORD}" -H "Content-Type:application/json" -H "Authorization:Bearer ${AUTH_KEY}" -H "X-Auth-Email:${EMAIL_ADDRESS}" | jq '.result["content"]')
CURRENT_IP_ADDRESS=$(curl -s ip.me)
if [ ${CURRENT_DNS_VALUE} != ${CURRENT_IP_ADDRESS} ]; then
curl -sX PUT "https://api.cloudflare.com/client/v4/zones/${DNS_ZONE}/dns_records/${DNS_RECORD}" -H "X-Auth-Email:${EMAIL_ADDRESS}" -H "Authorization:Bearer ${AUTH_KEY}" -H "Content-Type:application/json" --data '{"type":"A","name":"'"${DNS_RECORD_NAME}"'","content":"'"${CURRENT_IP_ADDRESS}"'"}' > /dev/null
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment