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.

http://wiki.mikrotik.com/wiki/Setting_static_DNS_record_for_each_DHCP_lease

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/


Did you like this article?


0 Shares:
You May Also Like

Hard times for hard drives: US may ban popular imports

This could be a damaging blow to quite a few companies within the United States. For instance, take a dedicated server hosting company that relies on one of many hardware vendors like Dell or HP (Hewlett-Packard). Its possible that said vendor will have stock pile of hard drives. Put lets put this into perspective. If you read the entire article you will see a similar situation that ended with Broadcom filing a complaint against Qualcomm. The International Trade Commission banned Qualcomm from shipping any new chips and hardware into the United Sates, and any chips that were already being shipped to the United States could continue. The ban lasted for 45 days, until an appeal by Qualcomm was successful and then the ban was lifted.
Read More

Linksys WRT54G and WRT54GL Being Phased Out?

I went to pick up a Wireless Router from a local computer shop http://www.a-power.com in hopes of grabbing a WRT54GL or at least a WRT54GS. The main reason being that I could flash the hardware to run DD-WRT http://www.dd-wrt.com which is a third part open source router firmware.
Read More

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.

http://groups.google.com/group/linux.debian.bugs.dist/browse_thread/thread/3de22bc415d3da02?pli=1

Read More