Community discussions

MikroTik App
 
kvocha
just joined
Topic Author
Posts: 13
Joined: Sat Apr 01, 2023 9:13 pm

Deleting expired hotspot user V6 V7

Wed Aug 23, 2023 7:34 am

Hi all,

Can anybody help me to find the script(scripts) to delete expired users.
I use script from viewtopic.php?p=994849#p994849 to make timestamp so I can put it in comment when creating user.
Now I'm searching for the script to add limit-time to timestamp, compare them and delete expired user.
 
kvocha
just joined
Topic Author
Posts: 13
Joined: Sat Apr 01, 2023 9:13 pm

Re: Deleting expired hotspot user V6 V7

Wed Aug 23, 2023 9:32 am

I composed this script using example from viewtopic.php?t=187680:

:foreach i in=[ip hotspot user find where name!="admin" and name!="default-trial" and disabled=no] do={

    :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)
    }
    
    
    :local epoch [$datetime2epoch];

    
    :local User [ip hotspot user get $i name];
    :local Limit [ip hotspot user get $i limit-uptime]
    :local Comment [ip hotspot user get $i comment]

    :if ($Comment!="") do={
        :local Comment1 [:tonum $Comment];
	:local Limit1 [:tonum $Limit]
	: local ts1 [$Comment1+$Limit1]
        :log info "DEBUG User: $User, Expires: $ts1, Epoch: $epoch"

        :if ($epoch >= $ts1) do={
            :log info "EXPIRED user: $user, Expires: $ts1, Epoch: $epoch"
            user remove $i
            cookie remove [find where user=$User]
            active remove [find where user=$User]
        }
    } else={
        :log info "Checked user: $User, Expires: $ts1, Epoch: $epoch"
    } 
but I have problem with this:
	:local Limit [user get $i limit-uptime]
	:local Comment [user get $i comment]
I need to put limit-uptime and comment to variable in seconds
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Deleting expired hotspot user V6 V7

Wed Aug 23, 2023 1:49 pm

you should look better in my resources....
viewtopic.php?t=177551#p977175


Just some hint:
:foreach i in=[ip hotspot user find where name!="admin" and name!="default-trial" and disabled=no] do={

    :global datetime2epoch do={
    […]
    }

    […]
}
do not define everytime for each cycle the function....

the corect way is this:
:global datetime2epoch do={
    […]
}

:foreach i in=[ip hotspot user find where name!="admin" and name!="default-trial" and disabled=no] do={
    […]
}

"EXPIRED user: $user" is wrong, must be $User with uppercase.

"Checked user: $User, Expires: $ts1, Epoch: $epoch" on the else $ts1 do not exist....

"limit-uptime" is time format like 7d, 1d etc and can't be used as number.... (for example from 1w00:00:00 is 604800 seconds)

revised for v6 and v7 but untested code

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

:global timetoseconds do={
    :local inTime $1
    :local wPos   [:find $inTime "w" -1]
    :local dPos   [:find $inTime "d" -1]
    :local itLen  [:find $inTime "." -1] ; :if ([:typeof $itLen] = "nil") do={:set itLen [:len $inTime]}
    :local itSec  [:pick $inTime ($itLen - 2) $itLen]
    :local itMin  [:pick $inTime ($itLen - 5) ($itLen - 3)]
    :local itHou  [:pick $inTime ($itLen - 8) ($itLen - 6)]
    :local itDay  0
    :local itWee  0
    :if (([:typeof $wPos] = "nil") and ([:typeof $dPos] = "num")) do={:set itDay [:pick $inTime 0 $dPos] }
    :if (([:typeof $wPos] = "num") and ([:typeof $dPos] = "num")) do={:set itDay [:pick $inTime ($wPos + 1) $dPos] }
    :if  ([:typeof $wPos] = "num")                                do={:set itWee [:pick $inTime 0 $wPos] }
    :local totitSec ($itSec + (60 * $itMin) + (3600 * $itHou) + (86400 * $itDay) + (604800 * $itWee))
    :return $totitSec
}

/ip hotspot user
{
    :local epoch [$datetime2epoch]
    :local User "" ; :local Limit 0 ; :local Comment ; :local ts1 0
    :foreach i in=[find where name!="admin" and name!="default-trial" and disabled=no] do={
        :set User    [get $i name]
        :set Limit   [$timetoseconds [get $i limit-uptime]]
        :set Comment [:tonum [get $i comment]]
        :if ([:typeof $Comment] = "num") do={
            :set ts1 ($Comment + $Limit)
            :log info "DEBUG User: $User, Expires: $ts1, Epoch: $epoch"
            :if ($epoch >= $ts1) do={
                :log info "EXPIRED User: $User, Expires: $ts1, Epoch: $epoch"
                remove $i
                .. cookie remove [find where user=$User]
                .. active remove [find where user=$User]
            }
        } else={
            :log info "Checked user: $User, Expires: $ts1, Epoch: $epoch"
        }
    }
}
Last edited by rextended on Wed Aug 23, 2023 2:17 pm, edited 4 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Deleting expired hotspot user V6 V7

Wed Aug 23, 2023 2:09 pm

   
 
kvocha
just joined
Topic Author
Posts: 13
Joined: Sat Apr 01, 2023 9:13 pm

Re: Deleting expired hotspot user V6 V7

Wed Aug 23, 2023 9:25 pm

It's great help bro!

I wrote this:

:global ltimeepoch do={ 
  :local ltime [:tostr $1]
  /ip hotspot user get user2 limit-uptime
    
:if ([:len $ltime] = 11) do={:set ltime "$[:pick $ltime 0] * 604800 + $[:pick $ltime 3] * 86400 + $[:pick $ltime 4 5] * 3600}"
:if ([:len $ltime] = 9) do={:set ltime "$[:pick $ltime 0] * 86400 + $[:pick $ltime 2 3] * 3600"}
:if ([:len $ltime] = 7) do={:set ltime "$[:pick $ltime 0 1] * 3600"}
:if ([:len $ltime] = 6) do={:set ltime "$[:pick $ltime 0] * 3600"}

and thought how to run it :D

Who is online

Users browsing this forum: rextended and 6 guests