2023-11-14 10:36:22 +01:00
|
|
|
#!rsc by RouterOS
|
|
|
|
# RouterOS script: hotspot-to-wpa-cleanup.wifi
|
2025-01-02 00:04:06 +01:00
|
|
|
# Copyright (c) 2021-2025 Christian Hesse <mail@eworm.de>
|
2025-01-24 20:46:11 +01:00
|
|
|
# https://rsc.eworm.de/COPYING.md
|
2023-11-14 10:36:22 +01:00
|
|
|
#
|
|
|
|
# provides: lease-script, order=80
|
2025-02-07 17:39:48 +01:00
|
|
|
# requires RouterOS, version=7.15
|
2025-01-29 10:35:23 +01:00
|
|
|
# requires device-mode, hotspot
|
2023-11-14 10:36:22 +01:00
|
|
|
#
|
|
|
|
# manage and clean up private WPA passphrase after hotspot login
|
2025-01-24 20:46:11 +01:00
|
|
|
# https://rsc.eworm.de/doc/hotspot-to-wpa.md
|
2023-11-14 10:36:22 +01:00
|
|
|
#
|
|
|
|
# !! Do not edit this file, it is generated from template!
|
|
|
|
|
2024-12-06 10:31:52 +01:00
|
|
|
:local ExitOK false;
|
2025-05-06 09:44:23 +02:00
|
|
|
:onerror Err {
|
2025-05-06 14:08:37 +02:00
|
|
|
:global GlobalConfigReady; :global GlobalFunctionsReady;
|
|
|
|
:retry { :if ($GlobalConfigReady != true || $GlobalFunctionsReady != true) \
|
|
|
|
do={ :error ("Global config and/or functions not ready."); }; } delay=500ms max=50;
|
2024-03-06 15:28:55 +01:00
|
|
|
:local ScriptName [ :jobname ];
|
2023-11-14 10:36:22 +01:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:global EitherOr;
|
2024-03-08 12:45:38 +01:00
|
|
|
:global LogPrint;
|
2024-03-04 13:48:01 +01:00
|
|
|
:global ParseKeyValueStore;
|
|
|
|
:global ScriptLock;
|
2023-11-14 10:36:22 +01:00
|
|
|
|
2024-03-05 16:12:36 +01:00
|
|
|
:if ([ $ScriptLock $ScriptName 10 ] = false) do={
|
2024-12-06 10:31:52 +01:00
|
|
|
:set ExitOK true;
|
2024-03-06 15:28:55 +01:00
|
|
|
:error false;
|
2024-03-05 16:12:36 +01:00
|
|
|
}
|
2024-03-04 13:48:01 +01:00
|
|
|
|
|
|
|
:local DHCPServers ({});
|
|
|
|
:foreach Server in=[ /ip/dhcp-server/find where comment~"hotspot-to-wpa" ] do={
|
|
|
|
:local ServerVal [ /ip/dhcp-server/get $Server ]
|
|
|
|
:local ServerInfo [ $ParseKeyValueStore ($ServerVal->"comment") ];
|
|
|
|
:if (($ServerInfo->"hotspot-to-wpa") = "wpa") do={
|
|
|
|
:set ($DHCPServers->($ServerVal->"name")) \
|
|
|
|
[ :totime [ $EitherOr ($ServerInfo->"timeout") 4w ] ];
|
|
|
|
}
|
2023-11-14 10:36:22 +01:00
|
|
|
}
|
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:foreach Client in=[ /interface/wifi/registration-table/find where comment~"^hotspot-to-wpa:" ] do={
|
|
|
|
:local ClientVal [ /interface/wifi/registration-table/get $Client ];
|
|
|
|
:foreach Lease in=[ /ip/dhcp-server/lease/find where dynamic \
|
|
|
|
mac-address=($ClientVal->"mac-address") ] do={
|
|
|
|
:if (($DHCPServers->[ /ip/dhcp-server/lease/get $Lease server ]) > 0s) do={
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint info $ScriptName ("Client with mac address " . ($ClientVal->"mac-address") . \
|
|
|
|
" connected to WPA, making lease static.");
|
2024-03-04 13:48:01 +01:00
|
|
|
/ip/dhcp-server/lease/make-static $Lease;
|
|
|
|
/ip/dhcp-server/lease/set comment=($ClientVal->"comment") $Lease;
|
|
|
|
}
|
2023-11-14 10:36:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:foreach Client in=[ /interface/wifi/access-list/find where comment~"^hotspot-to-wpa:" \
|
2024-09-25 11:01:40 +02:00
|
|
|
!(comment~[ /system/clock/get date ]) mac-address ] do={
|
2024-03-04 13:48:01 +01:00
|
|
|
:local ClientVal [ /interface/wifi/access-list/get $Client ];
|
|
|
|
:if ([ :len [ /ip/dhcp-server/lease/find where !dynamic comment~"^hotspot-to-wpa:" \
|
|
|
|
mac-address=($ClientVal->"mac-address") ] ] = 0) do={
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint info $ScriptName ("Client with mac address " . ($ClientVal->"mac-address") . \
|
|
|
|
" did not connect to WPA, removing from access list.");
|
2024-03-04 13:48:01 +01:00
|
|
|
/interface/wifi/access-list/remove $Client;
|
|
|
|
}
|
2023-11-14 10:36:22 +01:00
|
|
|
}
|
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:foreach Server,Timeout in=$DHCPServers do={
|
2024-11-13 08:57:05 +01:00
|
|
|
:local TimeoutExtra ($Timeout + [ /system/clock/get time ]);
|
2024-03-04 13:48:01 +01:00
|
|
|
:foreach Lease in=[ /ip/dhcp-server/lease/find where !dynamic status="waiting" \
|
2024-11-13 08:57:05 +01:00
|
|
|
server=$Server last-seen>$TimeoutExtra comment~"^hotspot-to-wpa:" ] do={
|
2024-03-04 13:48:01 +01:00
|
|
|
:local LeaseVal [ /ip/dhcp-server/lease/get $Lease ];
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint info $ScriptName ("Client with mac address " . ($LeaseVal->"mac-address") . \
|
2024-11-13 08:59:30 +01:00
|
|
|
" was not seen for " . ($LeaseVal->"last-seen") . ", removing.");
|
2024-03-04 13:48:01 +01:00
|
|
|
/interface/wifi/access-list/remove [ find where comment~"^hotspot-to-wpa:" \
|
|
|
|
mac-address=($LeaseVal->"mac-address") ];
|
|
|
|
/ip/dhcp-server/lease/remove $Lease;
|
|
|
|
}
|
2023-11-14 10:36:22 +01:00
|
|
|
}
|
2025-05-06 09:44:23 +02:00
|
|
|
} do={
|
|
|
|
:global ExitError; $ExitError $ExitOK [ :jobname ] $Err;
|
2024-12-06 10:31:52 +01:00
|
|
|
}
|