flipje

kill-user-timed

Aug 24th, 2011
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.35 KB | None | 0 0
  1. #!/bin/bash
  2. # +-----------------------------------------------------------------------------------------+
  3. # | Kill user with message and disable account                                              |
  4. # | Can be handy in multi user environments                                                 |
  5. # |                                                                                         |
  6. # | Augustus 2011 flip hess [email protected]                                             |
  7. # +-----------------------------------------------------------------------------------------+
  8.  
  9. # Global variables:
  10.  
  11. PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
  12. SCRIPT_PATH="${0}"
  13. TOTARGS="${#}"
  14.  
  15. # set defaults
  16. TIME='60'
  17. MSG='Go to Sleep!'
  18. USER="$( whoami )"
  19. LOCK='NO'
  20.  
  21.  
  22. # Functions:
  23.  
  24.   # exit function
  25.   function die()
  26.   {
  27.     echo "${1}"
  28.     exit 1
  29.   }
  30.  
  31.   # Shows usage function.
  32.   function fShowUsage()
  33.   {
  34.     echo -e "\
  35.    \n\
  36.    Kill user with message script\n\
  37.    \n\
  38.    Usage : ${SCRIPT_PATH} [ -t <time in seconds> ] [ -m <message> ] [ -u <username> ] [ -h ]\n\
  39.    \n\
  40.    -t             Set time in seconds. Default is 60 seconds\n\
  41.    -m             Set kill message. Default is \"Go to sleep!\"\n\
  42.    -u             Set username. Default is YOU!\n\
  43.    -l             Lock users account. set -l YES to lock.\n\
  44.    -h             Shows this help index.\n\
  45.    \n\
  46.    "
  47.  
  48.     return 0
  49.   }
  50.  
  51.   # check for.......
  52.   function fCheck()
  53.   {
  54.     # user must be root:
  55.     [ $(whoami) = root ] || die "User must be root!"
  56.  
  57.     # If no args supplied:
  58.     [ ${TOTARGS} = 0 ] && { fShowUsage; exit 1; }
  59.  
  60.     return 0
  61.   }
  62.  
  63.   # The main function.
  64.   function fMain()
  65.   {
  66.     #get options and set vars
  67.     while getopts "t:m:u:l:h" OPTION
  68.     do
  69.       case ${OPTION} in
  70.        
  71.         t)
  72.           TIME=${OPTARG}
  73.           ;;
  74.         m)
  75.           MSG=${OPTARG}
  76.           ;;
  77.         u)
  78.           USER=${OPTARG}
  79.           ;;
  80.         l)
  81.           LOCK=${OPTARG}
  82.           ;;
  83.         h)
  84.           fShowUsage
  85.           ;;
  86.         ?)
  87.           fShowUsage
  88.           ;;
  89.         *)
  90.           fShowUsage
  91.           ;;
  92.       esac
  93.     done
  94.  
  95.    # kill user:
  96.    fKill "${TIME}" "${USER}" "${MSG}" "${LOCK}"
  97.  
  98.     return 0
  99.   }
  100.  
  101.  
  102.   # the kill function
  103.   function fKill()
  104.   {
  105.    local TIME="${1}"
  106.    local USER="${2}"
  107.    local MSG="${3}"
  108.    local LOCK="${4}"
  109.  
  110.    # Send message to all users terminals
  111.    if [ ${USER} = root ]; then
  112.      for FUCK in $( ls /dev/pts | grep '[0-9]' )
  113.      do
  114.        echo "WARNING MESSAGE! - SOME IDIOT ADMIN IS TRYING TO KILL ALL THE ROOT PROCESSES!" > /dev/pts/${FUCK}
  115.        exit 1
  116.      done
  117.    else
  118.      for TERMS in $( w -hs ${USER} | awk '{print $2}' | cut -d'/' -f 2 | grep -v tty )
  119.      do
  120.        echo "WARNING MESSAGE! - ${MESSAGE} - Your sessions will be killed in ${TIME} seconds." > /dev/pts/${TERMS}
  121.        sleep "${TIME}"
  122.        # kill all users processes
  123.        ps -u ${USER} -o pid | grep -v PID | awk '{system ("kill -9 " $1) }'
  124.      done
  125.    fi
  126.  
  127.    if [ ${LOCK} = YES ]; then
  128.      # disable users account if not root:
  129.      [ ${USER} != root ] && passwd -l ${USER}
  130.    fi
  131.  
  132.    return 0
  133.  
  134.   }
  135.  
  136.  
  137. # Do the magic:
  138.  
  139.  # check environment:
  140.   fCheck
  141.  
  142.  # Start the program:
  143.   fMain "${@}"
  144.  
  145.  # Exit with previous return code:
  146.   exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment