Skip to content

Instantly share code, notes, and snippets.

@Dimtree
Created October 13, 2018 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dimtree/62618798971fb5534eab072c14ee660a to your computer and use it in GitHub Desktop.
Save Dimtree/62618798971fb5534eab072c14ee660a to your computer and use it in GitHub Desktop.
#!/bin/bash
cloudflare_email="test@example.com" # Your Cloudflare email
cloudflare_api_key="API_KEY_HERE" # Your Cloudflare API key
cloudflare_dns_zone="DNS_ZONE_HERE" # DNS zone containing the record you want to modify
cloudflare_dyndns_record="home.example.com" # Name of the record you want to modify
res="$(curl -sS -X GET "https://api.cloudflare.com/client/v4/zones/$cloudflare_dns_zone/dns_records" \
-H "X-Auth-Email: $cloudflare_email" \
-H "X-Auth-Key: $cloudflare_api_key" \
-H "Content-Type: application/json")"
remote_ip="$(echo "$res" | jq ".result[]|select(.name==\"$cloudflare_dyndns_record\").content")"
remote_ip="$(eval echo "$remote_ip")" # Fix quotes in string
local_ip="$(curl -sS -4 icanhazip.com)"
echo "Remote IP: $remote_ip"
echo "Local IP: $local_ip"
if [[ "$remote_ip" == "$local_ip" ]]; then
echo "IP addresses are the same."
exit 0
else
echo "IP addresses differ. Changing remote IP..."
cloudflare_dyndns_record_zone_id="$(echo "$res" | jq ".result[]|select(.name==\"$cloudflare_dyndns_record\").id")"
cloudflare_dyndns_record_zone_id="$(eval echo "$cloudflare_dyndns_record_zone_id")" # Fix quotes in string
if curl -sS -o /dev/null -X PUT "https://api.cloudflare.com/client/v4/zones/$cloudflare_dns_zone/dns_records/$cloudflare_dyndns_record_zone_id" \
-H "X-Auth-Email: $cloudflare_email" \
-H "X-Auth-Key: $cloudflare_api_key" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"'$cloudflare_dyndns_record'","content":"'$local_ip'","ttl":120,"proxied":false}'; then
echo "Successfully updated remote IP."
exit 0
else
echo "Unable to update remote IP."
exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment