Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick and dirty DDNS using Bash and Cloudflare (API v4 compatible)
#!/usr/bin/env bash
# Step 1: Fill in EMAIL, TOKEN, DOMAIN and SUBDOMAIN. Your API token is here: https://www.cloudflare.com/a/account/my-account
# Make sure the token is the Global token, or has these permissions: #zone:read, #dns_record:read, #dns_records:edit
# Step 2: Create an A record on Cloudflare with the subdomain you chose
# Step 3: Run "./ddns.sh -l" to get the zone_id and rec_id of the record you created.
# Fill in ZONE_ID and REC_ID below
# This step is optional, but will save you 2 requests every time you this script
# Step 4: Run "./ddns.sh". It should tell you that record was updated or that it didn't need updating.
# Step 5: Run it every hour with cron. Use the '-s' flag to silence normal output
# 0 * * * * /path/to/ddns.sh -s
EMAIL=''
TOKEN=''
DOMAIN=''
SUBDOMAIN=''
ZONE_ID=''
REC_ID=''
set -euo pipefail
#set -x # enable for debugging
VERBOSE="[ '${1:-}' != '-s' ]"
LOOKUP="[ '${1:-}' == '-l' ]"
API_URL="https://api.cloudflare.com/client/v4"
CURL="curl -s \
-H Content-Type:application/json \
-H X-Auth-Key:$TOKEN \
-H X-Auth-Email:$EMAIL "
if [ -z "$ZONE_ID" ] || $LOOKUP; then
ZONE_ID="$($CURL "$API_URL/zones?name=$DOMAIN" | sed -e 's/[{}]/\n/g' | grep '"name":"'"$DOMAIN"'"' | sed -e 's/,/\n/g' | grep '"id":"' | cut -d'"' -f4)"
$VERBOSE && echo "ZONE_ID='$ZONE_ID'"
fi
if [ -z "$REC_ID" ] || $LOOKUP; then
REC_ID="$($CURL "$API_URL/zones/$ZONE_ID/dns_records" | sed -e 's/[{}]/\n/g' | grep '"name":"'"$SUBDOMAIN"'.'"$DOMAIN"'"' | sed -e 's/,/\n/g' | grep '"id":"' | cut -d'"' -f4)"
$VERBOSE && echo "REC_ID='$REC_ID'"
fi
$LOOKUP && exit 0
IP="$(curl -s http://ipv4.icanhazip.com)"
RECORD_IP="$($CURL "$API_URL/zones/$ZONE_ID/dns_records/$REC_ID" | sed -e 's/[{}]/\n/g' | sed -e 's/,/\n/g' | grep '"content":"' | cut -d'"' -f4)"
if [ "$IP" == "$RECORD_IP" ]; then
$VERBOSE && echo "IP Unchanged"
exit 0
fi
$VERBOSE && echo "Setting IP to $IP"
$CURL -X PUT "$API_URL/zones/$ZONE_ID/dns_records/$REC_ID" --data '{"type":"A","name":"'"$SUBDOMAIN"'","content":"'"$IP"'","proxied":false}' 1>/dev/null
exit 0
@davidcastellani

This comment has been minimized.

Copy link

davidcastellani commented May 17, 2016

@lyoshenka , Cloudflare is deprecating API v1 in favor of v4.

Email I received just this morning.

Starting November 9th, 2016 at noon Pacific Time (20:00 UTC), CloudFlare will no longer be supporting API v1.

They then link to https://www.cloudflare.com/migrating-to-v4/

Any chance you would convert this script to API v4?

@lyoshenka

This comment has been minimized.

Copy link
Owner Author

lyoshenka commented Jul 24, 2016

@davidcastellani just updated it. give it a try and let me know if you see any issues

@Agazed

This comment has been minimized.

Copy link

Agazed commented Sep 28, 2016

How would you change a wildcard record or the root domain? What would you set in the subdomain variable for those records? I tried putting in * and (mydomain).net and it does not work. @lyoshenka

@niclasreich

This comment has been minimized.

Copy link

niclasreich commented Mar 2, 2017

@Agazed

Have you found any solution? Still looking for a fix...

@MachineITSvcs

This comment has been minimized.

Copy link

MachineITSvcs commented Jan 3, 2018

For anyone interested, I wrote a similar script in bash for use with multiple Cloudflare accounts, zones, records, and a proxy option: here

@DkWyatt

This comment has been minimized.

Copy link

DkWyatt commented Sep 23, 2018

reaplace L42
REC_ID="$($CURL "$API_URL/zones/$ZONE_ID/dns_records?name=$SUBDOMAIN" | awk '{t=$0;gsub(/.*"id":"|".*/,"",t);print t}')"

@waiyanwh

This comment has been minimized.

Copy link

waiyanwh commented May 21, 2019

replace l37

ZONE_ID="$($CURL "$API_URL/zones?name=$DOMAIN" | jq '.result[0].id'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.