mikrotik:scripting:private_dyndns

Privately owned dyndns server (sort of) and Mikrotik

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

deviceupdate.php
<?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

dyndns.sh
#!/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
  1. The router tries to access the specific web page on the web server that is located on the DNS server.
  2. Web server reads the IP from the router and if the hash is ok, it writes the IP into two files in the same folder
    1. One file for history purposes (with date and everything)
    2. One file with IP address only
  3. Cron runs a script that calculates the MD5 hash from the file containing only the IP address and compares it to the MD5 hash from before the change. If the two are different – it updates the zone file by replacing the one record and saves the new hash to a file for future comparison (so we don’t update and reload the zone all the time
  4. Zone is reloaded and propagated.
Enter your comment:
43 +9 =
 
  • mikrotik/scripting/private_dyndns.txt
  • Last modified: 2019/10/31 09:05
  • by 127.0.0.1