hotspot & queues user speed limit after x download scripts

in user list bytes in and out doesn’t count unless user log out and login back can anyone help me to make it count while user active or we can make script read active user statics bytes

am tryingg to limit user according to download this is the script i am trying to make it read active user bytes out and in but i couldn’t originally written by Deantwo

/ip hotspot user {
    :local limitedProfile "4m"
    :local limitedSlowProfile "LimitedSlow4"
    :local trafLimit 7516192768

    :local userList [find profile=$limitedProfile]
    :foreach user in=$userList do={
        :local traf ([get $user bytes-in] + [get $user bytes-out])
        :put ("User (" . [get $user name] . ") traf: " . $traf)
        :if ($traf > $trafLimit) do={
            set $user profile=$limitedSlowProfile
            :put ("User (" . [get $user name] . ") reached the traf limit")
            :log warning ("User (" . [get $user name] . ") reached the traf limit")
        }
    }
}

please anyone

I dont understand what do you want exactly, when user reach the total limit it will automatically remove from the active, do you want when the user reach the total limit it will change his profile to slower one ?

what i want is that user profile change to the slower one immidiatly when he reach the limit with out log out
and thank u for replying to me :slight_smile:

or we can kick only users who reached the limit is it possible ?

please anyone can help

I can make a script for you to change his profile to slower one after kicking him out
what u want can be done but it is not easy ..

the script above already identify user who reach limit and i use this script to kick them all

:foreach i in=[/ip hotspot active find]  do={
/ip hotspot active remove $i
}

but this is not what i want
what i want is :
user who reach limit only get kicked and change his profile to slower
user who didn’t reach limit remain connected and active
can u do that ?

it easy i ll do it soon …

thank u so much waiting you :slight_smile:

…up

First you limit the amount of the user in limit-bytes-total or out ..
Then in the Fast-speed profile at script on-logout put this script:
{
:if ($cause=“traffic limit reached”) do={
/ip hotspot user set profile=“profile2” [find name=“$user”]
/ip hotspot user reset-counters [find name=“$user”]
}}
change profile2 to the name of the slow-speed profile

i am testing now

what happen is that user didn’t logout after reaching limit he continued active

i think the best solution for all of this is to make users statistics work along with active statistics counters
the problem is user counters doesn’t work unless user log out and back again

i have been trying to do script that identfy active user download byte and change his prfile if he reach limit

but didn’t work

/ip hotspot user {

    :local limitedProfile "2m"  
    :local limitedSlowProfile "LimitedSlow2"
    :local trafLimit 314572800                   
                          
     :local userList [find profile=$limitedProfile]
     :foreach counter in=[/ip hotspot active find ] do={
        :local traf ([/ip hotspot active get $counter bytes-in] + [/ip hotspot active get $counter bytes-out])
           :foreach user in=$userList do={                                                   
         :put ("User (" . [get $user name] . ") traf: " . $traf)
        :if ($traf > $trafLimit) do={                          
          set $user profile=$limitedSlowProfile
            :put ("User (" . [get $user name] . ") reached the traf limit")
            :log warning ("User (" . [get $user name] . ") reached the traf limit")
           }                                                                    
        }
   }     
}   

I don’t have a way to test “hotspot active user” values, hence why the script I wrote uses the “hotspot user” values.

From what I can see “hotspot active user” has a “user” value with the username of the user. So making the script check that user and/or log out that user shouldn’t be that hard.

Things I don’t know is rather the “hotspot active user” byte values are for that one session or a total for that user.
For example if a user login, download 5 bytes, then logout and login again and then download another 7 bytes. what will each counter say?
My guess is that the “hotspot user” user will say 5 bytes and the “hotspot active user” will say 7 bytes. But I would like for you to possibly test this out for me.

Let me try to update this a little bit at least.

# Checks if limited users has reached the limit, if they have then set them to be limited slow state.
# Run this script every few minutes.
/ip hotspot user {
# Variables:
    :local limitedProfile "Limited"
    :local limitedSlowProfile "LimitedSlow"
    :local trafLimit 10000
# Code:
    :local userList [find profile=$limitedProfile]
    :foreach user in=$userList do={
        :local traf ([get $user bytes-in] + [get $user bytes-out])
        :local activeUser [/ip hotspot active find user=[/ip hotspot user get $user name]]
        :if ([:len $activeUser] != 0) do={
            :set traf ($traf + [/ip hotspot active get $activeUser bytes-in] + [/ip hotspot active get $activeUser bytes-out])
        }
        :put ("User (" . [get $user name] . ") traf: " . $traf)
        :if ($traf > $trafLimit) do={
            set $user profile=$limitedSlowProfile
            :put ("User (" . [get $user name] . ") reached the traf limit")
            :if ([:len $activeUser] != 0) do={
                /ip hotspot active remove $activeUser
                :put ("User (" . [get $user name] . ") has been forcefully logged out")
            }
            :log warning ("User (" . [get $user name] . ") reached the traf limit")
        }
    }
}



# Reset counts are the limited users.
# Run this script once every day.
/ip hotspot user {
# Variables:
    :local limitedProfile "Limited"
    :local limitedSlowProfile "LimitedSlow"
# Code:
    :foreach user in=[find (profile=$limitedProfile || profile=$limitedSlowProfile)] do={
        /ip hotspot active remove [/ip hotspot active find user=[/ip hotspot user get $user name]]
    }
    reset-counters [find (profile=$limitedProfile || profile=$limitedSlowProfile)]
    set [find profile=$limitedSlowProfile] profile=$limitedProfile
    :log warning "All user limits has been reset"
}

This may work better, it is untested on a live system though.

first thank u for replaying :slight_smile:
the user log in then the active counter start counting what is user is downloading -right now user value is 0 total bytes- let say he download 1 gig , when he log out this 1 gig will go to the user value statistics . if he login again active counter will start over again from 0
see attachment will demonstrate u will see user majed11 is active he download 600 mb but his user counters are 0 if force him log out what he download in active counters will move to user counters
+
i tried the script u wrote sadly it didn’t work (invalid internal item number)

Found a nice little way to test this, and it seems that I was correct. So my script should be handling that correctly.

Ok, I don’t know why the script fails on your end, I don’t have that issue in my tests.
Not doing anything in the script that should make it possible for invalid find results.

Only thing I can think of is if multiple active users have the same name, but that makes little sense.
Could also be a user logout at the exact moment the script was running, but that is just unlikely.

i tested it many times the same error hmmm :confused: all active user have different names i have two routers i tested it in both , can any one try this maybe he will get different result

Can you try running this and giving me the result if it fails?

# Variables:
:global limitedProfile "Limited"
# Code:
/ip hotspot user {
    :local userList [find profile=$limitedProfile]
    :put $userList
}
:put [/ip hotspot active find user=[/ip hotspot user get [:pick [find profile=$limitedProfile] 0] name]]
/ip hotspot user {
    :local userList [find profile=$limitedProfile]
    :foreach user in=$userList do={
        :local activeUser [/ip hotspot active find user=[/ip hotspot user get $user name]]
        :if ([:len $activeUser] != 0) do={
            :put $activeUser
        }
    }
}
:global limitedProfile