2020-09-18 11:00:27 +02:00
|
|
|
#!rsc by RouterOS
|
2018-07-05 15:29:26 +02:00
|
|
|
# RouterOS script: lease-script
|
2023-01-02 23:33:49 +01:00
|
|
|
# Copyright (c) 2013-2023 Christian Hesse <mail@eworm.de>
|
2020-06-19 22:17:42 +02:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
2018-07-05 15:29:26 +02:00
|
|
|
#
|
|
|
|
# run scripts on DHCP lease
|
2020-03-27 21:47:26 +01:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/lease-script.md
|
2018-07-05 15:29:26 +02:00
|
|
|
|
2021-02-22 15:14:10 +01:00
|
|
|
:local 0 "lease-script";
|
2021-02-18 14:52:47 +01:00
|
|
|
:global GlobalFunctionsReady;
|
|
|
|
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
|
|
|
|
|
2023-01-20 08:58:46 +01:00
|
|
|
:global Grep;
|
2021-06-23 00:53:19 +02:00
|
|
|
:global IfThenElse;
|
2021-06-24 17:17:43 +02:00
|
|
|
:global LogPrintExit2;
|
2021-07-08 16:05:11 +02:00
|
|
|
:global ParseKeyValueStore;
|
2021-07-15 11:45:35 +02:00
|
|
|
:global ScriptLock;
|
2020-02-24 20:09:51 +01:00
|
|
|
|
2019-07-05 23:38:44 +02:00
|
|
|
:if ([ :typeof $leaseActIP ] = "nothing" || \
|
|
|
|
[ :typeof $leaseActMAC ] = "nothing" || \
|
|
|
|
[ :typeof $leaseServerName ] = "nothing" || \
|
|
|
|
[ :typeof $leaseBound ] = "nothing") do={
|
2021-02-22 15:14:10 +01:00
|
|
|
$LogPrintExit2 error $0 ("This script is supposed to run from ip dhcp-server.") true;
|
2019-07-05 23:38:44 +02:00
|
|
|
}
|
|
|
|
|
2021-08-30 16:03:29 +02:00
|
|
|
$LogPrintExit2 debug $0 ("DHCP Server " . $leaseServerName . " " . [ $IfThenElse ($leaseBound = 0) \
|
|
|
|
"de" "" ] . "assigned lease " . $leaseActIP . " to " . $leaseActMAC) false;
|
|
|
|
|
2021-07-15 11:45:35 +02:00
|
|
|
$ScriptLock $0 false 10;
|
|
|
|
|
2022-05-10 15:06:01 +02:00
|
|
|
:if ([ :len [ /system/script/job/find where script=$0 ] ] > 1) do={
|
2021-07-15 11:45:35 +02:00
|
|
|
$LogPrintExit2 debug $0 ("More invocations are waiting, exiting early.") true;
|
|
|
|
}
|
|
|
|
|
2022-07-13 11:33:38 +02:00
|
|
|
:local RunOrder ({});
|
2018-07-05 15:29:26 +02:00
|
|
|
|
2022-05-10 15:06:01 +02:00
|
|
|
:foreach Script in=[ /system/script/find where source~("\n# provides: lease-script, ") ] do={
|
2023-01-20 08:58:46 +01:00
|
|
|
:local ScriptVal [ /system/script/get $Script ];
|
|
|
|
:local Store [ $ParseKeyValueStore [ $Grep ($ScriptVal->"source") "# provides: lease-script, " ] ];
|
2021-07-08 16:05:11 +02:00
|
|
|
|
2023-01-20 08:58:46 +01:00
|
|
|
:set ($RunOrder->($Store->"order")) ($ScriptVal->"name");
|
2021-07-08 16:05:11 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 17:51:55 +02:00
|
|
|
:foreach Order,Script in=$RunOrder do={
|
2021-07-15 11:45:35 +02:00
|
|
|
:do {
|
|
|
|
$LogPrintExit2 debug $0 ("Running script with order " . $Order . ": " . $Script) false;
|
2022-05-10 15:06:01 +02:00
|
|
|
/system/script/run $Script;
|
2021-07-15 11:45:35 +02:00
|
|
|
} on-error={
|
|
|
|
$LogPrintExit2 warning $0 ("Running script '" . $Script . "' failed!") false;
|
2021-06-23 01:37:52 +02:00
|
|
|
}
|
2018-07-05 15:29:26 +02:00
|
|
|
}
|