2021-06-22 15:58:03 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#!rsc by RouterOS
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# RouterOS script: hotspot-to-wpa-cleanup
							 | 
						
					
						
							
								
									
										
										
										
											2022-01-01 21:38:15 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								# Copyright (c) 2021-2022 Christian Hesse <mail@eworm.de>
							 | 
						
					
						
							
								
									
										
										
										
											2021-06-22 15:58:03 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#
							 | 
						
					
						
							
								
									
										
										
										
											2021-07-15 12:47:05 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								# provides: lease-script, order=80
							 | 
						
					
						
							
								
									
										
										
										
											2021-06-22 15:58:03 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# manage and clean up private WPA passphrase after hotspot login
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# https://git.eworm.de/cgit/routeros-scripts/about/doc/hotspot-to-wpa.md
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								:local 0 "hotspot-to-wpa-cleanup";
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								:global GlobalFunctionsReady;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								:global LogPrintExit2;
							 | 
						
					
						
							
								
									
										
										
										
											2021-06-28 00:13:16 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								:global ScriptLock;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								$ScriptLock $0 false 10;
							 | 
						
					
						
							
								
									
										
										
										
											2021-06-22 15:58:03 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2022-05-10 15:00:41 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								:foreach Client in=[ /caps-man/registration-table/find where comment~"^hotspot-to-wpa:" ] do={
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  :local ClientVal [ /caps-man/registration-table/get $Client ];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  :local Lease [ /ip/dhcp-server/lease/find where server~"wpa" dynamic \
							 | 
						
					
						
							
								
									
										
										
										
											2021-09-20 22:06:40 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    mac-address=($ClientVal->"mac-address") ];
							 | 
						
					
						
							
								
									
										
										
										
											2021-06-22 15:58:03 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  :if ([ :len $Lease ] > 0) do={
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    $LogPrintExit2 info $0 ("Client with mac address " . ($ClientVal->"mac-address") . \
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      " connected to WPA, making lease static.") false;
							 | 
						
					
						
							
								
									
										
										
										
											2022-05-10 15:00:41 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    /ip/dhcp-server/lease/make-static $Lease;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    /ip/dhcp-server/lease/set comment=($ClientVal->"comment") $Lease;
							 | 
						
					
						
							
								
									
										
										
										
											2021-06-22 15:58:03 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2022-05-10 15:00:41 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								:foreach Client in=[ /caps-man/access-list/find where comment~"^hotspot-to-wpa:" and \
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    !(comment~[ /system/clock/get date ]) ] do={
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  :local ClientVal [ /caps-man/access-list/get $Client ];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  :if ([ :len [ /ip/dhcp-server/lease/find where server~"wpa" !dynamic \
							 | 
						
					
						
							
								
									
										
										
										
											2021-09-20 22:06:40 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								       mac-address=($ClientVal->"mac-address") ] ] = 0) do={
							 | 
						
					
						
							
								
									
										
										
										
											2021-06-22 15:58:03 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    $LogPrintExit2 info $0 ("Client with mac address " . ($ClientVal->"mac-address") . \
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      " did not connect to WPA, removing from access list.") false;
							 | 
						
					
						
							
								
									
										
										
										
											2022-05-10 15:00:41 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    /caps-man/access-list/remove $Client;
							 | 
						
					
						
							
								
									
										
										
										
											2021-06-22 15:58:03 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2022-05-10 15:00:41 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								:foreach Lease in=[ /ip/dhcp-server/lease/find where !dynamic status=waiting \
							 | 
						
					
						
							
								
									
										
										
										
											2021-06-22 15:58:03 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    last-seen>4w comment~"^hotspot-to-wpa:" ] do={
							 | 
						
					
						
							
								
									
										
										
										
											2022-05-10 15:00:41 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  :local LeaseVal [ /ip/dhcp-server/lease/get $Lease ];
							 | 
						
					
						
							
								
									
										
										
										
											2021-06-22 15:58:03 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  $LogPrintExit2 info $0 ("Client with mac address " . ($LeaseVal->"mac-address") . \
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    " was not seen for long time, removing.") false;
							 | 
						
					
						
							
								
									
										
										
										
											2022-05-10 15:00:41 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  /caps-man/access-list/remove [ find where comment~"^hotspot-to-wpa:" \
							 | 
						
					
						
							
								
									
										
										
										
											2021-06-22 15:58:03 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								     mac-address=($LeaseVal->"mac-address") ];
							 | 
						
					
						
							
								
									
										
										
										
											2022-05-10 15:00:41 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  /ip/dhcp-server/lease/remove $Lease;
							 | 
						
					
						
							
								
									
										
										
										
											2021-06-22 15:58:03 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 |