aboutsummaryrefslogtreecommitdiffstats
path: root/lease-script
blob: 72f96ebe72336d115f07092b99976c8d56c5a61c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!rsc by RouterOS
# RouterOS script: lease-script
# Copyright (c) 2013-2021 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 LogPrintExit2;
:global IfThenElse;

: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;
}

:local State ([ $IfThenElse ($leaseBound = 0) "de" "" ] . "assign");

:log debug ("DHCP Server " . $leaseServerName . " " . \
  $State . "ed lease " . $leaseActIP . " to " . $leaseActMAC);

# delay a moment to update the lease table, do not run in parallel for de/assign
:delay ((1 + $leaseBound) . "s");

:foreach Script in=[ / system script find where source~("\n# provides: lease-script " . $State . "\n") ] do={
  :local ScriptName [ / system script get $Script name ];
  :do {
    :log debug ("Running script from lease-script: " . $ScriptName);
    / system script run $Script;
  } on-error={
    :log warning ("Running script '" . $ScriptName . "' from lease-script failed!");
  }
}