Community discussions

MikroTik App
 
Josephny
Member
Member
Topic Author
Posts: 495
Joined: Tue Sep 20, 2022 12:11 am

DHCP to DNS

Sun Oct 29, 2023 2:09 am

I've been using this DHCP LEASE to STATIC DNS entry script and I notice that it takes the hostname from the dhcp lease to form the dns entry.

But, some of my devices (mostly iOT-type things) have identical hostnames like: espressif and Emporia.

Would it be possible to check if an entry with that name but with a different MAC address already exists in the DNS list and if so, append a "-2" or something to the end of the hostname part in the dns entry (so it actually gets added)?

Thank you.

# SPDX-License-Identifier: CC0-1.0

:local domains [:toarray "76.local"]
:local dnsttl "15m"
:local magiccomment "automatic-from-dhcp (magic comment)"
:local activehosts [:toarray ""]

:foreach lease in [/ip dhcp-server lease find] do={
 :local hostname [/ip dhcp-server lease get value-name=host-name $lease]
 :local hostaddr [/ip dhcp-server lease get value-name=address $lease]

 :if ([:len $hostname] > 0) do={
 
    :foreach domain in $domains do={
      :local regdomain "$hostname.$domain"
      :set activehosts ($activehosts, $regdomain)
      :if ([:len [/ip dns static find where name=$regdomain]] = 0) do={
        /ip dns static add name=$regdomain address=$hostaddr comment=$magiccomment ttl=$dnsttl
      } else={
        :if ([:len [/ip dns static find where name=$regdomain comment=$magiccomment]] = 1) do={
          /ip dns static set address=$hostaddr [/ip dns static find name=$regdomain comment=$magiccomment]
        }
      }
    }
  }
}

:foreach dnsentry in [/ip dns static find where comment=$magiccomment] do={
  :local hostname [/ip dns static get value-name=name $dnsentry]
  :if ([:type [:find $activehosts $hostname]] = "nil") do={
    /ip dns static remove $dnsentry
  }
}

 
Josephny
Member
Member
Topic Author
Posts: 495
Joined: Tue Sep 20, 2022 12:11 am

Re: DHCP to DNS

Mon Oct 30, 2023 2:13 pm

This is beyond my capabilities.

I'm thinking that if code can be inserted as follows, then different hosts with the same hostname can be added to the static DNS list:


 :if ([:len $hostname] > 0) do={
 
    :foreach domain in $domains do={
      :local regdomain "$hostname.$domain"
      :set activehosts ($activehosts, $regdomain)
      :if ([:len [/ip dns static find where name=$regdomain]] = 0) do={
        /ip dns static add name=$regdomain address=$hostaddr comment=$magiccomment ttl=$dnsttl
      } else={
        :if ([:len [/ip dns static find where name=$regdomain comment=$magiccomment]] = 1) do={
        
             
 *****IF /ip dns static name=$regdomain ALREADY EXISTS, APPEND A 5 DIGIT RANDOM NUMBER to $regdomain and proceed
        
        
          /ip dns static set address=$hostaddr [/ip dns static find name=$regdomain comment=$magiccomment]
        }

 
Josephny
Member
Member
Topic Author
Posts: 495
Joined: Tue Sep 20, 2022 12:11 am

Re: DHCP to DNS

Tue Oct 31, 2023 10:17 am

It's conclusive: This is beyond me.

I have the following gobbly-gook:
# SPDX-License-Identifier: CC0-1.0

:local domains [:toarray "212.local"]
:local dnsttl "100m"
:local magiccomment "automatic-from-dhcp (magic comment)"
:local activehosts [:toarray ""]

:foreach lease in [/ip dhcp-server lease find] do={
  :local hostname [/ip dhcp-server lease get value-name=host-name $lease]
  :local hostaddr [/ip dhcp-server lease get value-name=address $lease]
  :local macaddr [/ip dhcp-server lease get value-name=mac-address $lease]

  :if ([:len $hostname] > 0) do={

    :foreach domain in $domains do={

      :local regdomain "$hostname.$domain"
      :set activehosts ($activehosts, $regdomain)

      :if ([:len [/ip dns static find where name=$regdomain]] = 0) do={
        /ip dns static add name=$regdomain address=$hostaddr comment=$magiccomment ttl=$dnsttl
      } else={
        :if ([/ip dns static find where name=$regdomain] = $hostname) | ([/ip dhcp-server lease find where address=$hostaddr] = $macaddr) do=(
		  :set regdomain=("$hostname", "-1")       
          :/ip dns static add name=$regdomain address=$hostaddr comment=$magiccomment ttl=$dnsttl
        }  
          else={
          /ip dns static add name=$regdomain address=$hostaddr comment=$magiccomment ttl=$dnsttl
          }


        :if ([:len [/ip dns static find where name=$regdomain comment=$magiccomment]] = 1) do={
          /ip dns static set address=$hostaddr [/ip dns static find name=$regdomain comment=$magiccomment]
        }
    }
  }

Who is online

Users browsing this forum: No registered users and 4 guests