-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser-manager.sh
143 lines (124 loc) · 3.09 KB
/
user-manager.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
!#/bin/bash
# Script for automated user adding and deletion and even more :)
# Please remember some of the features of this script can be run only with running this script with sudo permissions
function displayUsers() {
awk -F: '{ print $1}' /etc/passwd
}
function displayGroups() {
getent group | cut -d: -f1
}
function displaySudoers() {
grep '^sudo:.*$' /etc/group | cut -d: -f4
}
function addSomeGroup() {
echo ""
echo "Please type the name of the new group ( AVOID NAMES ALREADY EXISTING ! )"
read group_name
groupadd "$group_name"
if [ "$?" == 0 ]; then
echo " Congrats ! You have succesfully created a new group: "$group_name" :) "
else
echo "Something went wrong :( . Exiting ... "
exit 1
fi
}
function addUser() {
echo ""
echo "Please type the name of the new user ( AVOID NAMES ALREADY EXISTING ! )"
read user_name
useradd "$user_name"
if [ "$?" == 0 ]; then
echo " Congrats ! You have succesfully created a new user: "$user_name" :) "
else
echo "Something went wrong :( Exiting ... "
exit 1
fi
}
function addUserToGroup() {
echo ""
echo "Please type the name of the user: "
read username
echo ""
echo "Please type the name of the group: "
read groupname
usermod -a -G "$groupname" "$username"
if [ "$?" == 0 ]; then
echo " Congrats ! You have succesfully added "$username" to "$groupname" :) "
else
echo "Something went wrong :( Exiting ... "
exit 1
fi
}
function listUsersFromGroup() {
echo ""
echo "Please type the name of the group: "
read groupname
echo "HERE ARE USERS from: "$groupname""
echo ""
grep "$groupname" /etc/group | awk '{split($0,a,":"); print a[4] }'
echo ""
if [ "$?" == 0 ]; then
echo " Congrats ! You might see users that belong to this group: "$groupname" "
else
echo "Something went wrong :( Exiting ... "
exit 1
fi
}
function menuSelection() {
select y in ListUsers ListGroups ListSudoers AddUser AddGroup AddUserToGroup ListUserFromGroup Quit
do
case $y in "ListUsers")
echo "-----------------------"
echo "Current list of users: "
echo " "
displayUsers
;;
"ListGroups")
echo "-----------------------"
echo "Current list of groups: "
echo " "
displayGroups
;;
"ListSudoers")
echo "-----------------------"
echo "Current list of sudo users: "
echo " "
displaySudoers
;;
"AddUser")
echo "-----------------------"
echo "Let's add some user ! "
echo " "
addUser
;;
"AddGroup")
echo "-----------------------"
echo "Let's add some group ! "
echo " "
addSomeGroup;;
"AddUserToGroup")
echo "-----------------------"
echo "Let's add some user to group ! "
echo " "
addUserToGroup;;
"ListUserFromGroup")
echo "-----------------------"
echo "Let's list users belonging to particular group ! "
echo " "
listUsersFromGroup;;
"Quit") exit ;;
*) echo "SORRY IT'S WRONG DECISION. Eciting ... "
exit 1
esac
break
done
}
echo ""
echo "------------------------------------------------------------"
echo ""
echo " WELCOME IN MY USER MANAGER :) "
echo ""
echo "------------------------------------------------------------"
echo ""
echo "MENU: "
menuSelection