This commit is contained in:
Łukasz Witkowski 2026-05-14 22:40:39 +00:00 committed by GitHub
commit eccec7a2e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 48 additions and 3 deletions

View file

@ -17,6 +17,7 @@
:local ScriptName [ :jobname ];
:global Domain;
:global DhcpToDnsCnameDomain;
:global Identity;
:global CleanName;
@ -32,6 +33,22 @@
:error false;
}
:local ToLower do={
:local Input [ :tostr $1 ];
:local Upper "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
:local Lower "abcdefghijklmnopqrstuvwxyz";
:local Return "";
:for I from=0 to=([ :len $Input ] - 1) do={
:local Char [ :pick $Input $I ];
:local Pos [ :find $Upper $Char ];
:if ([ :typeof $Pos ] != "nil") do={
:set Char [ :pick $Lower $Pos ($Pos + 1) ];
}
:set Return ($Return . $Char);
}
:return $Return;
}
:local Ttl 5m;
:local CommentPrefix ("managed by " . $ScriptName);
:local CommentString ("--- " . $ScriptName . " above ---");
@ -70,8 +87,9 @@
:if ([ :len ($LeaseVal->"active-address") ] > 0) do={
:local Comment ($CommentPrefix . ", macaddress=" . $LeaseVal->"active-mac-address" . ", server=" . $LeaseVal->"server");
:local MacDash [ $CleanName ($LeaseVal->"active-mac-address") ];
:local HostName [ $CleanName [ $EitherOr ([ $ParseKeyValueStore ($LeaseVal->"comment") ]->"hostname") ($LeaseVal->"host-name") ] ];
:local MacDash [ $ToLower [ $CleanName ($LeaseVal->"active-mac-address") ] ];
:local LeaseInfo [ $ParseKeyValueStore ($LeaseVal->"comment") ];
:local HostName [ $ToLower [ $CleanName [ $EitherOr ($LeaseInfo->"hostname") ($LeaseVal->"host-name") ] ] ];
:local Network [ /ip/dhcp-server/network/find where ($LeaseVal->"active-address") in address ];
:local NetworkVal;
:if ([ :len $Network ] > 0) do={
@ -81,7 +99,8 @@
:local NetDomain ([ $IfThenElse ([ :len ($NetworkInfo->"name-extra") ] > 0) ($NetworkInfo->"name-extra" . ".") ] . \
[ $EitherOr [ $EitherOr ($NetworkInfo->"domain") ($NetworkVal->"domain") ] $Domain ]);
:local FullA ($MacDash . "." . $NetDomain);
:local FullCN ($HostName . "." . $NetDomain);
:local CnameDomain [ $EitherOr ($LeaseInfo->"cname-domain") [ $EitherOr ($NetworkInfo->"cname-domain") $DhcpToDnsCnameDomain ] ];
:local FullCN ($HostName . "." . [ $IfThenElse ([ :len $CnameDomain ] > 0) $CnameDomain $NetDomain ]);
:local MacInServer ($LeaseVal->"active-mac-address" . " in " . $LeaseVal->"server");
:local DnsRecord [ /ip/dns/static/find where comment=$Comment type=A ];

View file

@ -63,6 +63,27 @@ If no domain is found in dhcp server's network definition a fallback from
* `Domain`: the domain used for dns records
### Override domain for CNAME records
By default both the A record (based on mac address) and the CNAME record
(based on host name) use the same domain. You can set a different domain
for CNAME records at three levels (in order of priority):
**Per lease** — add `cname-domain=` to the lease comment:
/ip/dhcp-server/lease/add address=10.0.0.50 comment="hostname=mydevice, cname-domain=example.com" mac-address=00:11:22:33:44:55 server=dhcp;
**Per network** — add `cname-domain=` to the network comment:
/ip/dhcp-server/network/add address=10.0.0.0/24 domain=dhcp.example.com comment="name-extra=v10, cname-domain=example.com";
This would result in A record `00-11-22-33-44-55.v10.dhcp.example.com` and
CNAME record `mydevice.example.com` for the same lease.
**Globally** — set the parameter in `global-config-overlay`:
* `DhcpToDnsCnameDomain`: domain used for CNAME records; leave empty (default) to use the same domain as A records
> **Info**: Copy relevant configuration from
> [`global-config`](../global-config.rsc) (the one without `-overlay`) to
> your local `global-config-overlay` and modify it to your specific needs.

View file

@ -22,6 +22,11 @@
# and backup scripts for file names.
:global Domain "example.com";
# Override domain used for CNAME records in dhcp-to-dns. Can also be set
# per network or per lease via 'cname-domain=...' in their comment.
# Leave empty to use the same domain as A records (default).
:global DhcpToDnsCnameDomain "";
# You can send e-mail notifications. Configure the system's mail settings
# (/tool/e-mail), then install the module:
# $ScriptInstallUpdate mod/notification-email