Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # +-----------------------------------------------------------------------------------------+
- # | Kill user with message and disable account |
- # | Can be handy in multi user environments |
- # | |
- # | Augustus 2011 flip hess [email protected] |
- # +-----------------------------------------------------------------------------------------+
- # Global variables:
- PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
- SCRIPT_PATH="${0}"
- TOTARGS="${#}"
- # set defaults
- TIME='60'
- MSG='Go to Sleep!'
- USER="$( whoami )"
- LOCK='NO'
- # Functions:
- # exit function
- function die()
- {
- echo "${1}"
- exit 1
- }
- # Shows usage function.
- function fShowUsage()
- {
- echo -e "\
- \n\
- Kill user with message script\n\
- \n\
- Usage : ${SCRIPT_PATH} [ -t <time in seconds> ] [ -m <message> ] [ -u <username> ] [ -h ]\n\
- \n\
- -t Set time in seconds. Default is 60 seconds\n\
- -m Set kill message. Default is \"Go to sleep!\"\n\
- -u Set username. Default is YOU!\n\
- -l Lock users account. set -l YES to lock.\n\
- -h Shows this help index.\n\
- \n\
- "
- return 0
- }
- # check for.......
- function fCheck()
- {
- # user must be root:
- [ $(whoami) = root ] || die "User must be root!"
- # If no args supplied:
- [ ${TOTARGS} = 0 ] && { fShowUsage; exit 1; }
- return 0
- }
- # The main function.
- function fMain()
- {
- #get options and set vars
- while getopts "t:m:u:l:h" OPTION
- do
- case ${OPTION} in
- t)
- TIME=${OPTARG}
- ;;
- m)
- MSG=${OPTARG}
- ;;
- u)
- USER=${OPTARG}
- ;;
- l)
- LOCK=${OPTARG}
- ;;
- h)
- fShowUsage
- ;;
- ?)
- fShowUsage
- ;;
- *)
- fShowUsage
- ;;
- esac
- done
- # kill user:
- fKill "${TIME}" "${USER}" "${MSG}" "${LOCK}"
- return 0
- }
- # the kill function
- function fKill()
- {
- local TIME="${1}"
- local USER="${2}"
- local MSG="${3}"
- local LOCK="${4}"
- # Send message to all users terminals
- if [ ${USER} = root ]; then
- for FUCK in $( ls /dev/pts | grep '[0-9]' )
- do
- echo "WARNING MESSAGE! - SOME IDIOT ADMIN IS TRYING TO KILL ALL THE ROOT PROCESSES!" > /dev/pts/${FUCK}
- exit 1
- done
- else
- for TERMS in $( w -hs ${USER} | awk '{print $2}' | cut -d'/' -f 2 | grep -v tty )
- do
- echo "WARNING MESSAGE! - ${MESSAGE} - Your sessions will be killed in ${TIME} seconds." > /dev/pts/${TERMS}
- sleep "${TIME}"
- # kill all users processes
- ps -u ${USER} -o pid | grep -v PID | awk '{system ("kill -9 " $1) }'
- done
- fi
- if [ ${LOCK} = YES ]; then
- # disable users account if not root:
- [ ${USER} != root ] && passwd -l ${USER}
- fi
- return 0
- }
- # Do the magic:
- # check environment:
- fCheck
- # Start the program:
- fMain "${@}"
- # Exit with previous return code:
- exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment