Cloudflare dynamic DNS
Curl Cloudflare dymanic DNS


Sources:

https://jenneysgarage.com/dynamically-update-dns-with-cloudflare-client-api/

https://www.cloudflare.com/docs/client-api.html

Check the id and name for the domain/subdomain you want to update. The id is expressed as “rec_id”:“1212131313”.

curl https://www.cloudflare.com/api_json.html \
  -d 'a=rec_load_all' \
  -d 'tkn=CLOUDFLARE KEY' \
  -d 'email=EMAIL ADDRESS' \
  -d 'z=DOMAIN'

vim cloudflare.sh

#!/bin/bash

IP=$(wget -O - -q http://icanhazip.com)
OLD_IP=$(cat /home/user/scripts/IP.txt)

if [ "$IP" = "$OLD_IP" ]
 then
   exit 0
 else
 curl https://www.cloudflare.com/api_json.html \
  -d 'a=rec_edit' \
  -d 'tkn=CLOUDFLARE KEY' \
  -d 'email=EMAIL ADDRESS' \
  -d 'z=DOMAIN' \
  -d 'id=DOMAIN/SUBDOMAIN ID' \
  -d 'type=A' \
  -d 'name=DOMAIN/SUBDOMAIN NAME' \
  -d 'ttl=1' \
  -d "content=$IP"

    echo $IP > /home/user/scripts/IP.txt
    echo "The new IP is: $IP"
fi

set crontab to check for an update every 10 minutes:

crontab -e

*/10 * * * * /home/user/scripts/cloudflare.sh

Optional set root:root as owner and group for the files for added security and set crontab as root to execute the script.

sudo chown root:root /home/user/scripts/{cloudflare.sh,checkcloud.sh,IP.txt}

sudo chmod 700 /home/user/scripts/{cloudflare.sh,checkcloud.sh}

sudo chmod 600 /home/user/scripts/IP.txt


Posted on:

February 1, 2015
152 words

Contact Me:

If you are interested in contacting me feel free to do so by mail or online:

nob {at} nob.ro