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