MikroTik – Automatically creating DNS record for each DHCP lease/client

You’ll notice that when you first get your MikroTik router, that it doesn’t create DNS records for each DHCP client that successfully receives a lease. The below article provides the necessary script to create a DNS record for each DHCP Client. I’ve provided a link to the Mikrotik Wiki article so you can see the original source.


The script does need to be modified (white spaces taken out) so that it will run correctly on the MikroTik. I’ve done this already and provided the script below. Also, make sure that you change :local zone “local”; to your network domain name or dns search suffix.

You will also have to create this script via telnet/ssh as webfig will mess up line 11 and remove the “\\” and leave “\”. If you don’t know how to add a script via telnet/ssh, simply type system script add name=”dhcp-dns” soruce={ at which point you can then paste the script content below. You will then have to complete the line by entering in again.

Viola! Execute the script to ensure it runs without errors system scripts run name=dhcp-dns. Lastly we want to run this script often, to ensure the records are created and updated. Lets set a schedule run system scheduler add name=dhcp-dns-run interval=5m on-event=dhcp-dns and now the script should be running every 5 minutes. To confirm the scheduler is working, wait 5 minutes and then run system scheduler print and look at the “RUN-COUNT” value, which should be greater than 0.

This script is now in GitHub and can be seen here https://github.com/jordantrizz/mikrotik-scripts/blob/master/dhcp-dns

:local zone "local";
:local ttl "00:05:00"
:local hostname
:local ip
:local dnsip
:local dhcpip
:local dnsnode
:local dhcpnode

/ip dns static;
:foreach i in=[find where name ~ (".*\\.".$zone) ] do={
  :set hostname [ get $i name ];
  :set hostname [ :pick $hostname 0 ( [ :len $hostname ] - ( [ :len $zone ] + 1 ) ) ];
  /ip dhcp-server lease;
  :set dhcpnode [ find where host-name=$hostname ];
  :if ( [ :len $dhcpnode ] > 0) do={
    :log debug ("Lease for ".$hostname." still exists. Not deleting.");
  } else={
# there's no lease by that name. Maybe this mac has a static name.
    :local found false
    /system script environment
    :foreach n in=[ find where name ~ "shost[0-9A-F]+" ] do={
       :if ( [ get $n value ] = $hostname ) do={
         :set found true;
    :if ( found ) do={
      :log debug ("Hostname ".$hostname." is static");
    } else={
      :log info ("Lease expired for ".$hostname.", deleting DNS entry.");
      /ip dns static remove $i;

/ip dhcp-server lease;
:foreach i in=[find] do={
  :set hostname ""
  :local mac
  :set dhcpip [ get $i address ];
  :set mac [ get $i mac-address ];
  :while ($mac ~ ":") do={
    :local pos [ :find $mac ":" ];
    :set mac ( [ :pick $mac 0 $pos ] . [ :pick $mac ($pos + 1) 999 ] );
  :foreach n in=[ /system script environment find where name=("shost" . $mac) ] do={
    :set hostname [ /system script environment get $n value ];
  :if ( [ :len $hostname ] = 0) do={
    :set hostname [ get $i host-name ];
  :if ( [ :len $hostname ] > 0) do={
    :set hostname ( $hostname . "." . $zone );
    /ip dns static;
    :set dnsnode [ find where name=$hostname ];
    :if ( [ :len $dnsnode ] > 0 ) do={
# it exists. Is its IP the same
      :set dnsip [ get $dnsnode address ];
      :if ( $dnsip = $dhcpip ) do={
        :log debug ("DNS entry for " . $hostname . " does not need updating.");
      } else={
        :log info ("Replacing DNS entry for " . $hostname);
        /ip dns static remove $dnsnode;
        /ip dns static add name=$hostname address=$dhcpip ttl=$ttl;
    } else={
# it doesn't exist. Add it
      :log info ("Adding new DNS entry for " . $hostname);
      /ip dns static add name=$hostname address=$dhcpip ttl=$ttl;



*UPDATE 10/01/2014* A much better script has been created by Tyler and is available at http://www.tolaris.com/2014/09/27/synchronising-dhcp-and-dns-on-mikrotik-routers/

You May Also Like

Apples iPhone Firmware 2.0.2 Causes More Issues Than It Fixes

I just saw this article on Slashdot... Slashdot | Apple's IPhone 3G Firmware Update Bombs Yahoo posted the news article... Apple's iPhone 3G Firmware Update Bombs (NewsFactor) by NewsFactor: Yahoo! Tech Both articles comment on the 2.0.1 firmware. However if you read the original article, it speaks about the new firmware which is 2.0.2 and was released on the 18th. NewsFactor Network | Apple Reportedly Plans Another Fix for the iPhone Either way, I don't have problems with my phone aside from MobileChat crashing and other Applications as well.

Fastcgi and the dreaded aborted: select() failed

I was getting reports of "500 Internal Server Errors" on two of my web servers. And after further investigation it looks like it was related to the following error.

(4)Interrupted system call: FastCGI: comm with server "php-fastcgi.fcgi" aborted: select() failed

After doing some reasearch I stumbled upon the following patch.