-
Notifications
You must be signed in to change notification settings - Fork 11
/
kick.sh
151 lines (133 loc) · 2.6 KB
/
kick.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/local/bin/bash
#File: kick
#Purpose: Kick user(s) off the system by user name.
#Usage: kick.sh [username]
#Warning: kick does not currently discern between multiply logins. Kick will kill all user process with the given name.
#Author: bernie724@yahoo.com (Bernie Thompson)
#History: Nov 28 2000
#Changes: (r.09) Nov 30 2000
#Todo: kick an array of users,
#discern between multiply user process,
#write rkick to manage remote users, send different signals...
un=$*
ypd=`domainname`
kick_user()
{
ps -u $un | awk '{print $1}' > /tmp/kl.$$
while read buffer
do
unp=`echo $buffer`
if [ "$unp" = "PID" ]
then
echo "Processing PID(s)"
else
echo "User $un with PID $unp is in the kill queue."
kill -9 $unp >> /dev/null
fi
done < /tmp/kl.$$
rm -r /tmp/kl.$$
}
eval_user()
{
who | awk '{print $1}' > /tmp/vu.$$
while read buffer
do
vu=`echo $buffer`
if [ "$vu" = "$un" ]
then
vuf=$vu
fi
done < /tmp/vu.$$
rm -r /tmp/vu.$$
}
eval_ouser()
{
cat /etc/passwd | cut -d":" -f1 > /tmp/vou.$$
while read buffer
do
vou=`echo $buffer`
if [ "$vou" = "$un" ]
then
ovuf=$vou
fi
done < /tmp/vou.$$
rm -r /tmp/vou.$$
}
eval_youser()
{
ypcat passwd > /tmp/ypp.$$
cat /tmp/ypp.$$ | cut -d":" -f1 > /tmp/yvou.$$
while read buffer
do
yvou=`echo $buffer`
if [ "$yvou" = "$un" ]
then
ovuf=$yvou
fi
done < /tmp/yvou.$$
rm -r /tmp/yvou.$$
rm -r /tmp/ypp.$$
}
exit_user ()
{
if [ "$ovuf" = "" ]
then
echo "User $un does not exist. Exiting..."
exit 1
else
echo "User $ovuf is offline. Exiting..."
exit 1
fi
}
if [ "$un" = "" ]
then
echo "Usage: kick.sh [username]"
exit 1
fi
# Root User cannot be kicked
if [ "$un" = "root" ]
then
echo "User $un cannot be kicked! Exiting..."
exit
fi
# TM user in vista cannot be kicked
if [ "$un" = "tm" ]
then
echo "User $un cannot be kicked! Exiting..."
exit
fi
# See if the user is online: punt or exit
eval_user
if [ "$vuf" = "" ]
then
eval_ouser
if [ "$ypd" != "" ]
then
eval_youser
fi
exit_user
else
kick_user
fi
# Check and Return User Status After Kick
vuf=""
eval_user
if [ "$vuf" = "" ]
then
echo "User $un Kicked!"
exit 1
else
echo "User $un still exists. Exiting..."
exit 1
fi
##############################################################################
### This script is submitted to BigAdmin by a user of the BigAdmin community.
### Sun Microsystems, Inc. is not responsible for the
### contents or the code enclosed.
###
###
### Copyright 2007 Sun Microsystems, Inc. ALL RIGHTS RESERVED
### Use of this software is authorized pursuant to the
### terms of the license found at
### http://www.sun.com/bigadmin/common/berkeley_license.html
##############################################################################