2023-03-07 09:57:42 +00:00
|
|
|
#!rsc by RouterOS
|
|
|
|
# RouterOS script: dhcp-to-dns
|
|
|
|
# Copyright (c) 2013-2023 Christian Hesse <mail@eworm.de>
|
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
|
|
|
#
|
|
|
|
# provides: lease-script, order=20
|
|
|
|
#
|
|
|
|
# check DHCP leases and add/remove/update DNS entries
|
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/dhcp-to-dns.md
|
|
|
|
|
|
|
|
:local 0 "dhcp-to-dns";
|
|
|
|
:global GlobalFunctionsReady;
|
|
|
|
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
|
|
|
|
|
|
|
|
:global Domain;
|
|
|
|
:global Identity;
|
|
|
|
|
|
|
|
:global CharacterReplace;
|
2023-03-25 21:33:02 +00:00
|
|
|
:global EitherOr;
|
2023-03-07 09:57:42 +00:00
|
|
|
:global IfThenElse;
|
|
|
|
:global LogPrintExit2;
|
2023-10-27 07:08:23 +00:00
|
|
|
:global LogPrintOnce;
|
2023-03-25 21:33:02 +00:00
|
|
|
:global ParseKeyValueStore;
|
2023-03-07 09:57:42 +00:00
|
|
|
:global ScriptLock;
|
|
|
|
|
|
|
|
$ScriptLock $0 false 10;
|
|
|
|
|
|
|
|
:local Ttl 5m;
|
2023-10-27 06:58:29 +00:00
|
|
|
:local CommentPrefix ("managed by " . $0);
|
2023-03-07 09:57:42 +00:00
|
|
|
:local CommentString ("--- " . $0 . " above ---");
|
|
|
|
|
2023-03-22 07:42:59 +00:00
|
|
|
: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;
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
2023-03-22 07:42:59 +00:00
|
|
|
:local PlaceBefore ([ /ip/dns/static/find where (name=$CommentString or (comment=$CommentString and name=-)) type=NXDOMAIN disabled ]->0);
|
2023-03-07 09:57:42 +00:00
|
|
|
|
2023-10-27 06:58:29 +00:00
|
|
|
:foreach DnsRecord in=[ /ip/dns/static/find where comment~("^" . $CommentPrefix . "\\b") (!type or type=A) ] do={
|
2023-03-07 09:57:42 +00:00
|
|
|
:local DnsRecordVal [ /ip/dns/static/get $DnsRecord ];
|
2023-10-27 06:58:29 +00:00
|
|
|
:local DnsRecordInfo [ $ParseKeyValueStore ($DnsRecordVal->"comment") ];
|
|
|
|
:if ([ :len [ /ip/dhcp-server/lease/find where active-mac-address=($DnsRecordInfo->"macaddress") \
|
|
|
|
active-address=($DnsRecordVal->"address") server=($DnsRecordInfo->"server") status=bound ] ] > 0) do={
|
|
|
|
$LogPrintExit2 debug $0 ("Lease for " . $DnsRecordInfo->"macaddress" . " (" . $DnsRecordVal->"name" . ") still exists. Not deleting record.") false;
|
2023-03-07 09:57:42 +00:00
|
|
|
} else={
|
|
|
|
:local Found false;
|
2023-10-27 06:58:29 +00:00
|
|
|
$LogPrintExit2 info $0 ("Lease expired for " . $DnsRecordInfo->"macaddress" . " in " . \
|
|
|
|
$DnsRecordInfo->"server" . ", deleting record (" . $DnsRecordVal->"name" . ").") false;
|
2023-03-07 09:57:42 +00:00
|
|
|
/ip/dns/static/remove $DnsRecord;
|
2023-04-12 21:43:45 +00:00
|
|
|
/ip/dns/static/remove [ find where type=CNAME comment=($DnsRecordVal->"comment") ];
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
:foreach Lease in=[ /ip/dhcp-server/lease/find where status=bound ] do={
|
|
|
|
:local LeaseVal;
|
|
|
|
:do {
|
|
|
|
:set LeaseVal [ /ip/dhcp-server/lease/get $Lease ];
|
|
|
|
} on-error={
|
|
|
|
$LogPrintExit2 debug $0 ("A lease just vanished, ignoring.") false;
|
|
|
|
}
|
|
|
|
|
2023-06-08 19:33:49 +00:00
|
|
|
:if ([ :len ($LeaseVal->"active-address") ] > 0) do={
|
2023-10-27 06:58:29 +00:00
|
|
|
:local Comment ($CommentPrefix . ", macaddress=" . $LeaseVal->"active-mac-address" . ", server=" . $LeaseVal->"server");
|
2023-06-08 19:33:49 +00:00
|
|
|
:local MacDash [ $CharacterReplace ($LeaseVal->"active-mac-address") ":" "-" ];
|
2023-03-25 21:33:02 +00:00
|
|
|
:local HostName [ $CharacterReplace [ $EitherOr ([ $ParseKeyValueStore ($LeaseVal->"comment") ]->"hostname") ($LeaseVal->"host-name") ] " " "" ];
|
2023-06-08 19:33:49 +00:00
|
|
|
:local Network [ /ip/dhcp-server/network/find where ($LeaseVal->"active-address") in address ];
|
2023-04-12 22:32:13 +00:00
|
|
|
:local NetworkVal;
|
|
|
|
:if ([ :len $Network ] > 0) do={
|
|
|
|
:set NetworkVal [ /ip/dhcp-server/network/get ($Network->0) ];
|
|
|
|
}
|
|
|
|
:local NetworkInfo [ $ParseKeyValueStore ($NetworkVal->"comment") ];
|
|
|
|
:local NetDomain ([ $IfThenElse ([ :len ($NetworkInfo->"name-extra") ] > 0) ($NetworkInfo->"name-extra" . ".") ] . \
|
|
|
|
[ $EitherOr [ $EitherOr ($NetworkInfo->"domain") ($NetworkVal->"domain") ] $Domain ]);
|
2023-03-07 09:57:42 +00:00
|
|
|
|
2023-04-12 21:09:58 +00:00
|
|
|
:local DnsRecord [ /ip/dns/static/find where comment=$Comment (!type or type=A) ];
|
2023-03-07 09:57:42 +00:00
|
|
|
:if ([ :len $DnsRecord ] > 0) do={
|
2023-04-12 21:21:51 +00:00
|
|
|
:local DnsRecordVal [ /ip/dns/static/get $DnsRecord ];
|
2023-03-07 09:57:42 +00:00
|
|
|
|
2023-06-08 19:33:49 +00:00
|
|
|
:if ($DnsRecordVal->"address" = $LeaseVal->"active-address" && $DnsRecordVal->"name" = ($MacDash . "." . $NetDomain)) do={
|
2023-10-27 06:58:29 +00:00
|
|
|
$LogPrintExit2 debug $0 ("The A record for " . $LeaseVal->"active-mac-address" . " in " . \
|
|
|
|
$LeaseVal->"server" . " does not need updating.") false;
|
2023-03-07 09:57:42 +00:00
|
|
|
} else={
|
2023-10-27 06:58:29 +00:00
|
|
|
$LogPrintExit2 info $0 ("Updating A record for " . $LeaseVal->"active-mac-address" . " in " . \
|
|
|
|
$LeaseVal->"server" . " (" . ($MacDash . "." . $NetDomain) . " -> " . $LeaseVal->"active-address" . ").") false;
|
2023-06-08 19:33:49 +00:00
|
|
|
/ip/dns/static/set address=($LeaseVal->"active-address") name=($MacDash . "." . $NetDomain) $DnsRecord;
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
2023-03-22 09:21:31 +00:00
|
|
|
|
2023-10-27 06:58:29 +00:00
|
|
|
:local CName [ /ip/dns/static/find where comment=$Comment type=CNAME ];
|
|
|
|
:if ([ :len $CName ] > 0) do={
|
|
|
|
:local CNameVal [ /ip/dns/static/get $CName ];
|
|
|
|
:if ($CNameVal->"name" != ($HostName . "." . $NetDomain) || $CNameVal->"cname" != ($MacDash . "." . $NetDomain)) do={
|
|
|
|
$LogPrintExit2 info $0 ("Deleting CNAME record with wrong data for " . $LeaseVal->"active-mac-address" . " in " . \
|
|
|
|
$LeaseVal->"server" . ".") false;
|
|
|
|
/ip/dns/static/remove $CName;
|
|
|
|
}
|
2023-03-25 21:31:56 +00:00
|
|
|
}
|
2023-10-27 06:58:29 +00:00
|
|
|
:if ([ :len $HostName ] > 0 && [ :len [ /ip/dns/static/find where name=($HostName . "." . $NetDomain) type=CNAME ] ] = 0) do={
|
|
|
|
$LogPrintExit2 info $0 ("Adding CNAME record for " . $LeaseVal->"active-mac-address" . " in " . \
|
|
|
|
$LeaseVal->"server" . " (" . ($HostName . "." . $NetDomain) . " -> " . ($MacDash . "." . $NetDomain) . ").") false;
|
|
|
|
/ip/dns/static/add name=($HostName . "." . $NetDomain) type=CNAME cname=($MacDash . "." . $NetDomain) ttl=$Ttl comment=$Comment place-before=$PlaceBefore;
|
2023-03-22 09:21:31 +00:00
|
|
|
}
|
2023-10-27 06:58:29 +00:00
|
|
|
|
2023-03-07 09:57:42 +00:00
|
|
|
} else={
|
2023-10-27 06:58:29 +00:00
|
|
|
$LogPrintExit2 info $0 ("Adding A record for " . $LeaseVal->"active-mac-address" . " in " . \
|
|
|
|
$LeaseVal->"server" . " (" . ($MacDash . "." . $NetDomain) . " -> " . $LeaseVal->"active-address" . ").") false;
|
2023-06-08 19:33:49 +00:00
|
|
|
/ip/dns/static/add name=($MacDash . "." . $NetDomain) type=A address=($LeaseVal->"active-address") ttl=$Ttl comment=$Comment place-before=$PlaceBefore;
|
2023-10-27 06:58:29 +00:00
|
|
|
:if ([ :len $HostName ] > 0 && [ :len [ /ip/dns/static/find where name=($HostName . "." . $NetDomain) type=CNAME ] ] = 0) do={
|
|
|
|
$LogPrintExit2 info $0 ("Adding CNAME record for " . $LeaseVal->"active-mac-address" . " in " . \
|
|
|
|
$LeaseVal->"server" . " (" . ($HostName . "." . $NetDomain) . " -> " . ($MacDash . "." . $NetDomain) . ").") false;
|
2023-04-12 22:46:05 +00:00
|
|
|
/ip/dns/static/add name=($HostName . "." . $NetDomain) type=CNAME cname=($MacDash . "." . $NetDomain) ttl=$Ttl comment=$Comment place-before=$PlaceBefore;
|
2023-03-21 19:59:15 +00:00
|
|
|
}
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
2023-10-27 07:08:23 +00:00
|
|
|
|
|
|
|
:if ([ :len [ /ip/dns/static/find where name=($MacDash . "." . $NetDomain) (!type or type=A) ] ] > 1) do={
|
|
|
|
$LogPrintOnce warning $0 ("The name '" . $MacDash . "." . $NetDomain . "' appeared in more than one A record!");
|
|
|
|
}
|
2023-03-07 09:57:42 +00:00
|
|
|
} else={
|
|
|
|
$LogPrintExit2 debug $0 ("No address available... Ignoring.") false;
|
|
|
|
}
|
|
|
|
}
|