This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control_user.sh
executable file
·197 lines (183 loc) · 4.51 KB
/
control_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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/sh
# Group 6
# Peng Chhaileng
# Phang Ratanak
# Ret Sokheang
# On Bunthoeurn
# Heng Bunhak
# Phot Pheanitya
# Kev Chhunly
# Functions Area
check_root_user() {
if [ "$EUID" -ne 0 ]
then echo "[-] Permission denied! Are you root?"
exit
fi
}
menu() {
echo "#--------------------MENU---------------------"
echo "# 1. Create Users from File "
echo "# 2. Delete User "
echo "# 3. Update User "
echo "# 4. Create Group "
echo "# 5. Delete group "
echo "# 6. Add User to Group "
echo "# 7. Show all users in system "
echo "# 8. Show all groups in system "
echo "# 9. Show account information "
echo "# 0. Exit "
echo -n "# Choose options: "
}
menu_update_user() {
echo "#--------------------MENU---------------------"
echo "# 1. Update Information "
echo "# 2. Change Password "
echo "# 3. Add Description to User "
echo "# 4. Change Username (Login Name) "
echo "# 0. Back "
echo -n "# [Update User] options: "
}
pause() {
echo -n $1
read
}
add_user() {
egrep "^$1" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "[-] User $1 exists!"
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $2)
useradd -m -p $pass $1
[ $? -eq 0 ] && echo "[+] User has been added to system!" || echo "[-] Failed to add a user!"
fi
}
controller() {
users=()
while IFS='' read -r user || [[ -n "$user" ]]; do
if ! [ -z $user ]; then
users+=("$user")
fi
done < "$1"
echo "[+] Found ${#users[@]} users"
while true
do
menu
read choose
case $choose in
"1") count=1
for i in ${users[@]}
do
IFS=':' read -ra record <<< "$i"
echo "User record : $count"
echo "Username: ${record[0]}"
add_user ${record[0]} ${record[1]}
count=`expr $count + 1`
done
pause "Press enter to continue..."
;;
"2") read -p "[*] Enter username: " usr
userdel -r $usr > /dev/null 2>&1
del_status=$?
if [ $del_status -eq 0 ]; then
echo "[+] User $usr deleted successfully!"
else
echo "[-] User $usr is not exist!"
fi
pause "Press enter to continue..."
;;
"3") read -p "[*] Enter username: " usr
id $usr > /dev/null 2>&1
usr_status=$?
if [ $usr_status -eq 0 ]; then
while true
do
menu_update_user
read c
case $c in
"1") chfn $usr
pause "Press enter to continue..."
;;
"2") passwd $usr
pause "Press enter to continue..."
;;
"3") read -p "[*] Description (no space): " cmt
usermod -c $cmt $usr
cat /etc/passwd | grep $usr
pause "Press enter to continue..."
;;
"4") read -p "[*] Enter new username: " n_usr
usermod -l $n_usr $usr
echo "[+] Username update successfully!"
pause "Press enter to continue..."
;;
"0") break
;;
esac
clear
done
else
echo "[-] User $usr is not exist!"
pause "Press enter to continue..."
fi
;;
"4") #clear
read -p "[*] Enter group name: " gro
groupadd $gro
pause "Press enter to continue..."
;;
"5") #clear
read -p "[*] Enter group name: " gro
groupdel $gro
pause "Press enter to continue..."
;;
"6") #clear
read -p "[*] Enter group name: " gro
read -p "[*] Enter user name: " usr
usermod -G $gro $usr
pause "Press enter to continue..."
;;
"7") clear
cut -d: -f1 /etc/passwd
pause "Press enter to continue..."
;;
"8") clear
cat /etc/group
pause "Press enter to continue..."
;;
"9") #clear
read -p "[*] Enter username: " usr
id $usr > /dev/null 2>&1
usr_status=$?
if [ $usr_status -eq 0 ]; then
chage -l $usr
pause "Press enter to continue..."
else
echo "[-] User not found!"
pause "Press enter to continue..."
fi
;;
"0") exit
;;
*) echo "Command Not Found"
;;
esac
clear
done
}
main() {
read -p "[*] Enter users file name (users.txt): " file_name
#file_name="users.txt"
if [ -z $file_name ]; then
file_name="users.txt"
fi
if ! [ -s $file_name ]; then
echo "[-] File $file_name is not found!"
else
echo "[+] Using file $file_name..."
echo "[+] Reading user from file..."
controller $file_name
fi
}
# Main Area
check_root_user
main