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.
Code: Select all
# 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
}
}