Vida Digital, the style of technology
Vida Digital Publications  CloudFlare  Dynamic DNS ... 

Dynamic DNS with CloudFlare

 
CloudFlare

The actions rec_load_all and rec_edit of CloudFlare client API and the automatic execution of the Shell script described here usign the cron cron command, allow dynamically update the DNS record that associates the domain name of the site with the IP address that corresponds, each time the Internet service provider's (ISP) changes it.


Keywords: CloudFlare, DDNS, IP



We can verify that we have the services of cron and curl commands via

crontab -l

curl --help

If the answer is command not found, the we can install as follows

sudo apt-get install cron -y

sudo apt-get install curl -y

otherwise already installed on your system.


Check DNS record for a domain rec_load_all


For get the DNS records of a given domain name, must make a POST on the URL https://www.cloudflare.com/api_json.html with parameters
  • a, the action to take in this case rec_load_all
  • tkn, the API key available on the page of your CloudFlare account
  • email, email associated with the API key in your CloudFlare account
  • z, domain name that records are being retrieved from

Example of POST with the rec_load_all action for obtain records of your-domain.com

curl https://www.cloudflare.com/api_json.html \ -d 'a=rec_load_all' \ -d 'tkn=your_cloudflare_api_key' \ -d '[email protected]' \ -d 'z=your-domain.com'
The execution of the previous command will response a message in JSON (JavaScript Object Notation) format that we can analyze with the help of the tool Online JSON Editor.

We get the value of the attribute client_id in the type "A" record which is required as a parameter for the next action.


Edit record rec_edit


To edit a determined DNS record from our domain name, must make a POST on the URL https://www.cloudflare.com/api_json.html with the parameters
  • a, the action to take in this case rec_edit
  • tkn, the API key available in the page of your CloudFlare account
  • email, email associated to the API key in the page of your ClodFlare account
  • z, domain name that DNS records are being retrieved from
  • type, type of DNS record to change (A, CNAME, MX, TXT, SPF, AAAA, NS, SRV o LOC)
  • id, DNS record identifier, obtained through the action rec_load_all
  • name, name of DNS record
  • content, content of the DNS record
  • ttl, lifetime of the record in seconds

Example of POST with the rec_edit action for edit a type "A" record.

curl https://www.cloudflare.com/api_json.html \ -d 'a=rec_edit' \ -d 'tkn=your_cloudflare_api_key' \ -d '[email protected]' \ -d 'z=your-domain.com' \ -d 'id=197122144' \ -d 'type=A' \ -d 'name=your-domain.com' \ -d 'ttl=1' \ -d "content=ip_public_address"

Script Shell for update IP address


Shell script that performs the update of IP with the help of the command previously described
#!/bin/sh CURRENT_IP=`wget -O - -q http://icanhazip.com` PREVIOUS_IP=`cat /var/public_ip.txt` if [ "$CURRENT_IP" = "$PREVIOUS_IP" ] then echo "The public IP has not changed." else curl https://www.cloudflare.com/api_json.html \ -d 'a=rec_edit' \ -d 'tkn=your_cloudflare_api_key' \ -d '[email protected]' \ -d 'z=your-domain.com' \ -d 'id=137732146' \ -d 'type=A' \ -d 'name=your-domain.com' \ -d 'ttl=1' \ -d "content=$CURRENT_IP" echo "public IP updated." echo $CURRENT_IP > /var/public_ip.txt fi

Configuration of cron


Finally the cron configuration, to run the shell script every 10 minutes and ensure that the website is available again 10 minutes after that the public IP is changed by your Internet service provider.

Create the script update_ip_cloudflare.sh in the directory /opt, define it as their own and give execution privileges.

sudo chown $USER:root /opt/actualizar_ip_cloudflare.sh
chmod 0750 /opt/update_ip_cloudflare.sh

schedule the script execution with cron and redirect the output of standard messages and errors to the file /opt/update_ip_cloudflare.log.

crontab -e
*/10 * * * * /opt/update_ip_cloudflare.sh > /opt/update_ip_cloudflare.log 2>&1

For testing purposes, you can change the value of the IP address in the file /var/public_ip.txt and review after 10 minutes.
Phone Office +52 (55) 6263 6249
Email [email protected]
Google+ Vida Digital
Facebook Vida Digital
Google places Contact
Trust Certificate Trust Certificate
Idioma Español

Visit our Online Store Store, accept Bitcoin Bitcoins