diff --git a/ipv6-dhcp-client-script.rsc b/ipv6-dhcp-client-script.rsc new file mode 100644 index 00000000..c1b2ede1 --- /dev/null +++ b/ipv6-dhcp-client-script.rsc @@ -0,0 +1,59 @@ +#!rsc by RouterOS +# RouterOS script: ipv6-dhcp-client-script +# Copyright (c) 2026 Christian Hesse +# https://rsc.eworm.de/COPYING.md +# +# requires RouterOS, version=7.22 +# +# TODO +# https://rsc.eworm.de/doc/ipv6-dhcp-client-script.md + +:onerror Err { + :global GlobalConfigReady; :global GlobalFunctionsReady; + :retry { :if ($GlobalConfigReady != true || $GlobalFunctionsReady != true) \ + do={ :error ("Global config and/or functions not ready."); }; } delay=500ms max=50; + :local ScriptName [ :jobname ]; + + :global Grep; + :global LogPrint; + :global ParseKeyValueStore; + :global ScriptLock; + + :if ([ $ScriptLock $ScriptName ] = false) do={ + :exit; + } + + :if (([ :typeof $"na-address" ] = "nothing" || [ :typeof $"na-valid" ] = "nothing") && \ + ([ :typeof $"pd-prefix" ] = "nothing" || [ :typeof $"pd-valid" ] = "nothing")) do={ + $LogPrint error $ScriptName ("This script is supposed to run from ipv6 dhcp-client."); + :exit; + } + + :global IPv6DHCPClientEnvVars { + "na-address"=$"na-address"; + "na-valid"=$"na-valid"; + "pd-prefix"=$"pd-prefix"; + "pd-valid"=$"pd-valid" }; + + :local RunOrder ({}); + :foreach Script in=[ /system/script/find where source~("\n# provides: ipv6-dhcp-client-script\\b") ] do={ + :local ScriptVal [ /system/script/get $Script ]; + :local Store [ $ParseKeyValueStore [ $Grep ($ScriptVal->"source") ("\23 provides: ipv6-dhcp-client-script, ") ] ]; + + :set ($RunOrder->($Store->"order" . "-" . $ScriptVal->"name")) ($ScriptVal->"name"); + } + + :foreach Order,Script in=$RunOrder do={ + :onerror Err { + $LogPrint debug $ScriptName ("Running script with order " . $Order . ": " . $Script); + /system/script/run $Script; + } do={ + $LogPrint warning $ScriptName ("Running script '" . $Script . "' failed: " . $Err); + } + } + + :set IPv6DHCPClientEnvVars; +} do={ + :global IPv6DHCPClientEnvVars; :set IPv6DHCPClientEnvVars; + :global ExitOnError; $ExitOnError [ :jobname ] $Err; +}