Community discussions

MikroTik App
 
tomtom80
newbie
Topic Author
Posts: 43
Joined: Sun Jan 06, 2008 12:48 am

Remove false active sessions via script

Mon Jun 20, 2011 4:30 pm

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.
 
User avatar
petrn
Member Candidate
Member Candidate
Posts: 180
Joined: Thu Jul 29, 2010 3:56 am
Location: Dubai

Re: Remove false active sessions via script

Tue Jun 21, 2011 1:13 am

Hi,

isn't $i what you looking for?
 
tomtom80
newbie
Topic Author
Posts: 43
Joined: Sun Jan 06, 2008 12:48 am

Re: Remove false active sessions via script

Tue Jun 21, 2011 9:14 am

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... :-)

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]);
	}	
}
 
Superdust
Member Candidate
Member Candidate
Posts: 182
Joined: Mon Jun 11, 2007 3:24 pm

Re: Remove false active sessions via script

Wed Jul 20, 2011 12:16 pm

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 :(
Is there not any way to fix this MT?
Pretty pretty please!
 
Superdust
Member Candidate
Member Candidate
Posts: 182
Joined: Mon Jun 11, 2007 3:24 pm

Re: Remove false active sessions via script

Thu Jul 28, 2011 2:26 pm

No comments from MT?
 
Superdust
Member Candidate
Member Candidate
Posts: 182
Joined: Mon Jun 11, 2007 3:24 pm

Re: Remove false active sessions via script

Mon Dec 26, 2011 8:35 pm

Any update on this problem from MT?

I still have sessions that hangs, preventing clients connecting to usermanager....
 
tester
just joined
Posts: 5
Joined: Fri Sep 06, 2013 4:18 am

Re: Remove false active sessions via script

Fri Sep 06, 2013 4:31 am

I wrote this one, hope it helps :)
# 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")
      }
    }
  }
}
Last edited by tester on Thu Aug 07, 2014 2:01 am, edited 1 time in total.
 
cleiton
just joined
Posts: 4
Joined: Tue Mar 25, 2014 5:34 pm

Re: Remove false active sessions via script

Tue Mar 25, 2014 8:47 pm

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?
 
tester
just joined
Posts: 5
Joined: Fri Sep 06, 2013 4:18 am

Re: Remove false active sessions via script

Thu Aug 07, 2014 2:03 am

I've updated the code for RouterOS 6.x
 
ZSam
Frequent Visitor
Frequent Visitor
Posts: 61
Joined: Tue May 10, 2016 6:40 pm

Re: Remove false active sessions via script

Sat Mar 12, 2022 12:52 am


Here the script if somebody need it ...
WORKING!!!

THANK YOU FOR THIS!!!

Have had this problem for SO long and finally came across your solution!
 
AAw
just joined
Posts: 5
Joined: Tue Sep 02, 2014 5:16 pm

Re: Remove false active sessions via script

Sat Jan 14, 2023 10:13 pm

I've updated the code for RouterOS 6.x
can you put the updated script here please. I'm using RouterOS 6.49.6
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Remove false active sessions via script

Sat Jan 14, 2023 11:49 pm

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:
viewtopic.php?p=977598#p977598

Who is online

Users browsing this forum: No registered users and 6 guests