update-gre-address: move code into function

This commit is contained in:
Christian Hesse 2024-03-04 13:48:01 +01:00
parent 6fd745fc0f
commit ad623f069e

View file

@ -9,29 +9,34 @@
# ipsec remote peer # ipsec remote peer
# https://git.eworm.de/cgit/routeros-scripts/about/doc/update-gre-address.md # https://git.eworm.de/cgit/routeros-scripts/about/doc/update-gre-address.md
:local 0 [ :jobname ];
:global GlobalFunctionsReady; :global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; } :while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
:global CharacterReplace; :local Main do={
:global LogPrintExit2; :local ScriptName [ :tostr $1 ];
:global ScriptLock;
$ScriptLock $0; :global CharacterReplace;
:global LogPrintExit2;
:global ScriptLock;
/interface/gre/set remote-address=0.0.0.0 disabled=yes [ find where !running !disabled ]; $ScriptLock $ScriptName;
:foreach Peer in=[ /ip/ipsec/active-peers/find ] do={ /interface/gre/set remote-address=0.0.0.0 disabled=yes [ find where !running !disabled ];
:local PeerVal [ /ip/ipsec/active-peers/get $Peer ];
:local GreInt [ /interface/gre/find where comment=($PeerVal->"id") or comment=[ $CharacterReplace ($PeerVal->"id") "CN=" "" ] ]; :foreach Peer in=[ /ip/ipsec/active-peers/find ] do={
:if ([ :len $GreInt ] > 0) do={ :local PeerVal [ /ip/ipsec/active-peers/get $Peer ];
:local GreIntVal [ /interface/gre/get $GreInt ]; :local GreInt [ /interface/gre/find where comment=($PeerVal->"id") or comment=[ $CharacterReplace ($PeerVal->"id") "CN=" "" ] ];
:if ([ :typeof ($PeerVal->"dynamic-address") ] = "str" && \ :if ([ :len $GreInt ] > 0) do={
($PeerVal->"dynamic-address" != $GreIntVal->"remote-address" || \ :local GreIntVal [ /interface/gre/get $GreInt ];
$GreIntVal->"disabled" = true)) do={ :if ([ :typeof ($PeerVal->"dynamic-address") ] = "str" && \
$LogPrintExit2 info $0 ("Updating remote address for interface " . $GreIntVal->"name" . " to " . $PeerVal->"dynamic-address") false; ($PeerVal->"dynamic-address" != $GreIntVal->"remote-address" || \
/interface/gre/set remote-address=0.0.0.0 disabled=yes [ find where remote-address=$PeerVal->"dynamic-address" name!=$GreIntVal->"name" ]; $GreIntVal->"disabled" = true)) do={
/interface/gre/set $GreInt remote-address=($PeerVal->"dynamic-address") disabled=no; $LogPrintExit2 info $ScriptName ("Updating remote address for interface " . $GreIntVal->"name" . " to " . $PeerVal->"dynamic-address") false;
/interface/gre/set remote-address=0.0.0.0 disabled=yes [ find where remote-address=$PeerVal->"dynamic-address" name!=$GreIntVal->"name" ];
/interface/gre/set $GreInt remote-address=($PeerVal->"dynamic-address") disabled=no;
}
} }
} }
} }
$Main [ :jobname ];