routeros-scripts-main/hotspot-to-wpa.template.rsc

58 lines
1.9 KiB
Text
Raw Normal View History

#!rsc by RouterOS
# RouterOS script: hotspot-to-wpa%TEMPL%
2026-01-01 13:47:40 +01:00
# Copyright (c) 2019-2026 Christian Hesse <mail@eworm.de>
# https://rsc.eworm.de/COPYING.md
#
# requires RouterOS, version=7.21
# requires device-mode, hotspot
# requires policy, policy=read;write
#
# add private WPA passphrase after hotspot login
# https://rsc.eworm.de/doc/hotspot-to-wpa.md
#
# !! This is just a template to generate the real script!
# !! Pattern '%TEMPL%' is replaced, paths are filtered.
:local ExitOK false;
:onerror Err {
:local ScriptName [ :jobname ];
:local Address $"address";
:local Interface $"interface";
:local MacAddress $"mac-address";
:local UserName $"username";
:local Hotspot [ /ip/hotspot/host/get [ find where mac-address=$MacAddress authorized ] server ];
:if ([ :len [ /caps-man/access-list/find where \
:if ([ :len [ /interface/wifi/access-list/find where \
comment=("hotspot-to-wpa template " . $Hotspot) disabled action="reject" ] ] > 0) do={
:log info ($ScriptName . ": Ignoring login for " . $MacAddress . " on hotspot '" . $Hotspot . "'.");
:set ExitOK true;
:error false;
}
:local Lease [ /ip/dhcp-server/lease/find where mac-address=$MacAddress address=$Address ];
:if ([ :len $Lease ] != 1) do={
:log warning ($ScriptName . ": Did not find exactly one lease for " . $MacAddress . "!");
:set ExitOK true;
:error false;
}
2026-07-31 23:38:04 +02:00
:local Hotspot [ /ip/hotspot/host/get [ find where mac-address=$MacAddress authorized ] server ];
/ip/dhcp-server/lease/set \
comment=[ :serialize to=json ({ \
"hotspot-to-wpa"=true; \
2026-07-31 23:38:04 +02:00
"hotspot"=$Hotspot; \
"address"=$Address; \
"interface"=$Interface; \
"mac-address"=$MacAddress; \
"username"=$UserName }) ] $Lease;
/ip/dhcp-server/lease/make-static $Lease;
:delay 1s;
/ip/dhcp-server/lease/disable $Lease;
} do={
:if ($ExitOK = false) do={ :log error ([ :jobname ] . ": " . $Err); }
}