This repository has been archived by the owner on Feb 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kick-user.sh
executable file
·91 lines (80 loc) · 2.48 KB
/
kick-user.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/sh
# Set default flag for script:
DEFAULT_FLAG='-s' # Flag -s means kick from all shells.
SUDO=''
# Use $(id -u) instead of $UID for cross-platform performance.
if [ $(id -u) != 0 ]; then
# If you are not root, then we add sudo to beginning of some commands.
SUDO='sudo'
fi
displayHelp() {
echo -n '> You need to include at least 1 argument(username), '
echo 'flags are optional.'
echo
printf '%-12s' '> Usage: '
echo "$(basename ${0}) [username] [-r|-s|-l]"
printf '%-12s' '> Example: '
echo "$(basename ${0}) john"
printf '%-12s' '> Example: '
echo "$(basename ${0}) john -r"
echo
echo '> Flags:'
printf '%-12s' ' -r'
echo -n 'Kick only remote client (ssh).'
if [ ${DEFAULT_FLAG} = '-r' ]; then echo ' (Default)'; else echo; fi
printf '%-12s' ' -s'
echo -n 'Kick from all shells (including ssh).'
if [ ${DEFAULT_FLAG} = '-s' ]; then echo ' (Default)'; else echo; fi
printf '%-12s' ' -l'
echo -n 'Same as -s, but it also locks account after being kicked.'
if [ ${DEFAULT_FLAG} = '-l' ]; then echo ' (Default)'; else echo; fi
echo
exit 1
}
if [ ${#} -lt 1 ]; then
displayHelp
fi
# Check if username is valid and assign variables.
if [ ${#1} -lt 3 ] || [ $(echo ${1} | cut -c 1) = '-' ]; then
if [ ${1} = '-r' ] || [ ${1} = '-s' ] || [ ${1} = '-l' ]; then
FLAG=${1}
USER=${2}
fi
else
FLAG=${2}
USER=${1}
fi
# Check if username is really set.
if [ "${USER}" = '' ]; then
displayHelp
fi
# Check if username exists in system.
id -u ${USER} > /dev/null 2>&1
if [ ${?} -eq 1 ]; then
echo "Username '${USER}' does not exist."
exit 1
fi
# Force flags to be lowercase.
if [ ${#} -lt 2 ]; then
FLAG=$(echo ${DEFAULT_FLAG} | tr A-Z a-z)
else
FLAG=$(echo ${FLAG} | tr A-Z a-z)
fi
if [ ${FLAG} = '-r' ]; then
ps -U ${USER} | grep sshd | sed 's/^ *//g' | cut -d ' ' -f 1 | \
${SUDO} xargs kill -9 > /dev/null 2>&1
elif [ ${FLAG} = '-s' ] || [ ${FLAG} = '-l' ]; then
# If shells are open via ssh, shells will get killed with ssh and
# kill command will return error that pid of closed shells are not found.
# To prevent script from displaying error, we redirect stderr to /dev/null.
ps -U ${USER} | grep sh | sed 's/^ *//g' | cut -d ' ' -f 1 | \
${SUDO} xargs kill -9 > /dev/null 2>&1
fi
if [ ${FLAG} = '-l' ]; then
if [ $(uname -o) = 'FreeBSD' ]; then
${SUDO} pw lock ${USER}
else
${SUDO} usermod -L ${USER}
fi
fi
exit 0