Community discussions

MikroTik App
 
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: 19
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: 11
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.
 
xrlls
newbie
Posts: 42
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: 4
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: 4
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: 7
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: 7
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.
 
Ameeno
just joined
Posts: 22
Joined: Sun Apr 29, 2018 1:25 am

Re: Yet another DHCP to DNS script

Mon Feb 03, 2020 8:33 am

hello, is this broken?

I receive the following error when script is run:

"empty lease address"

this seems to relate to the value of :if ( [ :len $leaseActIP ] <= 0 ) do={ :error "empty lease address" }


perhaps Mikrotik has changed/removed/renamed this variable?
 
User avatar
willianwrm
just joined
Posts: 15
Joined: Mon Jun 06, 2016 8:54 pm
Contact:

Re: Yet another DHCP to DNS script

Wed Apr 15, 2020 7:41 pm

I receive the following error when script is run:

"empty lease address"

It's working fine in the long-term version 6.45.8.
You need to put this script in the Lease Script of the DHCP Server, it will be called automatically by new leases.

One thing I did notice is that the script does not run at renew address (after expiration time) but only when the host request a new lease.
 
sebaz
just joined
Posts: 1
Joined: Mon Mar 23, 2020 11:37 am

Re: Yet another DHCP to DNS script

Sun Apr 26, 2020 3:48 pm

I'm posting this since it might help someone. On my RouterOS v6.46.5 I was having problems with script also where in the log I could see messages like
DHCP2DNS: not registering domain name for address xxx.yyy.www.zzz because of empty lease host-name or comment
for some of the devices.

It turns out hostname was not read with following call
:set hostname [ get $leaseId host-name ]
so my solution was to add
:if ( [ :len $hostname ] <= 0 ) do={ :set hostname $"lease-hostname" }
line so you have following in script:
:if ( [ :len $hostname ] <= 0 ) do={ :set hostname $comment }

:if ( [ :len $hostname ] <= 0 ) do={ :set hostname $"lease-hostname" }
 
RackKing
Member
Member
Posts: 380
Joined: Wed Oct 09, 2013 1:59 pm

Re: Yet another DHCP to DNS script

Thu Jul 02, 2020 7:32 pm

Anyone have an updated version of this? It is not working for me on 6.47. Thanks.
 
User avatar
jbl42
Member Candidate
Member Candidate
Posts: 214
Joined: Sun Jun 21, 2020 12:58 pm

Re: Yet another DHCP to DNS script

Sun Jul 05, 2020 10:50 pm

Thanks for the script. This should be in the official docs.

I created a slightly modified version:
  • Strip spaces and \00 chars from DHCP lease host names before combining them with the domain to build DNS fqdn. Some DHCP clients, in my case smart Zyxel switches, register with host names with spaces and trailing \00 chars (this is most likely a Zyxel FW bug)
  • Generate a hostname for DHCP clients registering with empty host names to create a static DNS A record for all DHCP clients. Most Android devices since Version 8 do not provide host names with DHCP reqs. I like to have working DNS for all clients
  • Make log prefix configurable
:local DHCPtag   "#*# Created by DHCP2DNS #*#"
:local LogPrefix "DHCP2DNS ($leaseServerName)"

###
# Functions

# remove \0 and spaces from string passed as inStr=<string>
:local trimString do=\
{
  :local outStr
  :for i from=0 to=([:len $inStr] - 1) do=\
  {
    :local tmp [:pick $inStr $i];
    :if (($tmp !=" ") and ($tmp !="\00")) do=\
    {
      :set outStr ($outStr . $tmp)
    }
  }
  :return $outStr
}

# "a.b.c.d" -> "a-b-c-d" for IP addresses used as replacement for missing host names
:local ip2Host do=\
{
  :local outStr
  :for i from=0 to=([:len $inStr] - 1) do=\
  {
    :local tmp [:pick $inStr $i];
    :if ($tmp =".") do=\
    {
      :set tmp "-"
    }
    :set outStr ($outStr . $tmp)
  }
  :return $outStr
}

###
# Script entry point
#
# Expected environment variables:
# leaseBound         1 = lease bound, 0 = lease removed
# leaseServerName    Name of DHCP server
# leaseActIP         IP address of DHCP client

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

:if ( $leaseBound = 1 ) do=\
{
  # new DHCP lease added
  
  /ip dhcp-server
  :local ttl [ get [ find name=$leaseServerName ] lease-time ]
  network 
  :local domain [ get [ find $leaseActIP in address ] domain ]
  :set domain [ $trimString inStr=$domain ]

  .. lease
  :local 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 warning "$LogPrefix: Multiple active DHCP leases for '$leaseActIP' (???)"
    :error "Multiple active DHCP leases for '$leaseActIP' (???)"
  }  
  :local hostname [ get $leaseId host-name ]
  :set hostname [ $trimString inStr=$hostname ]

  :if ( [ :len $hostname ] <= 0 ) do=\
  {
    :set hostname [ $ip2Host inStr=$leaseActIP ]
    :log info "$LogPrefix: Empty hostname for '$leaseActIP', using generated host name '$hostname'"
  }
  :if ( [ :len $domain ] <= 0 ) do=\
  {
    :log warning "$LogPrefix: Empty domainname for '$leaseActIP', cannot create static DNS name"
    :error "Empty domainname for '$leaseActIP'"
  }

  :local fqdn ($hostname . "." .  $domain)

  /ip dns static
  :if ( [ :len [ find name=$fqdn and address=$leaseActIP and disabled=no ] ] = 0 ) do=\
  {
    add address=$leaseActIP name=$fqdn ttl=$ttl comment=$DHCPtag disabled=no
    :log info "$LogPrefix: Static domain name '$fqdn' created for '$leaseActIP' with ttl '$ttl'"
  }\
  else=\
  {
    :log warning "$LogPrefix: '$fqdn' already exists, cannot create static DNS name for '$leaseActIP'"
    :error "$LogPrefix: '$fqdn' already exists"
  }
}\
else=\
{
  # DHCP lease removed

  /ip dns static
  :local dnsDhcpId
  :set dnsDhcpId [ find address=$leaseActIP and comment=$DHCPtag ]
  :if ( [ :len $dnsDhcpId ] > 0 ) do=\
  {
    remove $dnsDhcpId
    :log info "$LogPrefix: Static DNS name(s) for '$leaseActIP' removed"
  }
}

For more flexibility, the above script can be stored in a /system script. This allows to use the same script from all DHCP servers to ease maintenance by putting a small wrapper script into the DHCP server lease script property:

:local scriptName "dhcp2dns.rsc"
:do {
  :local scriptSrc [ /system script get [ find name=$scriptName  ] source ]
  :local scriptObj [ :parse $scriptSrc ]
  $scriptObj leaseBound=$leaseBound  leaseServerName=$leaseServerName leaseActIP=$leaseActIP
} on-error={ :log warning "DHCP server '$leaseServerName' lease script error" };
Tested on RB4011 V6.47.1
Last edited by jbl42 on Sun Aug 09, 2020 4:44 pm, edited 1 time in total.
 
User avatar
HiltonT
Frequent Visitor
Frequent Visitor
Posts: 77
Joined: Mon Feb 07, 2011 4:24 am
Location: 'Srayamate
Contact:

Re: Yet another DHCP to DNS script

Sat Jul 11, 2020 10:25 am

Anyone have an updated version of this? It is not working for me on 6.47. Thanks.
I concur.
 
User avatar
Smoerrebroed
Frequent Visitor
Frequent Visitor
Posts: 75
Joined: Mon Feb 12, 2018 10:21 am

Re: Yet another DHCP to DNS script

Mon Sep 07, 2020 11:16 am

Good script! The only issue I had with it is that it will automatically assume the same lease-time for all (even static) leases, which might not be the case, so I changed
/ip dhcp-server
:set ttl [ get [ find name=$leaseServerName ] lease-time ]
network
to
/ip dhcp-server lease
:set ttl [ get [ find address=$leaseActIP ] lease-time ]
/ip dhcp-server network
Other than that, many thanks for contributing this!
 
User avatar
Smoerrebroed
Frequent Visitor
Frequent Visitor
Posts: 75
Joined: Mon Feb 12, 2018 10:21 am

Re: Yet another DHCP to DNS script

Thu Sep 10, 2020 1:57 pm

Okay, so I noticed that with my change, only leases that have their own lease-time set will be created in the DNS cache, so please use this instead:
    /ip dhcp-server lease
    :set ttl [ get [ find address=$leaseActIP ] lease-time ]
    :if ( [ :len $ttl ] <= 0 ) do={
        /ip dhcp-server
        :set ttl [ get [ find name=$leaseServerName ] lease-time ]
    }
 
nescafe2002
Forum Veteran
Forum Veteran
Posts: 897
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: Yet another DHCP to DNS script

Thu Sep 10, 2020 3:06 pm

Actually setting dns ttl equal to lease time doesn't make any sense and only leads to unexpected behavior especially for longer lease times..
 
neutronlaser
Member
Member
Posts: 445
Joined: Thu Jan 18, 2018 5:18 pm

Re: Yet another DHCP to DNS script

Sat Sep 12, 2020 9:56 pm

Someone post the latest and best script on wiki.
 
avggeek
newbie
Posts: 48
Joined: Thu Jun 06, 2013 9:33 am

Re: Yet another DHCP to DNS script

Tue Oct 20, 2020 8:22 am

I spent some time cleaning up the original script by @dse to incorporate the following:

Changelog
October 20,2020:
- IP to DNS name function implemented by @jbl42
- Replace illegal chars/uppercase letters using the functions provided by @sebastia in this forum thread
- Also changed the DNS entry removal logic to use a comment that is more likely to be unique across DHCP servers/devices (Credit to SmartFinn on GitHub)
- Added some logic to allow same MAC address to be registered under different DHCP servers (original script did not support this)
- Removed some logic checks for "leaseId" that seem to not work correctly
December 21,2020:
- Fix logic error relating to same MAC address appearing under different DHCP servers

Notes
- Script will look for domain name in the Domain field of the DHCP server "/ip dhcp-server network add domain"
- Script is only tested on ROS 6.47.4/6.47.7/6.49
# DNS TTL to set for DNS entries
:local dnsttl "00:15:00";

###
# Script entry point
#
# Expected environment variables:
# leaseBound         1 = lease bound, 0 = lease removed
# leaseServerName    Name of DHCP server
# leaseActIP         IP address of DHCP client
#leaseActMAC      MAC address of DHCP client
###

# "a.b.c.d" -> "a-b-c-d" for IP addresses used as replacement for missing host names
:local ip2Host do=\
{
  :local outStr
  :for i from=0 to=([:len $inStr] - 1) do=\
  {
    :local tmp [:pick $inStr $i];
    :if ($tmp =".") do=\
    {
      :set tmp "-"
    }
    :set outStr ($outStr . $tmp)
  }
  :return $outStr
}

:local mapHostName do={
# param: name
# max length = 63
# allowed chars a-z,0-9,-
  :local allowedChars "abcdefghijklmnopqrstuvwxyz0123456789-";
  :local numChars [:len $name];
  :if ($numChars > 63) do={:set numChars 63};
  :local result "";

  :for i from=0 to=($numChars - 1) do={
    :local char [:pick $name $i];
    :if ([:find $allowedChars $char] < 0) do={:set char "-"};
    :set result ($result . $char);
  }
  :return $result;
}

:local lowerCase do={
# param: entry
  :local lower "abcdefghijklmnopqrstuvwxyz";
  :local upper "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  :local result "";
  :for i from=0 to=([:len $entry] - 1) do={
    :local char [:pick $entry $i];
    :local pos [:find $upper $char];
    :if ($pos > -1) do={:set char [:pick $lower $pos]};
    :set result ($result . $char);
  }
  :return $result;
}

:local token "$leaseServerName-$leaseActMAC";
:local LogPrefix "DHCP2DNS ($leaseServerName)"

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

:if ( $leaseBound = 1 ) do=\
{
  # new DHCP lease added
  /ip dhcp-server
  #:local dnsttl [ get [ find name=$leaseServerName ] lease-time ]
  network
  :local domain [ get [ find $leaseActIP in address ] domain ]
  #:log info "$LogPrefix: DNS domain is $domain"

  :local hostname [/ip dhcp-server lease get [:pick [find mac-address=$leaseActMAC and server=$leaseServerName] 0] value-name=host-name]
  #:log info "$LogPrefix: DHCP hostname is $hostname"

 #Hostname cleanup
  :if ( [ :len $hostname ] <= 0 ) do=\
  {
    :set hostname [ $ip2Host inStr=$leaseActIP ]
    :log info "$LogPrefix: Empty hostname for '$leaseActIP', using generated host name '$hostname'"
  }
  :set hostname [$lowerCase entry=$hostname]
  :set hostname [$mapHostName name=$hostname]
  #:log info "$LogPrefix: Clean hostname for FQDN is $hostname";

  :if ( [ :len $domain ] <= 0 ) do=\
  {
    :log warning "$LogPrefix: Empty domainname for '$leaseActIP', cannot create static DNS name"
    :error "Empty domainname for '$leaseActIP'"
  }

  :local fqdn ($hostname . "." .  $domain)
  #:log info "$LogPrefix: FQDN for DNS is $fqdn"

    :if ([/ip dhcp-server lease get [:pick [find mac-address=$leaseActMAC and server=$leaseServerName] 0] ]) do={
      # :log info message="$LogPrefix: $leaseActMAC -> $hostname"
      :do {
        /ip dns static add address=$leaseActIP name=$fqdn ttl=$dnsttl comment=$token;
      } on-error={:log error message="$LogPrefix: Failure during dns registration of $fqdn with $leaseActIP"}
    }

} else={
# DHCP lease removed
  /ip dns static remove [find comment=$token];
}

In order to improve maintainability, I am using the wrapper script option that @jbl42 posted earlier in the thread
Last edited by avggeek on Fri Sep 23, 2022 12:58 pm, edited 3 times in total.
 
sebus
Frequent Visitor
Frequent Visitor
Posts: 67
Joined: Sun Mar 12, 2017 6:29 pm

Re: Yet another DHCP to DNS script

Sun Nov 08, 2020 2:11 pm

Why that is simply not build in RouterOS?

It is something one would just expect to be there!

Thanks

sebus
 
infabo
Long time Member
Long time Member
Posts: 585
Joined: Thu Nov 12, 2020 12:07 pm

Re: Yet another DHCP to DNS script

Thu Nov 12, 2020 12:12 pm

In order to improve maintainability, I am using the wrapper script option that @jbl42 posted earlier in the thread
The wrapper script just needs some adjustment, as it misses the parameter "leaseActMAC"
:local scriptName "dhcp2dns"
:do {
  :local scriptObj [:parse [/system script get $scriptName source]]
  $scriptObj leaseBound=$leaseBound leaseServerName=$leaseServerName leaseActIP=$leaseActIP leaseActMAC=$leaseActMAC
} on-error={ :log warning "DHCP server '$leaseServerName' lease script error" };
Last edited by infabo on Thu Nov 12, 2020 12:13 pm, edited 1 time in total.
 
ilium007
Member Candidate
Member Candidate
Posts: 206
Joined: Sun Jan 31, 2010 9:58 am
Location: Newcastle, Australia

Re: Yet another DHCP to DNS script

Sun Nov 29, 2020 3:08 pm

Is this script supposed to work with dhcp static leases? I have a couple of static leases that when acquired don’t add the DNS record.
 
avggeek
newbie
Posts: 48
Joined: Thu Jun 06, 2013 9:33 am

Re: Yet another DHCP to DNS script

Mon Dec 21, 2020 8:26 am

Is this script supposed to work with dhcp static leases? I have a couple of static leases that when acquired don’t add the DNS record.
Are you referring to DHCP Address bound to MAC addresses using the "Make Static" option? If so then my script should work with those sorts of entries - a lot of my VM's are configured that way and the DNS entries get added correctly.

Are you sure the script is not erroring out? Check the System>Log when you reboot a machine with a static lease. You might also want to uncomment some of the debug print statements in my script to see what's happening.
 
jonah1810
Frequent Visitor
Frequent Visitor
Posts: 98
Joined: Tue Jul 30, 2019 10:19 pm

Re: Yet another DHCP to DNS script

Wed Dec 23, 2020 3:21 am

I'm confused about the purpose of this script. why add dns entries for each user? shouldn't you just set up all users to the same 2-3 stable servers under dhcp-server/network for dns lookup?

Sent from my SM-G955W using Tapatalk

 
xinhood
just joined
Posts: 1
Joined: Sat Jan 02, 2021 5:49 pm

Re: Yet another DHCP to DNS script

Sat Jan 02, 2021 6:03 pm

Is this script supposed to work with dhcp static leases? I have a couple of static leases that when acquired don’t add the DNS record.
I had this issue, turned out that all my static entries were assigned to server "all". In my case that was because I added them in bulk when I moved to mikrotik and my script didn't assign a server.

This line was wat caused it to fail:
 :local hostname [/ip dhcp-server lease get [:pick [find mac-address=$leaseActMAC and server=$leaseServerName] 0] value-name=host-name]
I don't know a lot about this scripting language, so I can't fix the script without doing some research. So I assigned the proper server to all my DHCP entries.

The part
[find mac-address=$leaseActMAC and server=$leaseServerName] 
should say something like:
[find mac-address=$leaseActMAC and (server=$leaseServerName or server=all )]
but again, I don't know this scripting language so the syntax is most probably wrong but I hope someone who know the scripting language can give the proper syntax.
 
User avatar
Joni
Member Candidate
Member Candidate
Posts: 156
Joined: Fri Mar 20, 2015 2:46 pm
Contact:

Re: Yet another DHCP to DNS script

Tue Apr 20, 2021 10:26 am

While the script itself is marvelous, one of the best dhcp2dns scripts...
Interestingly this prompts yet another RouterOS "feature", logs filled with "statis dns entry added/removed" a event with system,info topics but without a DNS topic to filter them away...
 
nagylzs
Member
Member
Posts: 353
Joined: Sun May 26, 2019 2:08 pm

Re: Yet another DHCP to DNS script

Wed Jun 09, 2021 3:20 pm

One of the best script I have ever seen for the purpose. I also wrote another one that adds entries for already bound leases:

/system script remove resetDhcpToStaticDns;
/system script add name="resetDhcpToStaticDns" source={

:local DHCPtag
:local topdomain;
:local hostname;
:local hostip;
:local skip;
:local protected;
:local ttl
:local leaseServerName

:set DHCPtag "#DHCP"
:set leaseServerName "defconf"
:set topdomain "put_your_top_domain_here."

/ip dhcp-server
:set ttl [ get [ find name=$leaseServerName ] lease-time ]
  

/ip dhcp-server lease;
:foreach i in=[find where status="bound"] do={
  /ip dhcp-server lease;
  :if ([:len [get $i host-name]] > 0) do={
    :set hostname ([get $i host-name] . "." . $topdomain);
    :set hostip [get $i address];
    /ip dns static;
# Remove if DNS entry already exist
	:set protected false;
	:set skip false;
    :foreach di in [find] do={
      :if ([get $di name] = $hostname) do={
		:if ([get $di comment] = $DHCPtag) do={
			:if ([get $di address] = $hostip) do={
				:put ("Unchanged: " . $hostname . " : " . $hostip);
				:set skip true;
			} else={
				:put ("Removing: " . $hostname . " : " . $hostip);
			}
			remove $di;
		} else={
			:set protected true;
			:put ("Protected: " . $hostname . " : " . $hostip);
		}
      }
    }
	:if (!$skip && !$protected) do={
# Add DNS entry
		:put ("Adding: " . $hostname . " : " . $hostip);
		/ip dns static add name=$hostname address=$hostip ttl=$ttl comment=$DHCPtag;
	}
  }
}

}
 
leikoilja
just joined
Posts: 6
Joined: Sat May 08, 2021 10:53 am

Re: Yet another DHCP to DNS script

Sat Nov 27, 2021 10:56 am

@avggeek, your script was working miracles, but recently don't seem to work anymore. Does it work for anyone of RouterOS v6.49?

Tried to look at logs and closer to the script and I think it fails on the line where it set's the `domain`:
  :local domain [ get [ find $leaseActIP in address ] domain ]
  :log info "$LogPrefix: DNS domain is $domain"
In my case, it simply says `domain` is empty all the time for all devices
 
andreasbehnke
just joined
Posts: 6
Joined: Thu Oct 18, 2018 9:20 am

Re: Yet another DHCP to DNS script

Wed Mar 30, 2022 9:45 am

Hello!
I implemented a litte device monitor list software around this DHCP DNS lease script which you can host in your network to be kept informed about new devices and give devices meaningful DNS names. Take a look at this project: https://github.com/andreasbehnke/device-mon
 
Boardy
just joined
Posts: 5
Joined: Sun Apr 01, 2018 11:01 am

Re: Yet another DHCP to DNS script

Sun Apr 10, 2022 11:44 am

Hi, one question abaut this check:
# 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 warning "$LogPrefix: Multiple active DHCP leases for '$leaseActIP' (???)"
:error "Multiple active DHCP leases for '$leaseActIP' (???)"
}

Its actually going in error if there is an inactive assignment still existing (I put some static entirs on inactive to keep them if device will be reused later on - wanted to use new devices with same IP to reuse firewall settings for Testing only)
It looks like the script is also counting inactive entries - how can this get adapted???

Uwe
 
avggeek
newbie
Posts: 48
Joined: Thu Jun 06, 2013 9:33 am

Re: Yet another DHCP to DNS script

Fri Sep 23, 2022 12:55 pm

@avggeek, your script was working miracles, but recently don't seem to work anymore. Does it work for anyone of RouterOS v6.49?

Tried to look at logs and closer to the script and I think it fails on the line where it set's the `domain`:
  :local domain [ get [ find $leaseActIP in address ] domain ]
  :log info "$LogPrefix: DNS domain is $domain"
In my case, it simply says `domain` is empty all the time for all devices
I apologize but I do not get notifications unless someone quotes my post in a reply. To answer the question, I just uncommented that log message on my CCR-1009 running 6.49 and can confirm the log message prints correctly for me.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Yet another DHCP to DNS script

Fri Sep 23, 2022 7:41 pm

Probably all devices do not have hostname....
 
sebus46
newbie
Posts: 31
Joined: Sat Jun 17, 2023 4:59 pm

Re: Yet another DHCP to DNS script

Sun Jun 18, 2023 2:43 pm

I spent some time cleaning up the original script by @dse to incorporate the following:
On ROS 7.10 I get in log few (for various static reservations):
DHCP2DNS (defconf): Failure during dns registration of npi508736.domain_name.home with 192.168.88.39

Who is online

Users browsing this forum: No registered users and 17 guests