introduce 'dhcpv6-client-lease' to run scripts on lease

This commit is contained in:
Christian Hesse 2026-04-13 16:27:40 +02:00
parent 79cf89e1aa
commit 0e833b1d87
5 changed files with 128 additions and 1 deletions

View file

@ -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`)

60
dhcpv6-client-lease.rsc Normal file
View file

@ -0,0 +1,60 @@
#!rsc by RouterOS
# RouterOS script: dhcpv6-client-lease
# Copyright (c) 2026 Christian Hesse <mail@eworm.de>
# 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;
}

View file

@ -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)

View file

@ -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;

View file

@ -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