Community discussions

MikroTik App
 
idlemind
Forum Guru
Forum Guru
Topic Author
Posts: 1146
Joined: Fri Mar 24, 2017 11:15 pm
Location: USA

DHCP Lease-Script to Maintain DNS Cache

Wed Apr 05, 2017 4:52 pm

I've been looking at different scripts to keep a local DNS cache up to date based on DHCP leases. While I think this should "just work" like OpenWRT does with dnsmasq I need a solution now.

Looking at multiple options put together by the community the missing detail for me is how to best install the lease-script. It looks like if you paste it into the device over SSH you have to manually add your \r\n line breaks and escape out quotes. Is there a better way to upload a script and then target it as a file when specifying the lease-script?
 
FIPTech
Long time Member
Long time Member
Posts: 558
Joined: Tue Dec 22, 2009 1:53 am

Re: DHCP Lease-Script to Maintain DNS Cache

Wed May 03, 2017 4:23 pm

Here is a script i did to update the DNS static entries from DHCP leases. You need to put it in a dhcp script lease. I have no answer for your script upload question.

Router OS 6.39 at least is needed to get it working.

Be careful if you use it on a large DNS set as it could remove entries. It is designed to keep a clean local DNS set without addresses or domain names duplicates.

A problem using such a DHCP lease script, is that it will only trig for client bounds. This mean that if a client does only renew, and the domain name of the client has been modified in between, the DNS will not be updated. Even if it does renew at the end of the lease period, the script will not execute.

It would be nice if the lease script could trig for other DHCP events, not only bound (renew, release...) so that we can update and remove a DNS entry when there are DHCP lease modifications or releases.

Let me know if you find a solution to this problem.
# Write DNS static record for each DHCP lease

:local topdomain;
:local FullHostName;
:local NoUpdate false;


# Configure your domain
:set topdomain "yourdomain.com";

:if ($leaseBound = 1) do={

:set FullHostName ($"lease-hostname" . "." . $topdomain)

/ip dns static;

:foreach n in [find] do={
# If a static DNS entry is the same as the lease then leave it and mark to not Update it
:if (([get $n name] = $"FullHostName") and ([get $n address] = $leaseActIP)) do={
:set NoUpdate true;
} else={
# If some DNS entry with same fully qualified domain name or same address already exist remove it
:if (([get $n name] = $"FullHostName") or ([get $n address] = $leaseActIP)) do={
   :log info ("Removing from Static DNS : " . [get $n name] .  " @ " . [get $n address]);
    remove $n;
  }
 }
}
# Add new Static DNS Entry if necessary

:if ($NoUpdate = false) do={
:log info ("Adding to Static DNS : " . $"FullHostName" .  " @ " . $leaseActIP);
add name=($"lease-hostname" . "." . $topdomain) address=$leaseActIP;
 }
 
}
Last edited by FIPTech on Wed May 03, 2017 5:32 pm, edited 1 time in total.
 
idlemind
Forum Guru
Forum Guru
Topic Author
Posts: 1146
Joined: Fri Mar 24, 2017 11:15 pm
Location: USA

Re: DHCP Lease-Script to Maintain DNS Cache

Wed May 03, 2017 4:31 pm

Thanks, what's the most effective way to "install" this in RouterOS. Preferably at the command line. I imagine you can't just copy and paste it in correct?
 
FIPTech
Long time Member
Long time Member
Posts: 558
Joined: Tue Dec 22, 2009 1:53 am

Re: DHCP Lease-Script to Maintain DNS Cache

Wed May 03, 2017 5:26 pm

Yes i think you can just copy and past this inside the winbox dhcp server lease script window.

I've just made a new try, what i said in the previous message is not fully exact.

In fact the script does execute for a DHCP release. Here is a new version of the script, with DNS erase capability when a DHCP release is issued by the client.
# DNS record for DHCP lease

:local topdomain;
:local FullHostName;
:local NoUpdate false;


# Configure your domain
:set topdomain "yourdomain.com";

:set FullHostName ($"lease-hostname" . "." . $topdomain)

/ip dns static;

:if ($leaseBound = 1) do={

:foreach n in [find] do={
# If a static DNS entry is the same as the lease then leave it and set a flag to avoid DNS Update
:if (([get $n name] = $"FullHostName") and ([get $n address] = $leaseActIP)) do={
:set NoUpdate true;
} else={
# If some DNS entry with same fully qualified domain name or same address already exist remove it
:if (([get $n name] = $"FullHostName") or ([get $n address] = $leaseActIP)) do={
   :log info ("Removing from Static DNS : " . [get $n name] .  " @ " . [get $n address]);
    remove $n;
  }
 }
}
# Add new Static DNS Entry if necessary
:if ($NoUpdate = false) do={
:log info ("Adding to Static DNS : " . $"FullHostName" .  " @ " . $leaseActIP);
add name=($"lease-hostname" . "." . $topdomain) address=$leaseActIP;
 }
# remove DNS entry at DHCP release :
} else={
:log info ("Clear static DNS entry at DHCP Release : " . $"FullHostName" . " @ " . $leaseActIP);
:foreach n in [find] do={
:if (([get $n name] = $"FullHostName") and ([get $n address] = $leaseActIP)) do={
remove $n;
  }
 }
}
 
idlemind
Forum Guru
Forum Guru
Topic Author
Posts: 1146
Joined: Fri Mar 24, 2017 11:15 pm
Location: USA

Re: DHCP Lease-Script to Maintain DNS Cache

Wed May 03, 2017 7:24 pm

Thanks, no way to easily paste it in outside of WinBox huh?
 
FIPTech
Long time Member
Long time Member
Posts: 558
Joined: Tue Dec 22, 2009 1:53 am

Re: DHCP Lease-Script to Maintain DNS Cache

Wed May 03, 2017 8:48 pm

you could use Webfig as well to copy paste the script.

Then if you need it for other routers, export the DHCP config using an SSH or Telnet console :

/ip dhcp-server

export file=dhcp-config.rsc

Get this file through ftp for example, edit it to remove unuseful things with Notepad++ or similar editor, and copy it to other routers.

Then you can import it again with a Telnet or SSH console :

import file=dhcp-config.rsc


Like this you'll not have to manually escape the lease-script. The export/import will do it for you.
 
idlemind
Forum Guru
Forum Guru
Topic Author
Posts: 1146
Joined: Fri Mar 24, 2017 11:15 pm
Location: USA

Re: DHCP Lease-Script to Maintain DNS Cache

Wed May 03, 2017 10:55 pm

Thanks I'll give that a whirl. Is the .rsc file a UTF8 text file? I'm going to hop in WebFig and see if I can import it there otherwise it's time for me to launch WinBox I suppose.
 
FIPTech
Long time Member
Long time Member
Posts: 558
Joined: Tue Dec 22, 2009 1:53 am

Re: DHCP Lease-Script to Maintain DNS Cache

Thu May 04, 2017 3:15 am

Seems to be similar to CSS Unicode backlash escaped (Hexa) encoding. So in fact the underlying encoding is ASCII.

For example "é" become \E9.
 
reddot
just joined
Posts: 1
Joined: Wed Sep 22, 2021 6:06 pm

Re: DHCP Lease-Script to Maintain DNS Cache

Wed Sep 22, 2021 6:12 pm

A problem using such a DHCP lease script, is that it will only trig for client bounds. This mean that if a client does only renew, and the domain name of the client has been modified in between, the DNS will not be updated. Even if it does renew at the end of the lease period, the script will not execute.

It would be nice if the lease script could trig for other DHCP events, not only bound (renew, release...) so that we can update and remove a DNS entry when there are DHCP lease modifications or releases.
Recently I've contacted Mikrotik support team with a proposal to implement triggering of a lease-script on renewals.
I've got a reply stating that functionality may be implemented if more similar support requests are received.

So please send a message to support team if you're interested in such a fix in triggering of lease-scripts.

Who is online

Users browsing this forum: No registered users and 21 guests