2023-03-07 10:57:42 +01:00
|
|
|
#!rsc by RouterOS
|
|
|
|
# RouterOS script: update-gre-address
|
2025-01-02 00:04:06 +01:00
|
|
|
# Copyright (c) 2013-2025 Christian Hesse <mail@eworm.de>
|
2025-01-24 20:46:11 +01:00
|
|
|
# https://rsc.eworm.de/COPYING.md
|
2023-03-07 10:57:42 +01:00
|
|
|
#
|
2025-02-07 17:39:48 +01:00
|
|
|
# requires RouterOS, version=7.15
|
2023-11-15 21:31:19 +01:00
|
|
|
#
|
2023-03-07 10:57:42 +01:00
|
|
|
# update gre interface remote address with dynamic address from
|
|
|
|
# ipsec remote peer
|
2025-01-24 20:46:11 +01:00
|
|
|
# https://rsc.eworm.de/doc/update-gre-address.md
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-12-06 10:31:52 +01:00
|
|
|
:local ExitOK false;
|
2025-05-06 09:44:23 +02:00
|
|
|
:onerror Err {
|
2025-05-06 14:08:37 +02:00
|
|
|
:global GlobalConfigReady; :global GlobalFunctionsReady;
|
|
|
|
:retry { :if ($GlobalConfigReady != true || $GlobalFunctionsReady != true) \
|
|
|
|
do={ :error ("Global config and/or functions not ready."); }; } delay=500ms max=50;
|
2024-03-06 15:28:55 +01:00
|
|
|
:local ScriptName [ :jobname ];
|
2023-06-13 09:06:11 +02:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:global CharacterReplace;
|
2024-03-08 12:45:38 +01:00
|
|
|
:global LogPrint;
|
2024-03-04 13:48:01 +01:00
|
|
|
:global ScriptLock;
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-05 16:12:36 +01:00
|
|
|
:if ([ $ScriptLock $ScriptName ] = false) do={
|
2024-12-06 10:31:52 +01:00
|
|
|
:set ExitOK true;
|
2024-03-06 15:28:55 +01:00
|
|
|
:error false;
|
2024-03-05 16:12:36 +01:00
|
|
|
}
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
/interface/gre/set remote-address=0.0.0.0 disabled=yes [ find where !running !disabled ];
|
|
|
|
|
|
|
|
:foreach Peer in=[ /ip/ipsec/active-peers/find ] do={
|
|
|
|
:local PeerVal [ /ip/ipsec/active-peers/get $Peer ];
|
|
|
|
:local GreInt [ /interface/gre/find where comment=($PeerVal->"id") or comment=[ $CharacterReplace ($PeerVal->"id") "CN=" "" ] ];
|
|
|
|
:if ([ :len $GreInt ] > 0) do={
|
|
|
|
:local GreIntVal [ /interface/gre/get $GreInt ];
|
|
|
|
:if ([ :typeof ($PeerVal->"dynamic-address") ] = "str" && \
|
|
|
|
($PeerVal->"dynamic-address" != $GreIntVal->"remote-address" || \
|
|
|
|
$GreIntVal->"disabled" = true)) do={
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint info $ScriptName ("Updating remote address for interface " . $GreIntVal->"name" . " to " . $PeerVal->"dynamic-address");
|
2024-03-04 13:48:01 +01:00
|
|
|
/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;
|
|
|
|
}
|
2023-03-07 10:57:42 +01:00
|
|
|
}
|
|
|
|
}
|
2025-05-06 09:44:23 +02:00
|
|
|
} do={
|
|
|
|
:global ExitError; $ExitError $ExitOK [ :jobname ] $Err;
|
2024-12-06 10:31:52 +01:00
|
|
|
}
|