Skip to content

Instantly share code, notes, and snippets.

@aklef
Forked from benkulbertis/cloudflare-update-record.sh
Last active October 17, 2016 03:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aklef/43298d15366d2a763628201fc778d4fe to your computer and use it in GitHub Desktop.
Save aklef/43298d15366d2a763628201fc778d4fe to your computer and use it in GitHub Desktop.
Cloudflare API v4 Dynamic DNS updater for BSD Bash
#!/bin/sh
#
# 10/16/2016
# Cloudflare api script written in bash
#
# Using crontab, you should schedule this to run daily
# Something like: 0 */12 * * * (for noon and midnight)
# CHANGE THESE
cf_email="user@example.com"
auth_key="c2547eb745079dac9320b638f5e225cf483cc5cfdda41" # found in cloudflare account settings
zone_name="example.com"
record_name="www.example.com"
# MAYBE CHANGE THESE
IP=$(curl -s http://ipv4.icanhazip.com)
#IP=`wget -O - -q http://icanhazip.com`
IP_file=".old-ip-cf.txt"
id_file="cloudflare.ids"
log_file="cloudflare.log"
# LOGGER
log() {
if [ "$1" ]; then
echo -e "[$(date)] - $1" >> $log_file
fi
}
# contains(string, substring)
#
# Returns 0 if the specified string contains the specified substring,
# otherwise returns 1.
contains() {
string="$1"
substring="$2"
if test "${string#*$substring}" != "$string"
then
return 0 # $substring is in $string
else
return 1 # $substring is not in $string
fi
}
# SCRIPT START
log "DDNS Check Initiated..."
if [ -f $IP_file ]; then
OLD_IP=$(cat $IP_file)
if [ $IP == $OLD_IP ]; then
log "IP Unchanged."
exit 0
fi
else
log "No old IP on file, fetching..."
fi
if [ -f $id_file ] && [ $(wc -l $id_file | cut -d " " -f 1) == 2 ]; then
zone_identifier=$(head -1 $id_file)
record_identifier=$(tail -1 $id_file)
log "IDs were read from file..."
else
zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $cf_email" -H "X-Auth-Key: $cf_key" -H "Content-Type: application/json")
regex='"id"\:"([0-9a-z]*)"*'
if [[ $zone_identifier =~ $regex ]]; then
zone_identifier="${BASH_REMATCH[1]}"
fi
echo "$zone_identifier" > $id_file
record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $cf_email" -H "X-Auth-Key: $cf_key" -H "Content-Type: application/json")
if [[ $record_identifier =~ $regex ]]; then
record_identifier="${BASH_REMATCH[1]}"
fi
echo "$record_identifier" >> $id_file
log "IDs fetched from cf."
fi
update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $cf_email" -H "X-Auth-Key: $cf_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$IP\"}")
if (contains "$update" '"success":false')
then
msg="API UPDATE FAILED. DUMPING RESULTS:\n$update"
log "$msg"
echo -e "$msg"
exit 1
else
log "Writing IP to file..."
echo "$IP" > $IP_file
log "IP changed to: $IP"
echo "$msg"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment