If you constantly connect to your home network (as I do), you have, on occasion, experienced that frustrating moment when the dynamic dns service is down for whatever reason. Let’s assume that you are a person who likes to have more controll over the services you use, and have setup your private DNS server on a cheap VPS like DigitalOcean. If so, you’re at the right place.
For this to work, we will need a smarter router that can fetch http data – Mikrotik in my case, and a private DNS server on the public network hosting your domain and under your control. If you can’t access the shell, then you can’t complete this tutorial. Note that this isn’t very secure and I will be modifying the process to get a secure solution
Setup a web server on your DNS server Create a php update page
<?php $ip=$_SERVER[REMOTE_ADDR]; if ($_GET['hash']=="ae2b1fca515949e5d54fb22b8ed95575") { file_put_contents("deviceupdate.log","DATE: ".date("Y-m-d H:i:s")." IP: ".$ip."\n",FILE_APPEND | LOCK_EX); file_put_contents("homeip.log",$ip,LOCK_EX); print "DATE: ".date("Y-m-d H:i:s")." IP: ".$ip; } ?>
Setup Mikrotik schetuler that runs every few minutes
/tool fetch keep-result=no mode=http url="http://www.my-dns-servers-web-server.com/mikrotik/deviceupdate.php?hash=ae2b1fca515949e5d54fb22b8ed95575"
Create a shell script called dyndns.sh to update the DNS zone
#!/bin/bash newip=$(cat /var/www/ip/doma.ip) md5old=$(cat /var/www/ip/doma.md5) md5new=$(md5sum /var/www/ip/doma.ip | awk '{print $1}') if test "$md5old" = "$md5new" then echo "$(date) - No change" >> /var/log/dyndns else sed -i "s/\t.*\t; Serial$/\t$(date +%Y%m%d)00\t; Serial/" myfqdn.eu.zone sed -i "s/^doma\t.*/doma\t\t300\tIN\tA\t$newip/" myfqdn.eu.zone rndc reload myfqdn.eu md5sum /var/www/ipeu/doma.ip | awk '{print $1}' > /var/www/ipeu/doma.md5 echo "$(date) - Updated IP $newip" >> /var/log/dyndns fi
Create a cron job to run every few minutes. Run crontab -e and type
*/5 * * * * /scripts/dyndns.sh