Picking a DHCP hostname to static DNS script

I see there are a few different scripts for setting-up and tearing-down static DNS names from hostnames from the DHCP server. I found some on the MikroTik wiki, and I found some in the forum here, and I found some at various web pages. I’m not sure what the merits of each are.

I will at some point possibly be setting up remote DNS forwarding from a remote site running a MikriTik unit, to a home site running an Untangle box. I was planning on using a script generated here: https://blog.johannfenech.com/?p=536

So I would like a script that maintains a the local static DNS entries that is compatible with the method above. Perhaps they’re all compatible?

Are some of the DHCP hostname to DNS scripts more popular than others?

And final question, if you’re still reading this: Do these scripts just run on a timed basis, or are they set to run based on some event on the DHCP server or something? Seems like if the DHCP server is doing “work,” that right after that would be the time to call the DNS script?

Thanks in advance for any insight.

I wonder how to measure the popularity of various scripts doing the same thing given that one part of users write them themselves (so doesn’t even know some were published) and the other part just takes the first one uncle Google suggests. So you seem to represent the third one who studies the reviews :smiley:

To answer your question, /ip dhcp-server has a parameter lease-script (and, maybe surprisingly, the script is called both when the lease is granted and when it expires, so you have to check the manual to see how to detect at which event the script is called), and /ip dhcp-client has a parameter script (and the script is also called both when the lease is assigned and when it ends).

Compatibility betwen selective DNS forwarding and static DNS records does not depend on the script which copies the hostnames from leases to the static records but on the order of search of the two. As static records are consulted before sending a query outside, it depends on which DNS queries you redirect:

  • if you redirect client’s queries as they enter Mikrotik, they will skip the static records in Mikrotik because they will be forwarded to the external DNS without Mikrotik’s embedded DNS server to even see them
  • if you redirect the query which Mikrotik sends because a client asks for a resolution of something that is not available in the cache of Mikrotik’s embedded DNS cache, the static routes will win

.
Implementing redirection of Mikrotik’s own DNS queries is not exactly simple.

One tip, hostname updating scripts based on timer are most likely written for old RouterOS, where DHCP lease script didn’t exist. I’d avoid them, because it’s waste of resources to check again and again, even though nothing changed.

And about linked config generator for DNS redirection, it will work, but:

  • can have many false positives, find some inspiration for better regexp in this thread
  • checking tcp connections is useless, because when it finds the query in packet, connection is already established and can’t be redirected anymore

Alright much to think about.

Can anyone point me to scripts that use the DHCP Lease Script option instead of being run at scheduled intervals?

Found one. Googled “mikrotik leasebound dns” after reading the manual’s section on DHCP lease scripts and seeing leasebound was a passed global.

https://gist.github.com/dgholz/36543905cd0dc872f29eb207feb0eb52

DHCP server allows to set script that gets called when address is (de)assigned. Example of such script (I didn’t test it myself, but it looks good):
Yet another DHCP to DNS script

I’m not saying that the ones called from scheduler are necessarily bad. But DHCP’s lease script has advantages, because it runs only when something actually changes, and the update happens immediately. With scheduler, you need to run the script all the time, and there’s always some delay between address assignment and DNS update.