mirror of
https://github.com/eworm-de/routeros-scripts
synced 2024-05-14 08:04:19 +00:00
dhcp-to-dns: always create A record with mac address...
... and an optional CNAME with host name.
This commit is contained in:
parent
4a0f395fe1
commit
aac8709881
|
@ -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);
|
: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 DnsRecordVal [ /ip/dns/static/get $DnsRecord ];
|
||||||
:local MacAddress [ $CharacterReplace ($DnsRecordVal->"comment") $CommentPrefix "" ];
|
:local MacAddress [ $CharacterReplace ($DnsRecordVal->"comment") $CommentPrefix "" ];
|
||||||
:if ([ :len [ /ip/dhcp-server/lease/find where mac-address=$MacAddress address=($DnsRecordVal->"address") status=bound ] ] > 0) do={
|
: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;
|
:local Found false;
|
||||||
$LogPrintExit2 info $0 ("Lease expired for " . $MacAddress . " (" . $DnsRecordVal->"name" . "), deleting DNS entry.") false;
|
$LogPrintExit2 info $0 ("Lease expired for " . $MacAddress . " (" . $DnsRecordVal->"name" . "), deleting DNS entry.") false;
|
||||||
/ip/dns/static/remove $DnsRecord;
|
/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={
|
:if ([ :len ($LeaseVal->"address") ] > 0) do={
|
||||||
:local Comment ($CommentPrefix . $LeaseVal->"mac-address");
|
:local Comment ($CommentPrefix . $LeaseVal->"mac-address");
|
||||||
:local HostName [ $IfThenElse ([ :len ($LeaseVal->"host-name") ] = 0) \
|
:local MacDash [ $CharacterReplace ($LeaseVal->"mac-address") ":" "-" ];
|
||||||
[ $CharacterReplace ($LeaseVal->"mac-address") ":" "-" ] \
|
:local HostName [ $CharacterReplace ($LeaseVal->"host-name") " " "" ];
|
||||||
[ $CharacterReplace ($LeaseVal->"host-name") " " "" ] ];
|
|
||||||
:local Domain ([ $IfThenElse ($ServerNameInZone = true) ($LeaseVal->"server" . ".") ] . $Zone);
|
: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={
|
:if ([ :len $DnsRecord ] > 0) do={
|
||||||
:local DnsIp [ /ip/dns/static/get $DnsRecord address ];
|
:local DnsIp [ /ip/dns/static/get $DnsRecord address ];
|
||||||
|
|
||||||
|
@ -82,14 +82,17 @@ $ScriptLock $0 false 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
:if ($DnsIp = $LeaseVal->"address") do={
|
: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={
|
} else={
|
||||||
$LogPrintExit2 info $0 ("Replacing DNS entry for " . ($HostName . "." . $Domain) . ", new address is " . $LeaseVal->"address" . ".") false;
|
$LogPrintExit2 info $0 ("Replacing DNS entry for " . ($MacDash . "." . $Domain) . ", new address is " . $LeaseVal->"address" . ".") false;
|
||||||
/ip/dns/static/set name=($HostName . "." . $Domain) address=($LeaseVal->"address") ttl=$Ttl comment=$Comment $DnsRecord;
|
/ip/dns/static/set name=($MacDash . "." . $Domain) address=($LeaseVal->"address") ttl=$Ttl comment=$Comment $DnsRecord;
|
||||||
}
|
}
|
||||||
} else={
|
} else={
|
||||||
$LogPrintExit2 info $0 ("Adding new DNS entry for " . ($HostName . "." . $Domain) . ", address is " . $LeaseVal->"address" . ".") false;
|
$LogPrintExit2 info $0 ("Adding new DNS entry for " . ($MacDash . "." . $Domain) . ", address is " . $LeaseVal->"address" . ".") false;
|
||||||
/ip/dns/static/add name=($HostName . "." . $Domain) address=($LeaseVal->"address") ttl=$Ttl comment=$Comment place-before=$PlaceBefore;
|
/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={
|
} else={
|
||||||
$LogPrintExit2 debug $0 ("No address available... Ignoring.") false;
|
$LogPrintExit2 debug $0 ("No address available... Ignoring.") false;
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
:local 0 "global-functions";
|
:local 0 "global-functions";
|
||||||
|
|
||||||
# expected configuration version
|
# expected configuration version
|
||||||
:global ExpectedConfigVersion 96;
|
:global ExpectedConfigVersion 97;
|
||||||
|
|
||||||
# global variables not to be changed by user
|
# global variables not to be changed by user
|
||||||
:global GlobalFunctionsReady false;
|
:global GlobalFunctionsReady false;
|
||||||
|
|
|
@ -10,9 +10,10 @@
|
||||||
# Changes for global-config to be added to notification on script updates
|
# Changes for global-config to be added to notification on script updates
|
||||||
:global GlobalConfigChanges {
|
:global GlobalConfigChanges {
|
||||||
96="Added support for notes in 'netwatch-notify', these are included verbatim into the notification.";
|
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
|
# Migration steps to be applied on script updates
|
||||||
:global GlobalConfigMigration {
|
: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; }";
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue