Skip to content

Instantly share code, notes, and snippets.

@SidShetye
Last active June 9, 2021 12:04
Show Gist options
  • Save SidShetye/a083f2e4f3ec8d654b5793c7fda63643 to your computer and use it in GitHub Desktop.
Save SidShetye/a083f2e4f3ec8d654b5793c7fda63643 to your computer and use it in GitHub Desktop.
a shell script to update cloudflare's DNS record
#!/bin/sh
# This is probably better implemented in a real scripting language like powershell script or javascript but
# as a shell script to take minimal dependencies
###### Inputs - change these per your use case
# Go get Zone ID and account ID, log into the CF dashboard -> domain -> overview -> left column, bottom part
zone_identifier=000000000000000000000000000000
# The record we want to update
dns_name=example.com
dns_type="A"
# From https://dash.cloudflare.com -> profile -> token
cloudflare_auth_token=000000000000000000000000000000
log_dir=~/cloudflare-dns-update
#######################
########################
# Cloudflare API Documentation : https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record
########################
# Check jq dependency
jq_command=$(command -v jq)
if [ "$jq_command" = "" ]; then
echo "The JSON parsing tool \"jq\" not found, proceeding to install it ..."
# only supporting debian/ubuntu for now, adapt as needed
sudo apt-get install jq
fi
if [ ! -d "$log_dir" ]; then
echo "Log folder at $log_dir not found, creating it now ..."
mkdir -p $log_dir
fi
########################
# Get Zone ID (note `apt install jq` to parse the returned json)
dns_record_identifier_api=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?type=$dns_type&name=$dns_name&match=all" \
-H "Authorization: Bearer $cloudflare_auth_token" \
-H "Content-Type: application/json" \
| jq -r ".result[0].id")
echo "DNS Record: type=$dns_type name=$dns_name has CloudFlare Zone ID=$dns_record_identifier_api"
########################
# Check if IP has changed since last update
hourstamp=$(date +"%F-%H")
past_ip=$(cat $log_dir/publicip.log)
publicip=$(curl https://api.ipify.org 2>/dev/null)
echo $publicip > $log_dir/publicip.log
if [ "$past_ip1" = "$publicip" ]; then
echo "$(date) : IP Address of $dns_name not changed."
echo "$(date) : IP Address of $dns_name not changed." >> $log_dir/$dns_name.$dns_type.log
exit 0
fi
########################
# Update record
code=$(curl -s -X PUT https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$dns_record_identifier_api \
-H "Authorization: Bearer $cloudflare_auth_token" \
-H "Content-Type: application/json" \
--data '{"type":'\"$dns_type\"',"name":'\"$dns_name\"',"content":'\"$publicip\"',"ttl":1}')
if [ $(echo $code | jq -r ".success") = "true" ]
then
# record public IP after update to server
echo $publicip > $log_dir/publicip.log
echo "$(date) : Updated $dns_type record of $dns_name to $publicip"
echo "$(date) : Updated $dns_type record of $dns_name to $publicip" >> $log_dir/$dns_name.$dns_type.log
else
echo "$(date) : FAILED to update record of $dns_name to $publicip"
echo "$(date) : FAILED to update record of $dns_name to $publicip" >> $log_dir/$dns_name.$dns_type.log
echo $code | jq -r
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment