Skip to content

Instantly share code, notes, and snippets.

@alex-phillips
Created November 19, 2024 20:34
Show Gist options
  • Save alex-phillips/62b6f49d0d59d0ed703005a7d2ecbad4 to your computer and use it in GitHub Desktop.
Save alex-phillips/62b6f49d0d59d0ed703005a7d2ecbad4 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Dynamic DNS for CF!
# Perfect for updating a single sub domain
# Get a record name from the command line e.g: Update.sh taco ; This will update taco.domain.com
name=$1
if [ -z "$name" ]
then
echo "[$(date)] - Record Name Missing, e.g: Update.sh taco ; This will update taco.domain.com"
exit
fi
# CloudFlare Account
auth_email=""
auth_key=""
zone_name=""
# Quick check to ensure you have updated the config above.
if [ $auth_key = "AUTHKEY" ]
then
echo "[$(date)] - Oops! Please fill in the CloudFlare Account Details"
exit
fi
# Get IP Address
# If Amazon ever retire this service checkout https://ipinfo.io/ip or https://icanhazip.com
ip=$(curl -s https://checkip.amazonaws.com)
if [ -z "$ip" ]
then
echo "[$(date)] - Oops! We can't find an external IP address"
exit
fi
# Match the Name to our Zone
record_name="$name.$zone_name"
echo "[$(date)] - Checking $record_name"
# Get Zone ID
zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
# Check for Record Entry
record_exists=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*')
if [ -z "$record_exists" ]
then
echo "[$(date)] - No Entry for Record: $record_name"
exit
fi
# Get Record ID
record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*')
# Get Assigned IP
record_ip=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="content":")[^"]*')
if [ "$ip" = "$record_ip" ]
then
echo "[$(date)] - No Update Required: $record_name is $record_ip"
exit
else
echo "[$(date)] - Update Required: $record_name points to $record_ip"
fi
# Update Record
echo "[$(date)] - Updating $record_name to $ip"
update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\",\"proxied\":false}")
echo "[$(date)] - All Done!"
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment