mirror of
https://github.com/eworm-de/routeros-scripts.git
synced 2025-06-23 10:18:50 +02:00
dhcp-to-dns: move code into function
This commit is contained in:
parent
82ec11f2fc
commit
29f544d18d
1 changed files with 93 additions and 88 deletions
|
@ -9,10 +9,12 @@
|
||||||
# check DHCP leases and add/remove/update DNS entries
|
# check DHCP leases and add/remove/update DNS entries
|
||||||
# https://git.eworm.de/cgit/routeros-scripts/about/doc/dhcp-to-dns.md
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/dhcp-to-dns.md
|
||||||
|
|
||||||
:local 0 [ :jobname ];
|
|
||||||
:global GlobalFunctionsReady;
|
:global GlobalFunctionsReady;
|
||||||
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
|
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
|
||||||
|
|
||||||
|
:local Main do={
|
||||||
|
:local ScriptName [ :tostr $1 ];
|
||||||
|
|
||||||
:global Domain;
|
:global Domain;
|
||||||
:global Identity;
|
:global Identity;
|
||||||
|
|
||||||
|
@ -24,15 +26,15 @@
|
||||||
:global ParseKeyValueStore;
|
:global ParseKeyValueStore;
|
||||||
:global ScriptLock;
|
:global ScriptLock;
|
||||||
|
|
||||||
$ScriptLock $0 false 10;
|
$ScriptLock $ScriptName false 10;
|
||||||
|
|
||||||
:local Ttl 5m;
|
:local Ttl 5m;
|
||||||
:local CommentPrefix ("managed by " . $0);
|
:local CommentPrefix ("managed by " . $ScriptName);
|
||||||
:local CommentString ("--- " . $0 . " above ---");
|
:local CommentString ("--- " . $ScriptName . " above ---");
|
||||||
|
|
||||||
:if ([ :len [ /ip/dns/static/find where (name=$CommentString or (comment=$CommentString and name=-)) type=NXDOMAIN disabled ] ] = 0) do={
|
: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;
|
/ip/dns/static/add name=$CommentString type=NXDOMAIN disabled=yes;
|
||||||
$LogPrintExit2 warning $0 ("Added disabled static dns record with name '" . $CommentString . "'.") false;
|
$LogPrintExit2 warning $ScriptName ("Added disabled static dns record with name '" . $CommentString . "'.") false;
|
||||||
}
|
}
|
||||||
: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);
|
||||||
|
|
||||||
|
@ -43,10 +45,10 @@ $ScriptLock $0 false 10;
|
||||||
|
|
||||||
:if ([ :len [ /ip/dhcp-server/lease/find where active-mac-address=($DnsRecordInfo->"macaddress") \
|
:if ([ :len [ /ip/dhcp-server/lease/find where active-mac-address=($DnsRecordInfo->"macaddress") \
|
||||||
active-address=($DnsRecordVal->"address") server=($DnsRecordInfo->"server") status=bound ] ] > 0) do={
|
active-address=($DnsRecordVal->"address") server=($DnsRecordInfo->"server") status=bound ] ] > 0) do={
|
||||||
$LogPrintExit2 debug $0 ("Lease for " . $MacInServer . " (" . $DnsRecordVal->"name" . ") still exists. Not deleting record.") false;
|
$LogPrintExit2 debug $ScriptName ("Lease for " . $MacInServer . " (" . $DnsRecordVal->"name" . ") still exists. Not deleting record.") false;
|
||||||
} else={
|
} else={
|
||||||
:local Found false;
|
:local Found false;
|
||||||
$LogPrintExit2 info $0 ("Lease expired for " . $MacInServer . ", deleting record (" . $DnsRecordVal->"name" . ").") false;
|
$LogPrintExit2 info $ScriptName ("Lease expired for " . $MacInServer . ", deleting record (" . $DnsRecordVal->"name" . ").") false;
|
||||||
/ip/dns/static/remove $DnsRecord;
|
/ip/dns/static/remove $DnsRecord;
|
||||||
/ip/dns/static/remove [ find where type=CNAME comment=($DnsRecordVal->"comment") ];
|
/ip/dns/static/remove [ find where type=CNAME comment=($DnsRecordVal->"comment") ];
|
||||||
}
|
}
|
||||||
|
@ -57,10 +59,10 @@ $ScriptLock $0 false 10;
|
||||||
:do {
|
:do {
|
||||||
:set LeaseVal [ /ip/dhcp-server/lease/get $Lease ];
|
:set LeaseVal [ /ip/dhcp-server/lease/get $Lease ];
|
||||||
:if ([ :len [ /ip/dhcp-server/lease/find where active-mac-address=($LeaseVal->"active-mac-address") status=bound ] ] > 1) do={
|
:if ([ :len [ /ip/dhcp-server/lease/find where active-mac-address=($LeaseVal->"active-mac-address") status=bound ] ] > 1) do={
|
||||||
$LogPrintOnce info $0 ("Multiple bound leases found for mac-address " . ($LeaseVal->"active-mac-address") . "!") false;
|
$LogPrintOnce info $ScriptName ("Multiple bound leases found for mac-address " . ($LeaseVal->"active-mac-address") . "!") false;
|
||||||
}
|
}
|
||||||
} on-error={
|
} on-error={
|
||||||
$LogPrintExit2 debug $0 ("A lease just vanished, ignoring.") false;
|
$LogPrintExit2 debug $ScriptName ("A lease just vanished, ignoring.") false;
|
||||||
}
|
}
|
||||||
|
|
||||||
:if ([ :len ($LeaseVal->"active-address") ] > 0) do={
|
:if ([ :len ($LeaseVal->"active-address") ] > 0) do={
|
||||||
|
@ -84,9 +86,9 @@ $ScriptLock $0 false 10;
|
||||||
:local DnsRecordVal [ /ip/dns/static/get $DnsRecord ];
|
:local DnsRecordVal [ /ip/dns/static/get $DnsRecord ];
|
||||||
|
|
||||||
:if ($DnsRecordVal->"address" = $LeaseVal->"active-address" && $DnsRecordVal->"name" = $FullA) do={
|
:if ($DnsRecordVal->"address" = $LeaseVal->"active-address" && $DnsRecordVal->"name" = $FullA) do={
|
||||||
$LogPrintExit2 debug $0 ("The A record for " . $MacInServer . " (" . $FullA . ") does not need updating.") false;
|
$LogPrintExit2 debug $ScriptName ("The A record for " . $MacInServer . " (" . $FullA . ") does not need updating.") false;
|
||||||
} else={
|
} else={
|
||||||
$LogPrintExit2 info $0 ("Updating A record for " . $MacInServer . " (" . $FullA . " -> " . $LeaseVal->"active-address" . ").") false;
|
$LogPrintExit2 info $ScriptName ("Updating A record for " . $MacInServer . " (" . $FullA . " -> " . $LeaseVal->"active-address" . ").") false;
|
||||||
/ip/dns/static/set address=($LeaseVal->"active-address") name=$FullA $DnsRecord;
|
/ip/dns/static/set address=($LeaseVal->"active-address") name=$FullA $DnsRecord;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,28 +96,31 @@ $ScriptLock $0 false 10;
|
||||||
:if ([ :len $CName ] > 0) do={
|
:if ([ :len $CName ] > 0) do={
|
||||||
:local CNameVal [ /ip/dns/static/get $CName ];
|
:local CNameVal [ /ip/dns/static/get $CName ];
|
||||||
:if ($CNameVal->"name" != $FullCN || $CNameVal->"cname" != $FullA) do={
|
:if ($CNameVal->"name" != $FullCN || $CNameVal->"cname" != $FullA) do={
|
||||||
$LogPrintExit2 info $0 ("Deleting CNAME record with wrong data for " . $MacInServer . ".") false;
|
$LogPrintExit2 info $ScriptName ("Deleting CNAME record with wrong data for " . $MacInServer . ".") false;
|
||||||
/ip/dns/static/remove $CName;
|
/ip/dns/static/remove $CName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
:if ([ :len $HostName ] > 0 && [ :len [ /ip/dns/static/find where name=$FullCN type=CNAME ] ] = 0) do={
|
:if ([ :len $HostName ] > 0 && [ :len [ /ip/dns/static/find where name=$FullCN type=CNAME ] ] = 0) do={
|
||||||
$LogPrintExit2 info $0 ("Adding CNAME record for " . $MacInServer . " (" . $FullCN . " -> " . $FullA . ").") false;
|
$LogPrintExit2 info $ScriptName ("Adding CNAME record for " . $MacInServer . " (" . $FullCN . " -> " . $FullA . ").") false;
|
||||||
/ip/dns/static/add name=$FullCN type=CNAME cname=$FullA ttl=$Ttl comment=$Comment place-before=$PlaceBefore;
|
/ip/dns/static/add name=$FullCN type=CNAME cname=$FullA ttl=$Ttl comment=$Comment place-before=$PlaceBefore;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else={
|
} else={
|
||||||
$LogPrintExit2 info $0 ("Adding A record for " . $MacInServer . " (" . $FullA . " -> " . $LeaseVal->"active-address" . ").") false;
|
$LogPrintExit2 info $ScriptName ("Adding A record for " . $MacInServer . " (" . $FullA . " -> " . $LeaseVal->"active-address" . ").") false;
|
||||||
/ip/dns/static/add name=$FullA type=A address=($LeaseVal->"active-address") ttl=$Ttl comment=$Comment place-before=$PlaceBefore;
|
/ip/dns/static/add name=$FullA type=A address=($LeaseVal->"active-address") ttl=$Ttl comment=$Comment place-before=$PlaceBefore;
|
||||||
:if ([ :len $HostName ] > 0 && [ :len [ /ip/dns/static/find where name=$FullCN type=CNAME ] ] = 0) do={
|
:if ([ :len $HostName ] > 0 && [ :len [ /ip/dns/static/find where name=$FullCN type=CNAME ] ] = 0) do={
|
||||||
$LogPrintExit2 info $0 ("Adding CNAME record for " . $MacInServer . " (" . $FullCN . " -> " . $FullA . ").") false;
|
$LogPrintExit2 info $ScriptName ("Adding CNAME record for " . $MacInServer . " (" . $FullCN . " -> " . $FullA . ").") false;
|
||||||
/ip/dns/static/add name=$FullCN type=CNAME cname=$FullA ttl=$Ttl comment=$Comment place-before=$PlaceBefore;
|
/ip/dns/static/add name=$FullCN type=CNAME cname=$FullA ttl=$Ttl comment=$Comment place-before=$PlaceBefore;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
:if ([ :len [ /ip/dns/static/find where name=$FullA (!type or type=A) ] ] > 1) do={
|
:if ([ :len [ /ip/dns/static/find where name=$FullA (!type or type=A) ] ] > 1) do={
|
||||||
$LogPrintOnce warning $0 ("The name '" . $FullA . "' appeared in more than one A record!") false;
|
$LogPrintOnce warning $ScriptName ("The name '" . $FullA . "' appeared in more than one A record!") false;
|
||||||
}
|
}
|
||||||
} else={
|
} else={
|
||||||
$LogPrintExit2 debug $0 ("No address available... Ignoring.") false;
|
$LogPrintExit2 debug $ScriptName ("No address available... Ignoring.") false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$Main [ :jobname ];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue