สคริปต์เหล่านี้เป็นสคริปต์ที่ทำงานบนอุปกรณ์ Mikrotik ที่มี RouterOS เป็นหลัก. เป็นการทำงานลบผู้ใช้และเซสชัน (session) ที่หมดอายุแล้วในระบบ user manager ซึ่งทำงานเกี่ยวกับการจ่าย WiFi hotspot account.
สำหรับรุ่นที่รองรับ ถ้าหากระบุ 6.49 นั่นหมายถึงรุ่น 6.49.xx ย่อยทั้งหมด นอกจากนั้นในรุ่นอื่นๆอาทิเช่น 6.48 หรือ 7.0 ขึ้นไป อาจจะรองรับหรือไม่ก็ได้ ขอให้ผู้อ่านตรวจสอบอีกครั้งหนึ่ง.
ลบผู้ใช้ที่หมดอายุการใช้งานแล้ว
รองรับ: 6.49
# Automatic remove blank actual-profile user in User Manager for ROS v6.49
# https://forum.mikrotik.com/viewtopic.php?t=89184#p449692
:local username
:foreach i in [/tool user-manager user find where !actual-profile] do={
:set username [/tool user-manager user get $i username]
:log info "script: Remove user name $username"
[/tool user-manager user remove $i]
};
ลบ session ที่หมดอายุ
รองรับ: 6.49
# Convert date/time to timestamp. Original source code by ( https://forum.mikrotik.com/viewtopic.php?p=994844#p994849 ).
:global datetime2epoch do={
:local inputDateTime "$[/system clock get date] $[/system clock get time]"
if ([:len $1]>0) do={
:set inputDateTime $1
}
:local vdate [:pick $inputDateTime 0 [:find $inputDateTime " " -1]]
:local vtime [:pick $inputDateTime ([:find $inputDateTime " " -1] + 1) [:len $inputDateTime ]]
:local vgmt [/system clock get gmt-offset]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
:if ($vgmt < 0) do={:set vgmt ($vgmt * -1)}
:local arrm [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]
:local yyyy [:pick $vdate 7 11] ; :if ((($yyyy - 1972) % 4) = 0) do={:set ($arrm->1) -1; :set ($arrm->2) 30}
:local M ([:find "xxanebarprayunulugepctovecANEBARPRAYUNULUGEPCTOVEC" [:pick $vdate 1 3] -1] / 2); :if ($M>12) do={:set M ($M - 12)}
:local totd ((($yyyy - 1970) * 365) + (($yyyy - 1972) / 4) + ($arrm->$M) + [:pick $vdate 4 6])
:return (((((($totd * 24) + [:pick $vtime 0 2]) * 60) + [:pick $vtime 3 5]) * 60) + [:pick $vtime 6 8] - $vgmt)
}
# end function ---------------------------------
:local username
:local tilltime
:local tilltimeTs
# number of seconds for hotspot user's session timeout
:local sessionTimeout 86400
# current user's timeout timestamp.
:local curUserTimeoutTs
# current timestamp.
:local currentTs
:foreach i in [/tool user-manager session find] do={
:set username [/tool user-manager session get $i user]
:set tilltime [/tool user-manager session get $i till-time]
:set tilltimeTs [$datetime2epoch [/tool user-manager session get $i till-time]]
:set curUserTimeoutTs ($tilltimeTs + $sessionTimeout)
:set currentTs [$datetime2epoch]
if ($curUserTimeoutTs <= $currentTs) do={
:log info "script: user $username has timed out since $tilltime. deleting this session."
#[/tool user-manager session close-session numbers=$i]
[/tool user-manager session remove numbers=$i]
}
};
รองรับ 7.11
# Convert date/time to timestamp. Original source code by ( https://forum.mikrotik.com/viewtopic.php?p=994844#p994849 ).
:global datetime2epoch do={
:local dtime [:tostr $1]
/system clock
:local cyear [get date] ; :if ($cyear ~ "....-..-..") do={:set cyear [:pick $cyear 0 4]} else={:set cyear [:pick $cyear 7 11]}
:if (([:len $dtime] = 10) or ([:len $dtime] = 11)) do={:set dtime "$dtime 00:00:00"}
:if ([:len $dtime] = 15) do={:set dtime "$[:pick $dtime 0 6]/$cyear $[:pick $dtime 7 15]"}
:if ([:len $dtime] = 14) do={:set dtime "$cyear-$[:pick $dtime 0 5] $[:pick $dtime 6 14]"}
:if ([:len $dtime] = 8) do={:set dtime "$[get date] $dtime"}
:if ([:tostr $1] = "") do={:set dtime ("$[get date] $[get time]")}
:local vdate [:pick $dtime 0 [:find $dtime " " -1]]
:local vtime [:pick $dtime ([:find $dtime " " -1] + 1) [:len $dtime]]
:local vgmt [get gmt-offset]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
:if ($vgmt < 0) do={:set vgmt ($vgmt * -1)}
:local arrm [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]
:local vdoff [:toarray "0,4,5,7,8,10"]
:local MM [:pick $vdate ($vdoff->2) ($vdoff->3)]
:local M [:tonum $MM]
:if ($vdate ~ ".../../....") do={
:set vdoff [:toarray "7,11,1,3,4,6"]
:set M ([:find "xxanebarprayunulugepctovecANEBARPRAYUNULUGEPCTOVEC" [:pick $vdate ($vdoff->2) ($vdoff->3)] -1] / 2)
:if ($M>12) do={:set M ($M - 12)}
}
:local yyyy [:pick $vdate ($vdoff->0) ($vdoff->1)] ; :if ((($yyyy - 1968) % 4) = 0) do={:set ($arrm->1) -1; :set ($arrm->2) 30}
:local totd ((($yyyy - 1970) * 365) + (($yyyy - 1968) / 4) + ($arrm->$M) + ([:pick $vdate ($vdoff->4) ($vdoff->5)] - 1))
:return (((((($totd * 24) + [:pick $vtime 0 2]) * 60) + [:pick $vtime 3 5]) * 60) + [:pick $vtime 6 8] - $vgmt)
}
# end function ---------------------------------
:local username
:local tilltime
:local tilltimeTs
# number of seconds for hotspot user's session timeout
:local sessionTimeout 86400
# current user's timeout timestamp.
:local curUserTimeoutTs
# current timestamp.
:local currentTs
:foreach i in [/user-manager/session/find] do={
:set username [/user-manager/session/get $i user]
:set tilltime [/user-manager/session/get $i ended]
:set tilltimeTs [$datetime2epoch [/user-manager/session/get $i ended]]
:set curUserTimeoutTs ($tilltimeTs + $sessionTimeout)
:set currentTs [$datetime2epoch]
if ($curUserTimeoutTs <= $currentTs) do={
:log info "script: user $username has timed out since $tilltime. deleting this session."
[/user-manager/session/remove numbers=$i]
}
};