Time Is up!

Now that the kiddos are idle and asking to use the computer (watch videos rather) the script below, with the right permissions (root), can be used to abruptly terminate the user's session (just a minute I'm almost done!) when the timer expires. And you don't even have to be there.
Usage: ./kickuser.py username time
time is in minutes
>cat kickuser.py

#!/usr/bin/python
# Python script to time out a user.
# basically I'm allotting some time for a given user to use the system and after
# the time is up, I zap the user pretty mean eh?
# after time expires destroy all programs used by that user, time is given in seconds
import time
import threading
import sys
import os

username = sys.argv[1] # the username is a string
timeout  = sys.argv[2] # the time out is in minutes

class Timer(threading.Thread):
   def __init__(self, seconds):
      self.runTime = seconds
      threading.Thread.__init__(self)
def run(self):
      time.sleep(self.runTime)
      cmd = 'ps -U' +  username + ' | awk \'{print $1}\''
      for pid in os.popen(cmd).readlines():
              kill_command =  "kill -9 " + pid
              print kill_command
              os.system(kill_command)
              #now lock account
              lock_cmd = "passwd -l" + username
              os.system(lock_cmd)

t = Timer(int(timeout) * 60 )
#start the timer
t.start()
print "setting time out for " , username , " " , timeout , "minutes"
And that's that. Easy to execute and timely executed.

Comments

Popular Posts