Remove false active sessions via script

Hallo,

I’m using UM v5.4. When I have got a linkdown there is the problem that the UM don’t recognize that session is stopped and the session of the users are still active in UM.
So when the link is back and the users want to connect UM says that they can’t connect because it is only allowed do use the logging one time simultaneously.

Is there a sollution for that problem?

I try to script something but I have a problem. To close a session I need the number of the session. How the get the number of the session?!? See code ..

:local LastSessionUpdate;
:local SessionTimeout;

:foreach i in=[/tool user-manager session find where active=yes] do={
#when was the last Update of the session-informations
	:set LastSessionUpdate [/tool user-manager session get $i till-time]
#SessionTimeout is a value that tells me how many seconds ago the last update of this session was
	:set SessionTimeout ([system clock get time] - [:pick $LastSessionUpdate ([:find $LastSessionUpdate " "]+1) [:len $LastSessionUpdate]]-[/system clock get gmt-offset])
#Updates are every minute, if last update is more then 60 seconds ago then close session
	:if ($SessionTimeout > 60) do={

#how to get the number from the session?? 	
		/tool user-manager session close-session numbers= ???

	}	
}}

Thanks in advance.

Hi,

isn’t $i what you looking for?

Ups, yes … I never tested it in script. Because with put on the terminal I try to close the session with these values from the put. something like *e12 was the output and this didn’t work. But in the script it does!! Sometimes it could be so easy… Thanks… :slight_smile:

Here the script if somebody need it …

#This script remove false active sessions in User Manager v5
#when the last update of the session (till-time) is to long ago then the session will be closed 

#Setting Timeout in Seconds (my UserManager update session every minute), 
#Timeout in Seconds, when session update is older -> session clossed
:local Timeout 60

#------------------------------------------
:local LastSessionUpdate;
:local SessionTimeout;
:foreach i in=[/tool user-manager session find where active=yes] do={
#when was the last Update of the session-informations
	:set LastSessionUpdate [/tool user-manager session get $i till-time]
#SessionTimeout is a value that tells me how many seconds ago the last update of this session was
	:set SessionTimeout ([system clock get time] - [:pick $LastSessionUpdate ([:find $LastSessionUpdate " "]+1) [:len $LastSessionUpdate]]-[/system clock get gmt-offset])
#if last update is more then Timeout seconds ago then close session and log it
	:if ($SessionTimeout > $Timeout) do={ 	
		/tool user-manager session close-session numbers=$i
		:log info (" removed false active session" . [/tool user-manager session get $i user]);
	}	
}

I have had the same problems a lot of times, sent requests to MT support etc.
Have not seen this script until now, and it seems to work.

But this problem has been there for several years, think I had this problem in 2009!
Since it has caused me a lot of headaches :frowning:
Is there not any way to fix this MT?
Pretty pretty please!

No comments from MT?

Any update on this problem from MT?

I still have sessions that hangs, preventing clients connecting to usermanager…

I wrote this one, hope it helps :slight_smile:

# ver. 1.0   2013-09-06
# ver. 1.1   2013-11-01 - end of the month all sessions get killed problem solved
#                       - logging killed sessions in log as warning
# ver. 1.2   2014-03-09 - GMToffset not needed in 6.10 and higher (possibly 6.x)
#
# SCRIPT:  radius-close-inactive-sessions
# PURPOSE: close sessions which are active in radius, but last-seen is older than $actTimeout
#          this typically happens when client site loses connection to radius and user leaves hotspot
#------------------------------------------------------------------------------

# settings
# --------
# enable debugging { false | true }
local debug true
# after how many seconds of inactivity kill the session
local actTimeout (10*60)
# offset from GMT - this is not needed in User Manager 6.10 and higher - should be 0
local GMToffset [/system clock get gmt-offset]
if ( [/system clock get dst-active] != yes ) do={ :set GMToffset ( $GMToffset - 3600 ) }
local GMToffset 0


# variables
# ---------
local sessions
local testD [/system clock get date]
local testDayOfMonth [:pick $testD 4 6]
local testT [/system clock get time]
local testTsec ( ($testDayOfMonth*24*3600) + [:pick $testT 0 2]*3600 + [:pick $testT 3 5]*60 + [:pick $testT 6 8] )

:set sessions [/tool user-manager session find where active=yes]

if ($debug = true) do={
  :put ("testD: " . $testD)
  :put ("testDayOfMonth: " . $testDayOfMonth )
  :put ("testT: " . $testT)
  :put ("testTsec: " . $testTsec)
  :put ("Sessions: " . [:len $sessions])
}

foreach session in $sessions do={
  :local userName [/tool user-manager session get $session user ]
  :local uptime [/tool user-manager session get $session uptime ]
  :local hostIP [/tool user-manager session get $session "host-ip" ]
  :local lastSeen [/tool user-manager user get $userName last-seen ]

  local lastActD [:pick $lastSeen 0 11]
  local lastActDayOfMonth [:pick $lastSeen 4 6]
  local lastActT [:totime [:pick $lastSeen 12 20]]

  local lastActTsec ( ($lastActDayOfMonth*24*3600) + [:pick $lastActT 0 2]*3600 + [:pick $lastActT 3 5]*60 + [:pick $lastActT 6 8] )

  if ($debug = true) do={
    :put ("id=".$session . " " . " user " . $userName . " uptime  " . $uptime . " lastSeen " . $lastSeen . " hostIP ". $hostIP)
    :put ("lastActD: " . $lastActD)
    :put ("lastActDayOfMonth: " . $lastActDayOfMonth)
    :put ("lastActT: " . $lastActT)
    :put ("lastActTsec: " . $lastActTsec)
  }
  :set lastActTsec ($lastActTsec + $GMToffset)
  if ($debug = true) do={
    :put ("lastActTsec after GMT correction: " . $lastActTsec)
  }

#make sure we are at least end of month + $actTimeout
  if ( $testTsec > $actTimeout) do={
#difference is bigger than $actTimeout, but not bigger than 27 days (new month)
#for period localtime-GMT script will be ineffective once a month
    if ( (($testTsec - $lastActTsec) > $actTimeout) and (($testTsec - $lastActTsec) < 27*3600*24) ) do={
      if ($debug = true) do={
        :put ("radius inactive session killed, difference " . ($testTsec - $lastActTsec) . "s actTimeout=" . $actTimeout . "s")
      }
      /tool user-manager session close-session $session
      :log warning ("radius inactive session killed, difference " . ($testTsec - $lastActTsec) . " user " . $userName . " uptime  " . $uptime . " lastSeen " . $lastSeen . " hostIP ". $hostIP)
    } else={
      if ($debug = true) do={
        :put ("No need kill session, difference " . ($testTsec - $lastActTsec) . "s actTimeout=" . $actTimeout . "s")
      }
    }
  }
}

for me this script does not work, I would have friends could help us again as I have problems when the User becomes trapped in the session timeout and scored the hotspot or pppoe.
Does anyone have any more suggestions for sulucionar this problem?

I’ve updated the code for RouterOS 6.x

can you put the updated script here please. I’m using RouterOS 6.49.6

Do not ask on multiple topic,
the solution:
http://forum.mikrotik.com/t/false-active-sessions/163578/4