From 3d589def7d5a1919f41c6f6cd128ff7c383e7a3d Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Wed, 22 Mar 2023 08:42:59 +0100 Subject: dhcp-to-dns: allow the matching separator string in name --- dhcp-to-dns.rsc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dhcp-to-dns.rsc b/dhcp-to-dns.rsc index 48f96b2..6e2c2a9 100644 --- a/dhcp-to-dns.rsc +++ b/dhcp-to-dns.rsc @@ -32,11 +32,11 @@ $ScriptLock $0 false 10; :local CommentPrefix ("managed by " . $0 . " for "); :local CommentString ("--- " . $0 . " above ---"); -:if ([ :len [ /ip/dns/static/find where comment=$CommentString name=- type=NXDOMAIN disabled ] ] = 0) do={ - /ip/dns/static/add comment=$CommentString name=- type=NXDOMAIN disabled=yes; - $LogPrintExit2 warning $0 ("Added disabled static dns record with comment '" . $CommentString . "'.") false; +:if ([ :len [ /ip/dns/static/find where (name=$CommentString or (comment=$CommentString and name=-)) type=NXDOMAIN disabled ] ] = 0) do={ + /ip/dns/static/add name=$CommentString type=NXDOMAIN disabled=yes; + $LogPrintExit2 warning $0 ("Added disabled static dns record with name '" . $CommentString . "'.") false; } -:local PlaceBefore ([ /ip/dns/static/find where comment=$CommentString name=- type=NXDOMAIN disabled ]->0); +:local PlaceBefore ([ /ip/dns/static/find where (name=$CommentString or (comment=$CommentString and name=-)) type=NXDOMAIN disabled ]->0); :foreach DnsRecord in=[ /ip/dns/static/find where comment ~ $CommentPrefix ] do={ :local DnsRecordVal [ /ip/dns/static/get $DnsRecord ]; -- cgit v1.2.3-54-g00ecf From 682a09c94b777083e9a60c65e2f8c574850cfc20 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 21 Mar 2023 22:49:33 +0100 Subject: dhcp-to-dns: match comment on beginning of line --- dhcp-to-dns.rsc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dhcp-to-dns.rsc b/dhcp-to-dns.rsc index 6e2c2a9..0914330 100644 --- a/dhcp-to-dns.rsc +++ b/dhcp-to-dns.rsc @@ -38,7 +38,7 @@ $ScriptLock $0 false 10; } :local PlaceBefore ([ /ip/dns/static/find where (name=$CommentString or (comment=$CommentString and name=-)) type=NXDOMAIN disabled ]->0); -:foreach DnsRecord in=[ /ip/dns/static/find where comment ~ $CommentPrefix ] do={ +:foreach DnsRecord in=[ /ip/dns/static/find where comment~("^" . $CommentPrefix) ] do={ :local DnsRecordVal [ /ip/dns/static/get $DnsRecord ]; :local MacAddress [ $CharacterReplace ($DnsRecordVal->"comment") $CommentPrefix "" ]; :if ([ :len [ /ip/dhcp-server/lease/find where mac-address=$MacAddress address=($DnsRecordVal->"address") status=bound ] ] > 0) do={ -- cgit v1.2.3-54-g00ecf From 4a0f395fe127f82d35e0ac40af081a2391a36da5 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 21 Mar 2023 20:49:43 +0100 Subject: dhcp-to-dns: split fqdn to host name and domain --- dhcp-to-dns.rsc | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/dhcp-to-dns.rsc b/dhcp-to-dns.rsc index 0914330..59daef4 100644 --- a/dhcp-to-dns.rsc +++ b/dhcp-to-dns.rsc @@ -63,9 +63,9 @@ $ScriptLock $0 false 10; :local HostName [ $IfThenElse ([ :len ($LeaseVal->"host-name") ] = 0) \ [ $CharacterReplace ($LeaseVal->"mac-address") ":" "-" ] \ [ $CharacterReplace ($LeaseVal->"host-name") " " "" ] ]; + :local Domain ([ $IfThenElse ($ServerNameInZone = true) ($LeaseVal->"server" . ".") ] . $Zone); - :local Fqdn ($HostName . "." . [ $IfThenElse ($ServerNameInZone = true) ($LeaseVal->"server" . ".") ] . $Zone); - :local DnsRecord [ /ip/dns/static/find where name=$Fqdn ]; + :local DnsRecord [ /ip/dns/static/find where name=($HostName . "." . $Domain) ]; :if ([ :len $DnsRecord ] > 0) do={ :local DnsIp [ /ip/dns/static/get $DnsRecord address ]; @@ -82,14 +82,14 @@ $ScriptLock $0 false 10; } :if ($DnsIp = $LeaseVal->"address") do={ - $LogPrintExit2 debug $0 ("DNS entry for " . $Fqdn . " does not need updating.") false; + $LogPrintExit2 debug $0 ("DNS entry for " . ($HostName . "." . $Domain) . " does not need updating.") false; } else={ - $LogPrintExit2 info $0 ("Replacing DNS entry for " . $Fqdn . ", new address is " . $LeaseVal->"address" . ".") false; - /ip/dns/static/set name=$Fqdn address=($LeaseVal->"address") ttl=$Ttl comment=$Comment $DnsRecord; + $LogPrintExit2 info $0 ("Replacing DNS entry for " . ($HostName . "." . $Domain) . ", new address is " . $LeaseVal->"address" . ".") false; + /ip/dns/static/set name=($HostName . "." . $Domain) address=($LeaseVal->"address") ttl=$Ttl comment=$Comment $DnsRecord; } } else={ - $LogPrintExit2 info $0 ("Adding new DNS entry for " . $Fqdn . ", address is " . $LeaseVal->"address" . ".") false; - /ip/dns/static/add name=$Fqdn address=($LeaseVal->"address") ttl=$Ttl comment=$Comment place-before=$PlaceBefore; + $LogPrintExit2 info $0 ("Adding new DNS entry for " . ($HostName . "." . $Domain) . ", address is " . $LeaseVal->"address" . ".") false; + /ip/dns/static/add name=($HostName . "." . $Domain) address=($LeaseVal->"address") ttl=$Ttl comment=$Comment place-before=$PlaceBefore; } } else={ $LogPrintExit2 debug $0 ("No address available... Ignoring.") false; -- cgit v1.2.3-54-g00ecf From aac87098814e10d4ec36ba2a77a29c2018869774 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 21 Mar 2023 20:59:15 +0100 Subject: dhcp-to-dns: always create A record with mac address... ... and an optional CNAME with host name. --- dhcp-to-dns.rsc | 23 +++++++++++++---------- global-functions.rsc | 2 +- news-and-changes.rsc | 3 ++- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/dhcp-to-dns.rsc b/dhcp-to-dns.rsc index 59daef4..ee12697 100644 --- a/dhcp-to-dns.rsc +++ b/dhcp-to-dns.rsc @@ -38,7 +38,7 @@ $ScriptLock $0 false 10; } :local PlaceBefore ([ /ip/dns/static/find where (name=$CommentString or (comment=$CommentString and name=-)) type=NXDOMAIN disabled ]->0); -:foreach DnsRecord in=[ /ip/dns/static/find where comment~("^" . $CommentPrefix) ] do={ +:foreach DnsRecord in=[ /ip/dns/static/find where comment~("^" . $CommentPrefix) !(type=CNAME) ] do={ :local DnsRecordVal [ /ip/dns/static/get $DnsRecord ]; :local MacAddress [ $CharacterReplace ($DnsRecordVal->"comment") $CommentPrefix "" ]; :if ([ :len [ /ip/dhcp-server/lease/find where mac-address=$MacAddress address=($DnsRecordVal->"address") status=bound ] ] > 0) do={ @@ -47,6 +47,7 @@ $ScriptLock $0 false 10; :local Found false; $LogPrintExit2 info $0 ("Lease expired for " . $MacAddress . " (" . $DnsRecordVal->"name" . "), deleting DNS entry.") false; /ip/dns/static/remove $DnsRecord; + /ip/dns/static/remove [ find where type=CNAME cname=($DnsRecordVal->"name") comment=($DnsRecordVal->"comment") ]; } } @@ -60,12 +61,11 @@ $ScriptLock $0 false 10; :if ([ :len ($LeaseVal->"address") ] > 0) do={ :local Comment ($CommentPrefix . $LeaseVal->"mac-address"); - :local HostName [ $IfThenElse ([ :len ($LeaseVal->"host-name") ] = 0) \ - [ $CharacterReplace ($LeaseVal->"mac-address") ":" "-" ] \ - [ $CharacterReplace ($LeaseVal->"host-name") " " "" ] ]; + :local MacDash [ $CharacterReplace ($LeaseVal->"mac-address") ":" "-" ]; + :local HostName [ $CharacterReplace ($LeaseVal->"host-name") " " "" ]; :local Domain ([ $IfThenElse ($ServerNameInZone = true) ($LeaseVal->"server" . ".") ] . $Zone); - :local DnsRecord [ /ip/dns/static/find where name=($HostName . "." . $Domain) ]; + :local DnsRecord [ /ip/dns/static/find where name=($MacDash . "." . $Domain) ]; :if ([ :len $DnsRecord ] > 0) do={ :local DnsIp [ /ip/dns/static/get $DnsRecord address ]; @@ -82,14 +82,17 @@ $ScriptLock $0 false 10; } :if ($DnsIp = $LeaseVal->"address") do={ - $LogPrintExit2 debug $0 ("DNS entry for " . ($HostName . "." . $Domain) . " does not need updating.") false; + $LogPrintExit2 debug $0 ("DNS entry for " . ($MacDash . "." . $Domain) . " does not need updating.") false; } else={ - $LogPrintExit2 info $0 ("Replacing DNS entry for " . ($HostName . "." . $Domain) . ", new address is " . $LeaseVal->"address" . ".") false; - /ip/dns/static/set name=($HostName . "." . $Domain) address=($LeaseVal->"address") ttl=$Ttl comment=$Comment $DnsRecord; + $LogPrintExit2 info $0 ("Replacing DNS entry for " . ($MacDash . "." . $Domain) . ", new address is " . $LeaseVal->"address" . ".") false; + /ip/dns/static/set name=($MacDash . "." . $Domain) address=($LeaseVal->"address") ttl=$Ttl comment=$Comment $DnsRecord; } } else={ - $LogPrintExit2 info $0 ("Adding new DNS entry for " . ($HostName . "." . $Domain) . ", address is " . $LeaseVal->"address" . ".") false; - /ip/dns/static/add name=($HostName . "." . $Domain) address=($LeaseVal->"address") ttl=$Ttl comment=$Comment place-before=$PlaceBefore; + $LogPrintExit2 info $0 ("Adding new DNS entry for " . ($MacDash . "." . $Domain) . ", address is " . $LeaseVal->"address" . ".") false; + /ip/dns/static/add name=($MacDash . "." . $Domain) type=A address=($LeaseVal->"address") ttl=$Ttl comment=$Comment place-before=$PlaceBefore; + :if ([ :len $HostName ] > 0) do={ + /ip/dns/static/add name=($HostName . "." . $Domain) type=CNAME cname=($MacDash . "." . $Domain) ttl=$Ttl comment=$Comment place-before=$PlaceBefore; + } } } else={ $LogPrintExit2 debug $0 ("No address available... Ignoring.") false; diff --git a/global-functions.rsc b/global-functions.rsc index 512cf34..2249fe9 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -12,7 +12,7 @@ :local 0 "global-functions"; # expected configuration version -:global ExpectedConfigVersion 96; +:global ExpectedConfigVersion 97; # global variables not to be changed by user :global GlobalFunctionsReady false; diff --git a/news-and-changes.rsc b/news-and-changes.rsc index e809db2..d21ea6b 100644 --- a/news-and-changes.rsc +++ b/news-and-changes.rsc @@ -10,9 +10,10 @@ # Changes for global-config to be added to notification on script updates :global GlobalConfigChanges { 96="Added support for notes in 'netwatch-notify', these are included verbatim into the notification."; + 97="Modified 'dhcp-to-dns' to always add A records for names with mac address, and optionally add CNAME records if the host name is available."; }; # Migration steps to be applied on script updates :global GlobalConfigMigration { - 0; + 97=":local Rec [ /ip/dns/static/find where comment~\"^managed by dhcp-to-dns for \" ]; :if ([ :len \$Rec ] > 0) do={ /ip/dns/static/remove \$Rec; /system/script/run dhcp-to-dns; }"; }; -- cgit v1.2.3-54-g00ecf From 8c728bb6ab7ad27c223af536be50438d51ee360c Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Wed, 22 Mar 2023 08:46:35 +0100 Subject: dhcp-to-dns: do not update records based on host name --- dhcp-to-dns.rsc | 7 ------- 1 file changed, 7 deletions(-) diff --git a/dhcp-to-dns.rsc b/dhcp-to-dns.rsc index ee12697..8f5b842 100644 --- a/dhcp-to-dns.rsc +++ b/dhcp-to-dns.rsc @@ -74,13 +74,6 @@ $ScriptLock $0 false 10; :set ($LeaseVal->"address") [ /ip/dhcp-server/lease/get ($DupMacLeases->([ :len $DupMacLeases ] - 1)) address ]; } - :if ([ :len ($LeaseVal->"host-name") ] > 0) do={ - :local HostNameLeases [ /ip/dhcp-server/lease/find where host-name=($LeaseVal->"host-name") status=bound ]; - :if ([ :len $HostNameLeases ] > 1) do={ - :set ($LeaseVal->"address") [ /ip/dhcp-server/lease/get ($HostNameLeases->0) address ]; - } - } - :if ($DnsIp = $LeaseVal->"address") do={ $LogPrintExit2 debug $0 ("DNS entry for " . ($MacDash . "." . $Domain) . " does not need updating.") false; } else={ -- cgit v1.2.3-54-g00ecf From c5c235246b82530135352eef954e5ce477455f93 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Wed, 22 Mar 2023 09:16:16 +0100 Subject: dhcp-to-dns: add debug message on duplicate leases --- dhcp-to-dns.rsc | 1 + 1 file changed, 1 insertion(+) diff --git a/dhcp-to-dns.rsc b/dhcp-to-dns.rsc index 8f5b842..8166b0c 100644 --- a/dhcp-to-dns.rsc +++ b/dhcp-to-dns.rsc @@ -71,6 +71,7 @@ $ScriptLock $0 false 10; :local DupMacLeases [ /ip/dhcp-server/lease/find where mac-address=($LeaseVal->"mac-address") status=bound ]; :if ([ :len $DupMacLeases ] > 1) do={ + $LogPrintExit2 debug $0 ("Multiple bound leases found for mac-address " . ($LeaseVal->"mac-address") . ", using ip address of last one.") false; :set ($LeaseVal->"address") [ /ip/dhcp-server/lease/get ($DupMacLeases->([ :len $DupMacLeases ] - 1)) address ]; } -- cgit v1.2.3-54-g00ecf From b13a53531bcd2b557b207810110b8987b93208b6 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Wed, 22 Mar 2023 09:18:06 +0100 Subject: dhcp-to-dns: just update the address The other values *should* be unchanged... --- dhcp-to-dns.rsc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dhcp-to-dns.rsc b/dhcp-to-dns.rsc index 8166b0c..aa71e4c 100644 --- a/dhcp-to-dns.rsc +++ b/dhcp-to-dns.rsc @@ -79,7 +79,7 @@ $ScriptLock $0 false 10; $LogPrintExit2 debug $0 ("DNS entry for " . ($MacDash . "." . $Domain) . " does not need updating.") false; } else={ $LogPrintExit2 info $0 ("Replacing DNS entry for " . ($MacDash . "." . $Domain) . ", new address is " . $LeaseVal->"address" . ".") false; - /ip/dns/static/set name=($MacDash . "." . $Domain) address=($LeaseVal->"address") ttl=$Ttl comment=$Comment $DnsRecord; + /ip/dns/static/set address=($LeaseVal->"address") $DnsRecord; } } else={ $LogPrintExit2 info $0 ("Adding new DNS entry for " . ($MacDash . "." . $Domain) . ", address is " . $LeaseVal->"address" . ".") false; -- cgit v1.2.3-54-g00ecf From 295ccb570d98dd3f782da5e0272d62bf2698a836 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Wed, 22 Mar 2023 10:21:31 +0100 Subject: dhcp-to-dns: update cname record on changed host name --- dhcp-to-dns.rsc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dhcp-to-dns.rsc b/dhcp-to-dns.rsc index aa71e4c..75b3fbf 100644 --- a/dhcp-to-dns.rsc +++ b/dhcp-to-dns.rsc @@ -81,6 +81,12 @@ $ScriptLock $0 false 10; $LogPrintExit2 info $0 ("Replacing DNS entry for " . ($MacDash . "." . $Domain) . ", new address is " . $LeaseVal->"address" . ".") false; /ip/dns/static/set address=($LeaseVal->"address") $DnsRecord; } + + :local Cname [ /ip/dns/static/find where type=CNAME cname=($MacDash . "." . $Domain) comment=$Comment ]; + :if ([ :len $Cname ] > 0 && [ /ip/dns/static/get $Cname name ] != ($HostName . "." . $Domain)) do={ + $LogPrintExit2 info $0 ("Host name changed, updating CNAME (pointing to " . ($MacDash . "." . $Domain) . ") to " . ($HostName . "." . $Domain) . ".") false; + /ip/dns/static/set name=($HostName . "." . $Domain) $Cname; + } } else={ $LogPrintExit2 info $0 ("Adding new DNS entry for " . ($MacDash . "." . $Domain) . ", address is " . $LeaseVal->"address" . ".") false; /ip/dns/static/add name=($MacDash . "." . $Domain) type=A address=($LeaseVal->"address") ttl=$Ttl comment=$Comment place-before=$PlaceBefore; -- cgit v1.2.3-54-g00ecf