From bb4682eceea9a0d6d8e1420c5ad61e12a857de0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C=20=D0=91=D0=B0=D1=80=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Thu, 8 Aug 2024 13:22:01 +0000 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20DHCP2DNS=5FCLEANUP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DHCP2DNS_CLEANUP | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 DHCP2DNS_CLEANUP 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