From 0e833b1d8742f0b0c458d1f3bff2584914a77838 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Mon, 13 Apr 2026 16:27:40 +0200 Subject: [PATCH 1/2] introduce 'dhcpv6-client-lease' to run scripts on lease --- README.md | 1 + dhcpv6-client-lease.rsc | 60 +++++++++++++++++++++++++++++++++++ doc/dhcpv6-client-lease.md | 65 ++++++++++++++++++++++++++++++++++++++ global-functions.rsc | 2 +- news-and-changes.rsc | 1 + 5 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 dhcpv6-client-lease.rsc create mode 100644 doc/dhcpv6-client-lease.md diff --git a/README.md b/README.md index b3759eb1..e739756b 100644 --- a/README.md +++ b/README.md @@ -283,6 +283,7 @@ Available scripts * [Comment DHCP leases with info from access list](doc/dhcp-lease-comment.md) (`dhcp-lease-comment`) * [Create DNS records for DHCP leases](doc/dhcp-to-dns.md) (`dhcp-to-dns`) * [Run other scripts on IPv4 DHCP server lease](doc/dhcpv4-server-lease.md) (`dhcpv4-server-lease`) +* [Run other scripts on IPv6 DHCP client lease](doc/dhcpv6-client-lease.md) (`dhcpv6-client-lease`) * [Automatically upgrade firmware and reboot](doc/firmware-upgrade-reboot.md) (`firmware-upgrade-reboot`) * [Download, import and update firewall address-lists](doc/fw-addr-lists.md) (`fw-addr-lists`) * [Wait for global functions und modules](doc/global-wait.md) (`global-wait`) diff --git a/dhcpv6-client-lease.rsc b/dhcpv6-client-lease.rsc new file mode 100644 index 00000000..2a70a977 --- /dev/null +++ b/dhcpv6-client-lease.rsc @@ -0,0 +1,60 @@ +#!rsc by RouterOS +# RouterOS script: dhcpv6-client-lease +# Copyright (c) 2026 Christian Hesse +# https://rsc.eworm.de/COPYING.md +# +# requires RouterOS, version=7.22 +# +# run scripts on IPv6 DHCP client lease +# https://rsc.eworm.de/doc/dhcpv6-client-lease.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 10 ] = 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 DHCPv6ClientLeaseVars { + "na-address"=$"na-address"; + "na-valid"=$"na-valid"; + "pd-prefix"=$"pd-prefix"; + "pd-valid"=$"pd-valid"; + "options"=$"options" }; + + :local RunOrder ({}); + :foreach Script in=[ /system/script/find where source~("\n# provides: dhcpv6-client-lease\\b") ] do={ + :local ScriptVal [ /system/script/get $Script ]; + :local Store [ $ParseKeyValueStore [ $Grep ($ScriptVal->"source") ("\23 provides: dhcpv6-client-lease, ") ] ]; + + :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 DHCPv6ClientLeaseVars; +} do={ + :global DHCPv6ClientLeaseVars; :set DHCPv6ClientLeaseVars; + :global ExitOnError; $ExitOnError [ :jobname ] $Err; +} diff --git a/doc/dhcpv6-client-lease.md b/doc/dhcpv6-client-lease.md new file mode 100644 index 00000000..6e8cb28c --- /dev/null +++ b/doc/dhcpv6-client-lease.md @@ -0,0 +1,65 @@ +Run other scripts on IPv6 DHCP client lease +=========================================== + +[![GitHub stars](https://img.shields.io/github/stars/eworm-de/routeros-scripts?logo=GitHub&style=flat&color=red)](https://github.com/eworm-de/routeros-scripts/stargazers) +[![GitHub forks](https://img.shields.io/github/forks/eworm-de/routeros-scripts?logo=GitHub&style=flat&color=green)](https://github.com/eworm-de/routeros-scripts/network) +[![GitHub watchers](https://img.shields.io/github/watchers/eworm-de/routeros-scripts?logo=GitHub&style=flat&color=blue)](https://github.com/eworm-de/routeros-scripts/watchers) +[![required RouterOS version](https://img.shields.io/badge/RouterOS-7.22-yellow?style=flat)](https://mikrotik.com/download/changelogs/) +[![Telegram group @routeros_scripts](https://img.shields.io/badge/Telegram-%40routeros__scripts-%2326A5E4?logo=telegram&style=flat)](https://t.me/routeros_scripts) +[![donate with PayPal](https://img.shields.io/badge/Like_it%3F-Donate!-orange?logo=githubsponsors&logoColor=orange&style=flat)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=A4ZXBD6YS2W8J) + +[⬅️ Go back to main README](../README.md) + +> ℹ️ **Info**: This script can not be used on its own but requires the base +> installation. See [main README](../README.md) for details. + +Description +----------- + +This script is supposed to run from IPv6 DHCP client as lease script. On a +DHCP leasse it runs each script containing the following line, where `##` is +a decimal number for ordering: + + # provides: dhcpv6-client-lease, order=## + +The lease script is started with some variables injected, but these are not +available in child scripts. However this script makes these variables +available with a global variable. This code is required in child script: + + :global EitherOr; + + :global DHCPv6ClientLeaseVars; + + :local NaAddress [ $EitherOr $"na-address" ($DHCPv6ClientLeaseVars->"na-address") ]; + :local NaValid [ $EitherOr $"na-valid" ($DHCPv6ClientLeaseVars->"na-valid") ]; + :local PdPrefix [ $EitherOr $"pd-prefix" ($DHCPv6ClientLeaseVars->"pd-prefix") ]; + :local PdValid [ $EitherOr $"pd-valid" ($DHCPv6ClientLeaseVars->"pd-valid") ]; + :local Options [ $EitherOr $"options" ($DHCPv6ClientLeaseVars->"options") ]; + +The values are available under different name then, use `$PdPrefix` instead +of `$"pd-prefix"`, and so on. The resulting script supports both, being a +lease script itself or being run as child. + +Currently it runs if available, in order: + +* [ipv6-update](ipv6-update.md) + +Requirements and installation +----------------------------- + +Just install the script: + + $ScriptInstallUpdate dhcpv6-client-lease; + +... and add it as `lease-script` to your dhcp client: + + /ipv6/dhcp-client/set lease-script="dhcpv6-client-lease" [ find ]; + +See also +-------- + +* [Update configuration on IPv6 prefix change](ipv6-update.md) + +--- +[⬅️ Go back to main README](../README.md) +[⬆️ Go back to top](#top) diff --git a/global-functions.rsc b/global-functions.rsc index 9648b92f..d7f1aa70 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -15,7 +15,7 @@ # Git commit id & info, expected configuration version :global CommitId "unknown"; :global CommitInfo "unknown"; -:global ExpectedConfigVersion 140; +:global ExpectedConfigVersion 141; # global variables not to be changed by user :global GlobalFunctionsReady false; diff --git a/news-and-changes.rsc b/news-and-changes.rsc index 48558deb..109244ea 100644 --- a/news-and-changes.rsc +++ b/news-and-changes.rsc @@ -65,6 +65,7 @@ 138="RouterOS 7.19 is suffering an issue with certificate store. Fixing trust state for all certificates..."; 139="Certificate Authorities will reduce the leaf certificate validity times soon. Thus the defaults for renewal and warning in 'check-certificates' were decreased."; 140="The scripts 'lease-script' was renamed to 'dhcpv4-server-lease', configuration was updated automatically."; + 141="Introduced script 'dhcpv6-client-lease' to run several scripts on IPv6 DHCP client lease."; }; # Migration steps to be applied on script updates From c2d458fa716269e77678c31c6df5b84d75a6b6cd Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Mon, 13 Apr 2026 16:28:31 +0200 Subject: [PATCH 2/2] ipv6-update: support dhcpv6-client-lease script... ... by adding a provide, and handling global variable $DHCPv6ClientLeaseVars. --- doc/ipv6-update.md | 1 + doc/ppp-on-up.md | 1 + ipv6-update.rsc | 12 ++++++++---- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/doc/ipv6-update.md b/doc/ipv6-update.md index 672b5fac..a8d2c7e7 100644 --- a/doc/ipv6-update.md +++ b/doc/ipv6-update.md @@ -77,6 +77,7 @@ start with "`ipv6-pool-`" and actual pool name, followed by a comma, See also -------- +* [Run other scripts on IPv6 DHCP client lease](dhcpv6-client-lease.md) * [Run scripts on ppp connection](ppp-on-up.md) --- diff --git a/doc/ppp-on-up.md b/doc/ppp-on-up.md index 51c8bc96..e492eb1b 100644 --- a/doc/ppp-on-up.md +++ b/doc/ppp-on-up.md @@ -36,6 +36,7 @@ Just install the script: See also -------- +* [Run other scripts on IPv6 DHCP client lease](dhcpv6-client-lease.md) * [Update configuration on IPv6 prefix change](ipv6-update.md) * [Update tunnelbroker configuration](update-tunnelbroker.md) diff --git a/ipv6-update.rsc b/ipv6-update.rsc index 7b4e1c3f..54c08179 100644 --- a/ipv6-update.rsc +++ b/ipv6-update.rsc @@ -4,6 +4,7 @@ # https://rsc.eworm.de/COPYING.md # # requires RouterOS, version=7.22 +# provides: dhcpv6-client-lease, order=40 # # update firewall and dns settings on IPv6 prefix change # https://rsc.eworm.de/doc/ipv6-update.md @@ -14,14 +15,17 @@ do={ :error ("Global config and/or functions not ready."); }; } delay=500ms max=50; :local ScriptName [ :jobname ]; + :global EitherOr; :global LogPrint; :global ParseKeyValueStore; :global ScriptLock; - :local NaAddress $"na-address"; - :local NaValid $"na-valid"; - :local PdPrefix $"pd-prefix"; - :local PdValid $"pd-valid"; + :global DHCPv6ClientLeaseVars; + + :local NaAddress [ $EitherOr $"na-address" ($DHCPv6ClientLeaseVars->"na-address") ]; + :local NaValid [ $EitherOr $"na-valid" ($DHCPv6ClientLeaseVars->"na-valid") ]; + :local PdPrefix [ $EitherOr $"pd-prefix" ($DHCPv6ClientLeaseVars->"pd-prefix") ]; + :local PdValid [ $EitherOr $"pd-valid" ($DHCPv6ClientLeaseVars->"pd-valid") ]; :if ([ $ScriptLock $ScriptName ] = false) do={ :exit;