Community discussions

MikroTik App
 
samartsupply
just joined
Topic Author
Posts: 5
Joined: Sat Nov 19, 2016 5:07 pm

Script Expire users a after number of days

Sun Apr 23, 2017 5:53 am

Expire users a after number of days

Apply this to a hotspot user profile:
{
:local date [ /system clock get date ]
:if ( [ /ip hotspot user get $user comment ] = "" ) do={
[ /ip hotspot user set $user comment=$date ]
}
}


Run this once per day:
{
:local offset 7
:global today

{
:local date [ /system clock get date ]
:local montharray ( "jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec" )
:local monthdays ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 )
:local days [ :pick $date 4 6 ]
:local monthtxt [ :pick $date 0 3 ]
:local year [ :pick $date 7 11 ]
:local months ([ :find $montharray $monthtxt] )
:for nodays from=0 to=$months do={
:set days ( $days + [ :pick $monthdays $nodays ] )
}
:set days ($days + $year * 365)
:set today $days
}

:foreach i in [ /ip hotspot user find where disabled=no ] do={
:if ([ :find [ /ip hotspot user get $i comment ] ] = 0 ) do={
:local date [ /ip hotspot user get $i comment ]
:local montharray ( "jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec" )
:local monthdays ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 )
:local days [ :pick $date 4 6 ]
:local monthtxt [ :pick $date 0 3 ]
:local year [ :pick $date 7 11 ]
:local months ( [ :find $montharray $monthtxt ] )
:for nodays from=0 to=$months do={
:set days ( $days + [ :pick $monthdays $nodays ] )
}
:set days ($days + $year * 365)
:if ( ($days + $offset) < $today ) do={
:local name [/ip hotspot user get $i name]
:log info "HOTSPOT EXPIRE SCRIPT: Disabling Hotspot user $name first logged in $date"
[ /ip hotspot user disable $i ]
[ /ip hotspot active remove [find where user=$user] ]
}
}
}
}

I have Problem to Calculate ???
1. Add Day in Feburary(29day)
2. Calculate Add Day in next year etc. 30/December/2016
 
User avatar
Sinan
Trainer
Trainer
Posts: 129
Joined: Tue Sep 01, 2015 7:00 am

Re: Script Expire users a after number of days

Sun Apr 23, 2017 7:03 am

Yes there are many drawbacks in this script
i already developed a script for that which will work perfectly
i ll try to publish it soon
 
User avatar
zipvault
Member Candidate
Member Candidate
Posts: 140
Joined: Fri Dec 23, 2016 8:15 am

Re: Script Expire users a after number of days

Sun Apr 23, 2017 10:58 am

Session_timeout

And idle_timeout

should both work
 
doctorfix
just joined
Posts: 2
Joined: Wed Oct 18, 2017 7:27 pm

Re: Script Expire users a after number of days

Wed Oct 18, 2017 7:32 pm

#comment format on hotspot user: 201710180000 yyyymmddhhmm
#run script with scheduler
#script
:local thisDate
:local thisYear
:local thisDay
:local thisMonth
:local thisTime
:local thisTime1
:local thisTime2
:set thisDate [/ system clock get date]
:set thisTime [/ system clock get time]
:set thisYear [:pick $thisDate 7 11]
:if ($thisYear > "2010") do={
:set thisDay [:pick $thisDate 4 6]
:set thisMonth [:pick $thisDate 0 3]
:set thisTime1 [:pick $thisTime 0 2]
:set thisTime2 [:pick $thisTime 3 5]
:if ($thisMonth = "jan") do={ :set thisMonth "01" }
:if ($thisMonth = "feb") do={ :set thisMonth "02" }
:if ($thisMonth = "mar") do={ :set thisMonth "03" }
:if ($thisMonth = "apr") do={ :set thisMonth "04" }
:if ($thisMonth = "may") do={ :set thisMonth "05" }
:if ($thisMonth = "jun") do={ :set thisMonth "06" }
:if ($thisMonth = "jul") do={ :set thisMonth "07" }
:if ($thisMonth = "aug") do={ :set thisMonth "08" }
:if ($thisMonth = "sep") do={ :set thisMonth "09" }
:if ($thisMonth = "oct") do={ :set thisMonth "10" }
:if ($thisMonth = "nov") do={ :set thisMonth "11" }
:if ($thisMonth = "dec") do={ :set thisMonth "12" }
:set thisDate ($thisYear.$thisMonth.$thisDay.$thisTime1.$thisTime2)
:local users [/ip hotspot user find]
:local i
:local expirationDate
:foreach i in=$users do={
:set expirationDate [/ ip hotspot user get $i comment]
:if ([:len $expirationDate] = 12) do={
:local expNum [:tonum $expirationDate]
:local thisNum [:tonum $thisDate]
:if (([:typeof $expNum] = "num") and($expNum < $thisNum)) do={
:local userName [/ip hotspot user get $i name]
:local activeUser [/ip hotspot active find where user=$userName]
/ip hotspot user remove $i
/ip hotspot active remove $activeUser
:log info "Detected hotspot expired user"
}
}
:delay 0.5s
}
}
 
User avatar
BartoszP
Forum Guru
Forum Guru
Posts: 2880
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland

Re: Script Expire users a after number of days

Wed Oct 18, 2017 8:29 pm

To shorten you script look at this example
[admin@x86] >:global aaa {a=1;b=2}
[admin@x86] > :put ($aaa->"a")
1
[admin@x86] > :put ($aaa->"b")
2
 
atik49720
just joined
Posts: 2
Joined: Fri Apr 03, 2020 9:09 pm

Re: Script Expire users a after number of days

Fri Apr 03, 2020 9:21 pm

Which script i have to apply on user profiles on login script. I want to disable users after 30d from 1st login.
#comment format on hotspot user: 201710180000 yyyymmddhhmm
#run script with scheduler
#script
:local thisDate
:local thisYear
:local thisDay
:local thisMonth
:local thisTime
:local thisTime1
:local thisTime2
:set thisDate [/ system clock get date]
:set thisTime [/ system clock get time]
:set thisYear [:pick $thisDate 7 11]
:if ($thisYear > "2010") do={
:set thisDay [:pick $thisDate 4 6]
:set thisMonth [:pick $thisDate 0 3]
:set thisTime1 [:pick $thisTime 0 2]
:set thisTime2 [:pick $thisTime 3 5]
:if ($thisMonth = "jan") do={ :set thisMonth "01" }
:if ($thisMonth = "feb") do={ :set thisMonth "02" }
:if ($thisMonth = "mar") do={ :set thisMonth "03" }
:if ($thisMonth = "apr") do={ :set thisMonth "04" }
:if ($thisMonth = "may") do={ :set thisMonth "05" }
:if ($thisMonth = "jun") do={ :set thisMonth "06" }
:if ($thisMonth = "jul") do={ :set thisMonth "07" }
:if ($thisMonth = "aug") do={ :set thisMonth "08" }
:if ($thisMonth = "sep") do={ :set thisMonth "09" }
:if ($thisMonth = "oct") do={ :set thisMonth "10" }
:if ($thisMonth = "nov") do={ :set thisMonth "11" }
:if ($thisMonth = "dec") do={ :set thisMonth "12" }
:set thisDate ($thisYear.$thisMonth.$thisDay.$thisTime1.$thisTime2)
:local users [/ip hotspot user find]
:local i
:local expirationDate
:foreach i in=$users do={
:set expirationDate [/ ip hotspot user get $i comment]
:if ([:len $expirationDate] = 12) do={
:local expNum [:tonum $expirationDate]
:local thisNum [:tonum $thisDate]
:if (([:typeof $expNum] = "num") and($expNum < $thisNum)) do={
:local userName [/ip hotspot user get $i name]
:local activeUser [/ip hotspot active find where user=$userName]
/ip hotspot user remove $i
/ip hotspot active remove $activeUser
:log info "Detected hotspot expired user"
}
}
:delay 0.5s
}
}

Who is online

Users browsing this forum: No registered users and 67 guests