I’m very new to MikroTik (first install today): Is there and easy way to have DNS entries set from DHCP?
That is, if I set a hostname in a DHCP range of “corp.com” and a client ends up with a hostname of “pc-1” – I should be able to query the router’s DNS for “pc-1.corp.com”.
Having used a feature like this with dnsmasq, I too was surprised when I set up my first Mikrotik router 2-3 years ago that I couldn’t find a built-in way to do this. I did a lot of searching at the time, but still I was hoping that I had just overlooked it somehow.
I haven’t looked into ROS scripting details yet - so I may be way wrong here - but from what I can see, I’d expect the scripts to have two main disadvantages, as compared to the dnsmasq feature:
I assume that using the scheduler means that the scripts would be executed periodically, rather than being triggered by events like a DHCP lease for a machine being created or timing out.
The first two scripts appear to remove all the DHCP entries and re-create the list every time they run. If the tables are stored on flash, there could be a lot of extra erase/write cycles for (typically) the majority of polls where nothing changes, and reducing the polling period in order to increase responsiveness would make that worse. Also I wonder if there are instants while the script is running that there are no DHCP entries, causing local look-ups to fail. The third one looks a bit more clever - I don’t remember seeing that one back when I looked at them the first time.
I use a script for this which I am using with with 6.0rc9 and is very simple to implement. Just create a new script with none of the checkbox’s selected and paste in the following code:-
# Domain to be added to your DHCP-clients hostname
:local topdomain;
:set topdomain "corp.com";
# Use ttl to distinguish dynamic added DNS records
:local ttl;
:set ttl "00:59:59";
# Set variables to use
:local hostname;
:local hostip;
:local free;
# Remove all dynamic records
/ip dns static;
:foreach a in=[find] do={
:if ([get $a ttl] = $ttl) do={
:put ("Removing: " . [get $a name] . " : " . [get $a address]);
remove $a;
}
}
/ip dhcp-server lease ;
:foreach i in=[find] do={
/ip dhcp-server lease ;
:if ([:len [get $i host-name]] > 0) do={
:set free "true";
:set hostname ([get $i host-name] . "." . $topdomain);
:set hostip [get $i address];
/ip dns static ;
# Check if entry already exist
:foreach di in [find] do={
:if ([get $di name] = $hostname) do={
:set free "false";
:put ("Not adding already existing entry: " . $hostname);
}
}
:if ($free = true) do={
:put ("Adding: " . $hostname . " : " . $hostip ) ;
/ip dns static add name=$hostname address=$hostip ttl=$ttl;
}
}
}
Then set up a schedule with an interval of 30 minutes and on event run
/system script run dhcpHostToDNS
Change the name to whatever you called the script and select the following options to read, write, policy and test.
This has successfully worked for me for over a year. I can’t take credit for the script as I did not write it and can’t remember the source. Once you have run it you should be able to go in to the static dns entries and see the entries that have been created.