Community discussions

MUM Europe 2020
 
dse
just joined
Topic Author
Posts: 1
Joined: Fri Mar 10, 2017 9:33 pm

Yet another DHCP to DNS script

Fri Mar 10, 2017 10:51 pm

This script intended to run as a DHCP server lease script and it manages (registers or removes) static DNS entries in accordance to DHCP lease allocation/expiration.

The script registers only fully qualified domain names (FQDN). Host part of the registering FQDN is the value of the "host-name" property of the lease or the "comment" property if the "host-name" is empty. Domain part is the value of the "domain" property of the corresponding DHCP server network. The script doesn't register DNS entries for invalid FQDNs with empty or undefined host or domain parts. Also the script doesn't register DNS entry when the entry with the same IP address or the FQDN already exists in the router's static DNS database.

Static DNS entries, managed by the script, have the tag #DHCP as a comment to distinguish them from the manually created ones.

The TTL of registering DNS entry is equal to the TTL of the corresponding DHCP lease.

On the DHCP lease expiration the script removes corresponding DNS entry with the same IP address, tagged as #DHCP in the comment.

The script logs various errors and informational messages with the tag DHCP2DNS. The logged messages are self-explanatory.

The script should be created as the standard system script. For the script to run for the given DHCP server, it's name should be assigned to the "lease-script" property of this server.

Source code follows.
:local DHCPtag
:set DHCPtag "#DHCP"

:if ( [ :len $leaseActIP ] <= 0 ) do={ :error "empty lease address" }

:if ( $leaseBound = 1 ) do=\
{
  :local ttl
  :local domain
  :local hostname
  :local fqdn
  :local leaseId
  :local comment

  /ip dhcp-server
  :set ttl [ get [ find name=$leaseServerName ] lease-time ]
  network 
  :set domain [ get [ find $leaseActIP in address ] domain ]
  
  .. lease
  :set leaseId [ find address=$leaseActIP ]

# Check for multiple active leases for the same IP address. It's weird and it shouldn't be, but just in case.

  :if ( [ :len $leaseId ] != 1) do=\
  {
   :log info "DHCP2DNS: not registering domain name for address $leaseActIP because of multiple active leases for $leaseActIP"
   :error "multiple active leases for $leaseActIP"
  }  

  :set hostname [ get $leaseId host-name ]
  :set comment [ get $leaseId comment ]
  /

  :if ( [ :len $hostname ] <= 0 ) do={ :set hostname $comment }

  :if ( [ :len $hostname ] <= 0 ) do=\
  {
    :log error "DHCP2DNS: not registering domain name for address $leaseActIP because of empty lease host-name or comment"
    :error "empty lease host-name or comment"
  }
  :if ( [ :len $domain ] <= 0 ) do=\
  {
    :log error "DHCP2DNS: not registering domain name for address $leaseActIP because of empty network domain name"
    :error "empty network domain name"
  }

  :set fqdn "$hostname.$domain"
  
  /ip dns static
  :if ( [ :len [ find name=$fqdn and address=$leaseActIP and disabled=no ] ] = 0 ) do=\
  {
    :log info "DHCP2DNS: registering static domain name $fqdn for address $leaseActIP with ttl $ttl"
    add address=$leaseActIP name=$fqdn ttl=$ttl comment=$DHCPtag disabled=no
  } else=\
  {
    :log error "DHCP2DNS: not registering domain name $fqdn for address $leaseActIP because of existing active static DNS entry with this name or address" 
  }
  /
} \
else=\
{
  /ip dns static
  :local dnsDhcpId 
  :set dnsDhcpId [ find address=$leaseActIP and comment=$DHCPtag ]

  :if ( [ :len $dnsDhcpId ] > 0 ) do=\
  {
    :log info "DHCP2DNS: removing static domain name(s) for address $leaseActIP"
    remove $dnsDhcpId
  }
  /
}
 
phin
just joined
Posts: 15
Joined: Mon Dec 04, 2017 11:25 pm

Re: Yet another DHCP to DNS script

Sun Feb 11, 2018 6:25 am

This is exactly what I was looking for. Thanks. Works as expected.
 
User avatar
emk2203
just joined
Posts: 10
Joined: Tue Feb 07, 2017 11:33 pm
Location: Germany

Re: Yet another DHCP to DNS script

Wed Aug 15, 2018 3:06 pm

Much better script than all the predecessors. This should be in the wiki.
RB 750G r3 + RB wAP G-5HacT2HnD
 
xrlls
just joined
Posts: 3
Joined: Sun Jan 13, 2019 4:43 pm

Re: Yet another DHCP to DNS script

Sun Jan 13, 2019 8:04 pm

Excellent! Just what I needed!
 
dca1
just joined
Posts: 2
Joined: Sat Aug 31, 2019 2:43 pm

Re: Yet another DHCP to DNS script

Sat Aug 31, 2019 3:01 pm

Can someone point me at what I'm doing wrong here? I've placed this script in the 'Lease Script' section of the DCHP Server setup window. I am watching my leases renew but I am now seeing anything being added to static DNS entries. I also tried adding it as a script and adding /system script run dhcptodns as the Lease Script. I just don't kinow how this is set up..
 
dca1
just joined
Posts: 2
Joined: Sat Aug 31, 2019 2:43 pm

Re: Yet another DHCP to DNS script

Sun Sep 01, 2019 1:46 pm

Answered my own question in the end. Script was erroring as I did not have local domain set. Set that and all good. Thanks a lot.
 
revamp
just joined
Posts: 2
Joined: Tue Dec 03, 2019 2:08 pm

Re: Yet another DHCP to DNS script

Tue Dec 03, 2019 2:20 pm

Need help. I just moved to MikroTik and this post is exactly what I needed.
After adding the script I can see in the logs the FQDN names are set correctly in the log.

However from the clients, the ping fails with FQDN (ping on just hostname works fine)

Here is my setup - ether4 (10.9.7.0/24) has the script assigned and domain "rev.local"
I have 2 windows machines with static leases
M1 - 10.9.7.11
M2 - 10.9.7.12

Machine are getting correct IP's, however their DNS server is 9.9.9.9 (which is set on the wan interface)

When I ping from M1
--> ping M2.rev.local --> cannot resolve....
--> ping M2 --> gets 10.9.7.2 and works fine
Same from M2

However, from the winBox terminal both M1 and M2 work fine with M1.rev.local and M2.rev.local

One more observation. there is M3 which does not have static lease (gets address 10.9.8.131) - can ping to M1, M2 but not M1.rev.local

Am I missing anything...
 
mniewiera
just joined
Posts: 6
Joined: Wed Dec 27, 2017 4:52 pm

Re: Yet another DHCP to DNS script

Tue Dec 03, 2019 7:31 pm

From what i understand your problem is that the machines (m1, m2 and m3) use 9.9.9.9 as DNS Servers.
They should use the mikrotik as DNS Server. This way they can resolve m1.rev.local and DNS requests for public domains (for example google.com) is forwarded to 9.9.9.9.

I guess that you can even ping m1 or m2 but not .rev.local is coming from the WINS Service. (In this case i guess you are using windows as operating system?)

You can change what DNS Server they are getting in the DHCP section.
 
revamp
just joined
Posts: 2
Joined: Tue Dec 03, 2019 2:08 pm

Re: Yet another DHCP to DNS script

Tue Dec 03, 2019 7:47 pm

Thanks for the reply.

The Windows machines are set to get DNS settings dynamically from DHCP settings
WINS yes, but in my earlier DD-WRT, same worked perfectly fine.

I need to study how to setup the internal DNS (any pointers).
 
mniewiera
just joined
Posts: 6
Joined: Wed Dec 27, 2017 4:52 pm

Re: Yet another DHCP to DNS script

Tue Dec 03, 2019 10:04 pm

I think your DNS is working fine because it worked from winbox.
So the DNS Server set on your client machines is set to 9.9.9.9?
If so, then it just can't work. Because 9.9.9.9 can't resolve your internal DNS entries. Only the mikrotik can.
If the DNS is set to 9.9.9.9, and this setting is coming from the DHCP Server, then you have to change what is assigned to them.
I guess your DHCP is also the mikrotik? If so then you have to change the configuration on the DHCP Server in the mikrotik. Since i'm currently not within reach of a mikrotik device i can't tell you where to go right now. But in the mikrotik wiki should be some helpful article.
So you (at least currently) don't have to study how DNS works. DHCP is where your problem is in my opinion.

Who is online

Users browsing this forum: No registered users and 8 guests