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

Boeing’s unmanned A160T Hummingbird helicopter takes flight

Not the first Unmanned aerial vehicle (UAV) made. But the Boeing A160HT Humming Bird should be one of the best once further tests and simulation in combat missions is completed.
Boeing's unmanned A160T Hummingbird helicopter takes flight -

Filed under:


It's been a few ticks since we've seen Boeing send an atypical aircraft into the friendly skies, but the firm's latest helicopter has successfully completed a 12-minute test flight without so much as a pilot on board. The A160T Hummingbird unmanned rotorcraft is a turbine-powered "warfighter" that aims to provide "intelligence, surveillance, and reconnaissance coverage" in locales that could make even the most calloused veteran queasy. During its time in the air, it reportedly met every objective set for it, and while we've no idea how soon this thing will be lifting itself up, it'll eventually reach speeds of up to 140 knots and stay airborne for up to 20 hours before returning to base for a pat on the wing.

[Via The Raw Feed, image courtesy of SkyControl]

 

Read | Permalink | Email this | Comments


Office Depot Featured Gadget: Xbox 360 Platinum System Packs the power to bring games to life!

[EnGadget]
Read More