Cloudflare dns update - Free alternative to ddns?
Tags: dns, shell, webdev, cloudflare
Published: 2018-12-19 09:36:16

---
title: Cloudflare dns update - Free alternative to ddns?
published: true
description: Free alternative to dynamic dsn clients   
tags: dns,shell,webdev,CloudFlare
---

So recently I setup myself a little web server at home so I could mess around with it and have a place where I could store and later show the projects to friends and/or clients. But I was facing a problem with my little idea: **_I have a dynamic IP at home_**. So I started looking around for solutions and I remembered the [DynDNS](https://dyn.com/dns/) and that they __had__ a free version of their dynamic dns plan (sadly that is not the case anymore). So I looked for an alternative and found the [NoIP](https://noip.com). They have a free plan which gives you an ability to pick your hostname with one of their free domains (ex yourname.ddns.net).

This was a great find but unfortunately for me it had an clause in their free plan that meant I had to click on a link in my email and go trough the verification process to confirm that I'm still using my hostname and that it shouldn't be removed. I wasn't able to automate this process as it had several steps in it and a google recaptcha to make sure I wasn't using a script or a bot to do this for me.

So I started looking around for other options while keeping the NoIP hostname alive to allow me to access the server from the outside world. One other alternative I found was using my friends servers and domain to establish a ssh tunnel via autossh and have a port forwarding setup so that I could access it. This was also nice but it had some downsides to it. One was that I had to use my friends domain and ports that he would allow me to use so that they don't conflict with ports he is using. 

So again we set it all up and I used it for a while to see how things would go and everything was nice (I still use some parts of it, for security) but for the most part I found an alternative. I remembered that my own domain was being routed trough the [CloudFlare](https://cloudflare.com) and that I saw links to some API documentation. So I logged in to my account and found the documentation and started to read it. Quickly found the documentation for the dns zones and how to get a list of them and how to update a specific zone. 

_Now came the fun part_

I opened up nano (don't judge me :) ) on my server and started writing and googling on how to do stuff in shell/bash (not much experience before this). One of the first things I had to lookup was how to get my public IP and store it to a variable. 

A big help in this was a [nixCraft](https://www.cyberciti.biz/) site and a thank you to [@nixCraft](https://twitter.com/nixcraft) for writing such awesome and easy to understand tutorials you are the RMVP :)

After that I started to look around for ways to get the information from CloudFlare and used [Postman](https://www.getpostman.com/) and its awesome feature to generate code from your request. I stored the output of the call into a variable and than used [jq](https://stedolan.github.io/jq/) to parse the JSON output into 2 other variables. One was for storing the result from the success response and the other one was for storing the error message. 

After that I looked up how to send email with the shell and looked at [mail](https://www.cyberciti.biz/faq/linux-unix-bash-ksh-csh-sendingfiles-mail-attachments/) command. It was nice but in order to send it to the real world I needed something else (or at least I didn't know how to configure it to do this). So I settled with [sSMTP](https://www.cyberciti.biz/tips/linux-use-gmail-as-a-smarthost.html) which allowed me to send emails to myself if there was a problem with updating the DNS record. 


You can checkout the full source code on my github:

{% github gigili/Cloudflare-dns-update %}