diff --git a/DHCP2DNS_CLEANUP b/DHCP2DNS_CLEANUP new file mode 100644 index 0000000..388c81e --- /dev/null +++ b/DHCP2DNS_CLEANUP @@ -0,0 +1,40 @@ +# Remove static DNS entries that are no longer in the DHCP lease list +:foreach dnsRecord in=[/ip dns static find where comment~"#DHCP"] do={ + + :local dnsName [/ip dns static get $dnsRecord name] + :local dnsTTL [/ip dns static get $dnsRecord ttl] + :local dnsAddress [/ip dns static get $dnsRecord address] + + :local found false + + # Check if the DNS name exists in the DHCP lease list + :foreach lease in=[/ip dhcp-server lease find] do={ + :local hostName [/ip dhcp-server lease get $lease host-name] + :local ipAddress [/ip dhcp-server lease get $lease address] + :local ttl [/ip dhcp-server lease get $lease expires-after] + + # Find the corresponding network for the lease's IP address + :local networkId [/ip dhcp-server network find where $ipAddress in address] + :local domainName [/ip dhcp-server network get $networkId domain] + + :local fqdn "$hostName.$domainName" + + :if ($fqdn = $dnsName) do={ + :set found true + + # Update TTL if it's different + :if ($dnsTTL != $ttl) do={ + :log info "Updating TTL for $dnsName from $dnsTTL to $ttl" + /ip dns static set $dnsRecord ttl=$ttl + } + + :break + } + } + + # If not found, remove the DNS static entry + :if (!$found) do={ + :log info "Removing static DNS entry: $dnsName" + /ip dns static remove $dnsRecord + } +} \ No newline at end of file