routeros-scripts/ipsec-to-dns

70 lines
3.1 KiB
Text
Raw Normal View History

2021-05-17 14:32:07 +00:00
#!rsc by RouterOS
# RouterOS script: ipsec-to-dns
2023-01-02 22:33:49 +00:00
# Copyright (c) 2021-2023 Christian Hesse <mail@eworm.de>
2021-05-17 14:32:07 +00:00
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
#
# and add/remove/update DNS entries from IPSec mode-config
# https://git.eworm.de/cgit/routeros-scripts/about/doc/ipsec-to-dns.md
:local 0 "ipsec-to-dns";
:global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
:global Domain;
:global HostNameInZone;
:global Identity;
:global PrefixInZone;
:global CharacterReplace;
:global EscapeForRegEx;
2021-05-17 14:32:07 +00:00
:global IfThenElse;
:global LogPrintExit2;
2021-05-17 14:32:07 +00:00
:local Zone \
([ $IfThenElse ($PrefixInZone = true) "ipsec." ] . \
[ $IfThenElse ($HostNameInZone = true) ($Identity . ".") ] . $Domain);
:local Ttl 5m;
:local CommentPrefix ("managed by " . $0 . " for ");
:local CommentString ("--- " . $0 . " above ---");
2022-05-10 13:01:19 +00:00
: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;
2021-05-17 14:32:07 +00:00
$LogPrintExit2 warning $0 ("Added disabled static dns record with comment '" . $CommentString . "'.") false;
}
2022-05-10 13:01:19 +00:00
:local PlaceBefore ([ /ip/dns/static/find where comment=$CommentString name=- type=NXDOMAIN disabled ]->0);
2021-05-17 14:32:07 +00:00
2022-05-10 13:01:19 +00:00
:foreach DnsRecord in=[ /ip/dns/static/find where comment ~ $CommentPrefix ] do={
:local DnsRecordVal [ /ip/dns/static/get $DnsRecord ];
2021-05-17 14:32:07 +00:00
:local PeerId [ $CharacterReplace ($DnsRecordVal->"comment") $CommentPrefix "" ];
:if ([ :len [ /ip/ipsec/active-peers/find where id~("^(CN=)?" . [ $EscapeForRegEx $PeerId ] . "\$") \
dynamic-address=($DnsRecordVal->"address") ] ] > 0) do={
2021-05-17 14:32:07 +00:00
$LogPrintExit2 debug $0 ("Peer " . $PeerId . " (" . $DnsRecordVal->"name" . ") still exists. Not deleting DNS entry.") false;
} else={
:local Found false;
$LogPrintExit2 info $0 ("Peer " . $PeerId . " (" . $DnsRecordVal->"name" . ") has gone, deleting DNS entry.") false;
2022-05-10 13:01:19 +00:00
/ip/dns/static/remove $DnsRecord;
2021-05-17 14:32:07 +00:00
}
}
2022-05-10 13:01:19 +00:00
:foreach Peer in=[ /ip/ipsec/active-peers/find where !(dynamic-address=[]) ] do={
:local PeerVal [ /ip/ipsec/active-peers/get $Peer ];
:local PeerId [ $CharacterReplace ($PeerVal->"id") "CN=" "" ];
:local Comment ($CommentPrefix . $PeerId);
:local HostName [ :pick $PeerId 0 [ :find ($PeerId . ".") "." ] ];
2021-05-17 14:32:07 +00:00
:local Fqdn ($HostName . "." . $Zone);
2022-05-10 13:01:19 +00:00
:local DnsRecord [ /ip/dns/static/find where name=$Fqdn ];
2021-05-17 14:32:07 +00:00
:if ([ :len $DnsRecord ] > 0) do={
2022-05-10 13:01:19 +00:00
:local DnsIp [ /ip/dns/static/get $DnsRecord address ];
2021-05-17 14:32:07 +00:00
:if ($DnsIp = $PeerVal->"dynamic-address") do={
$LogPrintExit2 debug $0 ("DNS entry for " . $Fqdn . " does not need updating.") false;
} else={
$LogPrintExit2 info $0 ("Replacing DNS entry for " . $Fqdn . ", new address is " . $PeerVal->"dynamic-address" . ".") false;
2022-05-10 13:01:19 +00:00
/ip/dns/static/set name=$Fqdn address=($PeerVal->"dynamic-address") ttl=$Ttl comment=$Comment $DnsRecord;
2021-05-17 14:32:07 +00:00
}
} else={
$LogPrintExit2 info $0 ("Adding new DNS entry for " . $Fqdn . ", address is " . $PeerVal->"dynamic-address" . ".") false;
2022-05-10 13:01:19 +00:00
/ip/dns/static/add name=$Fqdn address=($PeerVal->"dynamic-address") ttl=$Ttl comment=$Comment place-before=$PlaceBefore;
2021-05-17 14:32:07 +00:00
}
}