Jump to content

Auto-disconnect other network users


Marc
 Share

Recommended Posts

Hi,

at my day job we're using a standard Windows 2000 PC which offers some file shares to our department. Sadly, only 10 connections at the same time are allowed. To get more, we would have to buy a W2k Server license.

Would be the most expensive solution to this problem, since the share does not have to be connected permanently, its just used as some kind of data exchange directory.

Found out that several people have connected the shares permanently and/or keep a window open without doing anything in it.

So I wrote a small script which uses the "net session" command to find out how long the users have been idle and removes the idling ones o:)

This version works fine on a german w2k system, perhaps the output format of an english version of "net session" would be different, but I doubt that.

; Language: AutoIt 3.0.103
; Autor   : M.R.Delhalle
; Datum   : 2004-09-15

#include<process.au3>
#include<file.au3>

Opt("TrayIconDebug",1)
$datei = @TempDir & "\netsession.txt"

while 1
   
; List all connected users in tempfile
_rundos("net session >" & $datei)
   
   $file = FileOpen($datei, 0)
   If $file = -1 Then
      MsgBox(0, "Error", "Could not create temporary file for net session")
      Exit
   EndIf
   
   if FileGetSize($datei)>50 then check(); some shares seem to exist

   FileClose($datei)
   FileDelete($datei)
   
   sleep (300000); 5 min Pause
Wend
  
Func check()
   
   $counter = 0
  ; Read file and search for connected users
   do
      $line = FileReadLine($file)
   Until StringInStr($line, "------------") > 0
   
   While 1
      $line = FileReadLine($file)
      If @error = -1 Then ExitLoop
      $time = StringMid($line, 69)     ; extrat time entry from current line
      $timesplit = StringSplit($time,":"); split into hours and minutes
      if ($timesplit[0]=3) and (($timesplit[1] > 0) or ($timesplit[2]>10)) Then; disconnect after 10 minutes
        $computer = StringStripWS(StringLeft($line, 22),2)
       ; tricky: echo y = answer yes to the requester
       ; in german OS replace the "y" with a "j"
        _rundos("echo y|net session " & $computer & " /delete")
        $counter=$counter + 1
      EndIf   
   Wend
   if $counter>0 then TrayTip ( "Removed connected user", "Successfully disconnected " & $counter & " user(s)", 5 ) 

EndFunc

By the way, this script is in use for over 4 month now and since it runs, I never ever had a team mates request to remove other users manually, which happened four times a day before... :lmao:

Best regards,

Marc

Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL)

Link to comment
Share on other sites

Hi,

at my day job we're using a standard Windows 2000 PC which offers some file shares to our department. Sadly, only 10 connections at the same time are allowed. To get more, we would have to buy a W2k Server license.

Would be the most expensive solution to this problem, since the share does not have to be connected permanently, its just used as some kind of data exchange directory.

Found out that several people have connected the shares permanently and/or keep a window open without doing anything in it.

So I wrote a small script which uses the "net session" command to find out how long the users have been idle and removes the idling ones  :)

This version works fine on a german w2k system, perhaps the output format of an english version of "net session" would be different, but I doubt that.

; Language: AutoIt 3.0.103
; Autor   : M.R.Delhalle
; Datum   : 2004-09-15

#include<process.au3>
#include<file.au3>

Opt("TrayIconDebug",1)
$datei = @TempDir & "\netsession.txt"

while 1
   
; List all connected users in tempfile
_rundos("net session >" & $datei)
   
   $file = FileOpen($datei, 0)
   If $file = -1 Then
      MsgBox(0, "Error", "Could not create temporary file for net session")
      Exit
   EndIf
   
   if FileGetSize($datei)>50 then check(); some shares seem to exist

   FileClose($datei)
   FileDelete($datei)
   
   sleep (300000); 5 min Pause
Wend
  
Func check()
   
   $counter = 0
  ; Read file and search for connected users
   do
      $line = FileReadLine($file)
   Until StringInStr($line, "------------") > 0
   
   While 1
      $line = FileReadLine($file)
      If @error = -1 Then ExitLoop
      $time = StringMid($line, 69)      ; extrat time entry from current line
      $timesplit = StringSplit($time,":"); split into hours and minutes
      if ($timesplit[0]=3) and (($timesplit[1] > 0) or ($timesplit[2]>10)) Then; disconnect after 10 minutes
        $computer = StringStripWS(StringLeft($line, 22),2)
      ; tricky: echo y = answer yes to the requester
      ; in german OS replace the "y" with a "j"
        _rundos("echo y|net session " & $computer & " /delete")
        $counter=$counter + 1
      EndIf   
   Wend
   if $counter>0 then TrayTip ( "Removed connected user", "Successfully disconnected " & $counter & " user(s)", 5 ) 

EndFunc

By the way, this script is in use for over 4 month now and since it runs, I never ever had a team mates request to remove other users manually, which happened four times a day before...  o:)

Best regards,

Marc

<{POST_SNAPBACK}>

Hello thx To Share it Out, great job :lmao: can i adde it on my forum wis your credits to it? :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...