Script to remove unflaged sessions

Hey all
I want to remove nonactive session in /user-manager session and I got this code so far but it’s not working and I don’t know how to get it right.

:local Flag
:set Flag [/user-manager session find where flags=""]
:user-manager session print where flags=""
:if([:len $Flag] > 0) do={/user-manager session remove $Flag}

From who, from the junk shop?


Posting random and useless code diverts attention from the underlying problem.
Ask what you want to do, don’t ask to change a code that doesn’t work (?) to do what you want to do…

Sorry that was a typo (I asked and ChatGPT generated that)

I'm looking for a script that can remove inactive sessions from the '/user-manager session' log. With each user login and logout, a log is created, and after a few days, it can grow to hundreds of thousands of entries. I want the script to run periodically, say every 24 hours, to remove the inactive entries. However, I don't want to remove all entries at once, as that would cause users to lose their connection and have to reconnect again hence the script should only remove entries without A (active) flag. How can I achieve this?

I do not have a user-manager v7 to test now, at home, but probably are like the v6, only one instruction:
/user-manager session remove [find where active=no]

If do not work, ask it and I test tomorrow at work with the test device.

That worked man thanks a bunch.

MikroTik script to clean up inactive User Manager sessions while preserving active connections.

_# Script to cleanup inactive User-Manager sessions

Add this script to your scheduler to run every 24 hours\


:local cleanupLog do={

Get current time for logging

:local currentTime [/system clock get time]
:local currentDate [/system clock get date]
\

Log start of cleanup process

:log info "Starting User-Manager session cleanup at $currentTime on $currentDate"
\

Counter for removed sessions

:local removedCount 0
\

Get all sessions without active flag

:foreach session in=[/user-manager session find where active=no] do={

Remove the inactive session

/user-manager session remove $session
:set removedCount ($removedCount + 1)
}
\

Log completion with count of removed sessions

:log info "User-Manager session cleanup completed. Removed $removedCount inactive sessions"
}
\

Execute the cleanup function

$cleanupLog
\

Add this to scheduler using:

/system scheduler

add interval=24h name=userman-cleanup on-event="/system script run userman-cleanup" \

start-date=jan/01/2024 start-time=02:00:00_

This script does the following:

  1. Creates a function cleanupLog that:
    o Gets current time and date for logging purposes
    o Logs the start of the cleanup process
    o Counts removed sessions
    o Finds and removes all inactive sessions
    o Logs completion with the number of sessions removed

  2. Uses the RouterOS v7 user-manager commands to safely remove only inactive sessions

  3. Includes detailed logging for monitoring and troubleshooting

  4. Preserves all active sessions (those with the A flag)

To implement this:

  1. Create a new system script:
    /system script add name=userman-cleanup source=

  2. Add it to the scheduler to run every 24 hours (suggested at 2 AM):
    /system scheduler add interval=24h name=userman-cleanup on-event="/system script run userman-cleanup" start-date=jan/01/2024 start-time=02:00:00

  3. You can also run it manually to test:
    /system script run userman-cleanup

Enjoy.

Necroposting for nothing.
The least efficient way to do things, a whole page of useless writing to do the instruction I wrote in the previous post.

Hi everyone,

Does removing inactive sessions impact user manager accounting?

:foreach i in=[/user-manager session find where active=no] do { /user-manager session remove $i; }

This remove only inactive sessions

While the one previously posted by rextended, which is much simpler :

/user-manager session remove [find where active=no]

did something different? :question: