aboutsummaryrefslogtreecommitdiffstats
path: root/lease-script.rsc
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2023-03-07 10:57:42 +0100
committerGravatar Christian Hesse <mail@eworm.de>2023-03-07 22:26:01 +0100
commita832fd04ef085ef7e85000843a8e8fa59ce36dfb (patch)
treec977ca4b5167d8aa07dbe6bfe15fca8f467db0d9 /lease-script.rsc
parent1e6e0646e256c4acb9f8470853d0699286af010e (diff)
rename scripts and add file extension ".rsc"change-95
No functional change for the user... The migration is done automatically.
Diffstat (limited to 'lease-script.rsc')
-rw-r--r--lease-script.rsc51
1 files changed, 51 insertions, 0 deletions
diff --git a/lease-script.rsc b/lease-script.rsc
new file mode 100644
index 0000000..346d52b
--- /dev/null
+++ b/lease-script.rsc
@@ -0,0 +1,51 @@
+#!rsc by RouterOS
+# RouterOS script: lease-script
+# Copyright (c) 2013-2023 Christian Hesse <mail@eworm.de>
+# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
+#
+# run scripts on DHCP lease
+# https://git.eworm.de/cgit/routeros-scripts/about/doc/lease-script.md
+
+:local 0 "lease-script";
+:global GlobalFunctionsReady;
+:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
+
+:global Grep;
+:global IfThenElse;
+:global LogPrintExit2;
+:global ParseKeyValueStore;
+:global ScriptLock;
+
+:if ([ :typeof $leaseActIP ] = "nothing" || \
+ [ :typeof $leaseActMAC ] = "nothing" || \
+ [ :typeof $leaseServerName ] = "nothing" || \
+ [ :typeof $leaseBound ] = "nothing") do={
+ $LogPrintExit2 error $0 ("This script is supposed to run from ip dhcp-server.") true;
+}
+
+$LogPrintExit2 debug $0 ("DHCP Server " . $leaseServerName . " " . [ $IfThenElse ($leaseBound = 0) \
+ "de" "" ] . "assigned lease " . $leaseActIP . " to " . $leaseActMAC) false;
+
+$ScriptLock $0 false 10;
+
+:if ([ :len [ /system/script/job/find where script=$0 ] ] > 1) do={
+ $LogPrintExit2 debug $0 ("More invocations are waiting, exiting early.") true;
+}
+
+:local RunOrder ({});
+
+:foreach Script in=[ /system/script/find where source~("\n# provides: lease-script, ") ] do={
+ :local ScriptVal [ /system/script/get $Script ];
+ :local Store [ $ParseKeyValueStore [ $Grep ($ScriptVal->"source") "# provides: lease-script, " ] ];
+
+ :set ($RunOrder->($Store->"order")) ($ScriptVal->"name");
+}
+
+:foreach Order,Script in=$RunOrder do={
+ :do {
+ $LogPrintExit2 debug $0 ("Running script with order " . $Order . ": " . $Script) false;
+ /system/script/run $Script;
+ } on-error={
+ $LogPrintExit2 warning $0 ("Running script '" . $Script . "' failed!") false;
+ }
+}