Expire users a after number of days Stops Work after Upgrade to v.6.33rc11

This Part works fine

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

The Problematic Part comes here

{
: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] ]
}
}
}
}

The new terminal error message

invalid value of "to", must be integer

NORMIS, PLEASE HELP ME TO DO THE SCRIPT COME BACK TO WORK FINE !!!!!!!!!!!!!!!!!!!!!!!!!!!

Everywhere you have:

:for nodays from=0 to=$months do={

Try using:

:for nodays from=0 to=[:tonum $months] do={

Thank you! Your code works! You are great!

This her is my own Version

Important: always set the limit-uptime under ip-hotspot user # :smiley: :laughing: :slight_smile:

{
:local date [ /system clock get date ]
:if ( [ /ip hotspot user get $user comment ] = "" ) do={
[ /ip hotspot user set $user comment="$date"]
}
}
:foreach h in=[/ip hotspot active find] do={
:global user [/ip hotspot active get $h user];
:global mac [/ip hotspot active get $h mac-address];
/ip hotspot user set $user mac=$mac
}
{
:foreach h in=[/ip hotspot active find] do={
:local user [/ip hotspot active get $h user];
:local mac [/ip hotspot active get $h mac-address];
:local logdate [/ip hotspot user get $user comment];
:local date [/system clock get date];
:local time [/system clock get time];
:local upt [/ip hotspot user get $user limit-uptime];
/system scheduler remove [/system scheduler find name="$user"];
/system scheduler add comment="$date . $time" interval=$upt name="$user" on-event="/ip
_hotspot active remove [/ip hotspot active find user="$user"]\r
\n/ip hotspot user disable [/ip hotspot user find name="$user"]\r
\n/ip hotspot cookie remove [/ip hotspot cookie find name="$user"]\r
\n/system scheduler remove [/system scheduler find name="$user"]"
policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive
start-date=$logdate start-time=12:00:00
}
}

There is a new error! This is very Curios!

{
:local offset 2
: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=[:tonum $months] do={
:set days ( $days + [ :pick $monthdays $nodays ] )
}
:set days ($days + $year * 365)
:set today $days
}

:foreach i in [ /ip hotspot user find where profile=PRIVATE ] 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=[:tonum $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 ]
}
}
}
}

By profile=PRIVATE NewTerminal error ,,invalid value of "to", must be integer"



{
:local offset 2
: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=[:tonum $months] do={
:set days ( $days + [ :pick $monthdays $nodays ] )
}
:set days ($days + $year * 365)
:set today $days
}

:foreach i in [ /ip hotspot user find where profile=10RS ] 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=[:tonum $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 ]
}
}
}
}

By profile=10RS without error, NewTerminal O.K, why ????????????????

You should try inserting some “:log info ()” commands to debug your code. For example

...

:foreach i in [ /ip hotspot user find where profile=PRIVATE ] 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 ]
:log info ($days)
:local monthtxt [ :pick $date 0 3 ]
:log info ($monthtxt)
:local year [ :pick $date 7 11 ]
:log info ($year)
:local months ( [ :find $montharray $monthtxt ] )
:log info ($months)
:for nodays from=0 to=[:tonum $months] do={
:set days ( $days + [ :pick $monthdays $nodays ] )
}

...

the result is
script info th
script info HP
script info r

what I not understand is
when I change the profile from the user the script runs very well, when I change the profile in users other time back to my custumated the error comes back

{
:local offset 1
: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=[:tonum $months] do={
:set days ( $days + [ :pick $monthdays $nodays ] )
}
:set days ($days + $year * 365)
:set today $days
}

:foreach i in [ /ip hotspot user find where profile=1000 ] 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 ]
:log info ($days)
:local monthtxt [ :pick $date 0 3 ]
:log info ($monthtxt)
:local year [ :pick $date 7 11 ]
:log info ($year)
:local months ( [ :find $montharray $monthtxt ] )
:log info ($months)
:for nodays from=0 to=[:tonum $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]
:local mac [/ip hotspot user get $i mac-address]
:local mail [/ip hotspot user get $i email]
:local time [/system clock get time]
:local cdate [/system clock get date]
:log info "HOTSPOT EXPIRE SCRIPT: Sending email to Hotspot user $name first logged in $date"
[ /tool e-mail send to="$mail" from="5g.net" subject=("Aviso 5G Net" . " -- " . "Usoario=$name" . " -- " . "Seu Mac=$mac") body=
"AVISO SUA RECARGA VAI VENCER !!!
Sua Recarga vai vencer nos proximos 24 Horas, use todos os Gigabytes ou Megabytes restantes imediatamente !!!
Sua Recarga atual foi ativado no Dia-$date !!!
Nome do Usoario=$name, O Mac do seu Dispositivo=$mac
Esta Mensagem foi enviado em-$cdate, Horas-$time
Muito Obrigado dos seus Amigos da 5G Net de Novo Progresso, PA, CEP 68193-000, Brasil" ]
}
}
}
}

invalid value of "to", must be integer

script info th

script info HP
script info r
script info #

\

now I change the profile=270RS in hotspot user



{
:local offset 1
: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=[:tonum $months] do={
:set days ( $days + [ :pick $monthdays $nodays ] )
}
:set days ($days + $year * 365)
:set today $days
}

:foreach i in [ /ip hotspot user find where profile=270RS ] 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 ]
:log info ($days)
:local monthtxt [ :pick $date 0 3 ]
:log info ($monthtxt)
:local year [ :pick $date 7 11 ]
:log info ($year)
:local months ( [ :find $montharray $monthtxt ] )
:log info ($months)
:for nodays from=0 to=[:tonum $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]
:local mac [/ip hotspot user get $i mac-address]
:local mail [/ip hotspot user get $i email]
:local time [/system clock get time]
:local cdate [/system clock get date]
:log info "HOTSPOT EXPIRE SCRIPT: Sending email to Hotspot user $name first logged in $date"
[ /tool e-mail send to="$mail" from="5g.net" subject=("Aviso 5G Net" . " -- " . "Usoario=$name" . " -- " . "Seu Mac=$mac") body=
"AVISO SUA RECARGA VAI VENCER !!!
Sua Recarga vai vencer nos proximos 24 Horas, use todos os Gigabytes ou Megabytes restantes imediatamente !!!
Sua Recarga atual foi ativado no Dia-$date !!!
Nome do Usoario=$name, O Mac do seu Dispositivo=$mac
Esta Mensagem foi enviado em-$cdate, Horas-$time
Muito Obrigado dos seus Amigos da 5G Net de Novo Progresso, PA, CEP 68193-000, Brasil" ]
}
}
}
}

email send O.K, New Terminal no error,

script info 10
script info oct
script info 2015
script info 9

My email client thunderbird receifed

AVISO SUA RECARGA VAI VENCER !!!
Sua Recarga vai vencer nos proximos 24 Horas, use todos os Gigabytes ou Megabytes restantes imediatamente !!!
Sua Recarga atual foi ativado no Dia-oct/10/2015 !!!
Nome do Usoario=Sa, O Mac do seu Dispositivo=94:51:03:D3:B6:B8
Esta Mensagem foi enviado em-oct/19/2015, Horas-12:55:04
Muito Obrigado dos seus Amigos da 5G Net de Novo Progresso, PA, CEP 68193-000, Brasil #

Right, so $days is wrong and that comes from the variable $date which comes from a hotspot user comment:

:local date [ /ip hotspot user get $i comment ]

So clearly, one of your hotspot users doesn’t have a comment in the expected format.

oct/10/2015

I don’t understand why this script works fine for all user profiles, error comes only by one especific profile, I doesn’t change any thing from the rule, error occur when I change the profile in user profile???

Try logging the date variable each time through the loop.

I don’t understand what you mean??? :confused:

Right after your line:

:local date [ /ip hotspot user get $i comment ]

Add a log:

:local date [ /ip hotspot user get $i comment ]
:log info "DATE IS:$date"

You’ll see something you don’t expect to be in the comment.

:foreach i in [ /ip hotspot user find where profile=1000 ] do={
:if ([ :find [ /ip hotspot user get $i comment ] ] = 0 ) do={
:local date [ /ip hotspot user get $i comment ]
:confused:

Dear PeterDoBrasil

Did you reach solution for your problem? I´m trying to do a script similar to yours.

High Voltage has damaged my Router, I have no other device to work with Scripting now !!! :confused:

Dear @PeterDoBrasil

Your script is being very useful to me. Thanks for share it on mikrotik WIKI.

I´m developing a group of scripts that should create a static user with the data of dinamic “trial hotspot user” and every LOGOUT, update the “uptime variable” of him (I´m using limit-uptime field).

Every days I run your script (with little adaptations) to know if the user spend more then 7hs in the last 7 days.

MY PROBLEM:

In “http://wiki.mikrotik.com/wiki/Manual:IP/Hotspot/User” says: “Starting with v6.34rc11 some additional variables are available:” we can find the new variable: “$uptime-secs” but using v6.35.2 or v6.34.1 the variable is not there.

Maybe your or the master @JJCinAZ can try help me.

Sorry for my bad english. My main language is portuguese. If possible call me on skype: osorio@diodo.com.br

Thank all.

This script has many errors !!

Friends, I found this script and I hope it will help someone:

:local mdays  {31;28;31;30;31;30;31;31;30;31;30;31}
:local months {"jan"=1;"feb"=2;"mar"=3;"apr"=4;"may"=5;"jun"=6;"jul"=7;"aug"=8;"sep"=9;"oct"=10;"nov"=11;"dec"=12}
:local monthr  {"jan";"feb";"mar";"apr";"may";"jun";"jul";"aug";"sep";"oct";"nov";"dec"}

:local dd  [:tonum [:pick $date 4 6]]
:local yy [:tonum [:pick $date 7 11]]
:local month [:pick $date 0 3]

:local mm (:$months->$month)
:set dd ($dd+$days)

:local dm [:pick $mdays ($mm-1)]
:if ($mm=2 && (($yy&3=0 && ($yy/100*100 != $yy)) || $yy/400*400=$yy) ) do={ :set dm 29 }

:while ($dd>$dm) do={
    :set dd ($dd-$dm)
    :set mm ($mm+1)
    :if ($mm>12) do={
        :set mm 1
        :set yy ($yy+1)
    }
    :set dm [:pick $mdays ($mm-1)]
    :if ($mm=2 &&  (($yy&3=0 && ($yy/100*100 != $yy)) || $yy/400*400=$yy) ) do={ :set dm 29 }
}
:local res "$[:pick $monthr ($mm-1)]/"
if ($dd<10) do={ :set res ($res."0") }
:set $res "$res$dd/$yy"
:return $res

:local date ([system clock get date]." ".[system clock get time])
:if ([ip hotspot user get $user value-name=comment]="") do={
    ip hotspot user set $user comment=$date
}
:local validDays 30

:local getNextTime [:parse [/system script get nextDateByCaibneTik source]]
foreach usr in=[ip hotspot user find] do={
    if (([len [ip hotspot user get $usr comment]]=20) && ([:find [ip hotspot user get $usr comment] " " -1]=11)) do={
        :local getComment [ip hotspot user get $usr comment]; :local date [:pick $getComment 0 11]; :local time [:pick $getComment 12 20]
        if ([$getNextTime date=$date days=$validDays]=[system clock get date]) do={
            :local nowTime [system clock get time]
            :local nowMS (([pick $nowTime 0 2]*60*60*1000)+([pick $nowTime 3 5]*60*1000)+([pick $nowTime 6 8]*1000))
            :local ms (([pick $time 0 2]*60*60*1000)+([pick $time 3 5]*60*1000)+([pick $time 6 8]*1000))
            if ([$nowMS]>=$ms) do={
                :local username [ip hotspot user get $usr name]
                ip hotspot active remove [find user=$username]
                ip hotspot user remove $usr
            } 
        }
    }
}

EL DONCITO.

A code thrown there haphazardly, without instructions, and incomplete, does not help anyone.
It’s just garbage,
For example, where is the “nextDateByCaibneTik” script?