-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd-Delete_user
More file actions
96 lines (81 loc) · 1.9 KB
/
Copy pathAdd-Delete_user
File metadata and controls
96 lines (81 loc) · 1.9 KB
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
#!/bin/bash
# This script enables you to add or remove user
ARCHIEVE_DIR='/archieve'
#CASE="${1}"
#case ${1} in
# ADD) adduser "$@" ;;
# DEL) deleteuser "$@" ;;
# ?) echo " usage ${0} add USER_NAME [FULL NAME] nothing" ;;
#esac
addo(){
#USER_NAME="${2}"
#shift
#COMMENT=${@} # FULL NAME
# ADDING USER
echo "adding user with username ${USER_NAME}"
useradd -m "${USER_NAME}"
if [ "${?}" -ne 0 ]
then
echo "username can not be created try again with a differnt one" >&2
exit 1
fi
# CREATING SYSTEM GENERATED PASSWORD
PASSWORD=$( date +%s%N$RANDOM | sha256sum | head -c32 )
SPECIAL_CHARACTER_1=$(echo '@!#%^^$*()+;.><' |fold -w1 | shuf | head -c1)
SPECIAL_CHARACTER_2=$(echo '@!#%^^$*()+;.><' |fold -w1 | shuf | head -c1)
PASSWORD="${SPECIAL_CHARACTER_2}${PASSWORD}${SPECIAL_CHARACTER_1}"
echo "${PASSWORD}" | passwd --stdin "${USER_NAME}"
# Check to see if the passwd comamnd succeded
if [ $? -ne 0 ]
then
echo 'the password could not be set' >&2
exit 1
fi
# force passwrd change on the first login
passwd -e "${USER_NAME}"
# Display the username, password, and the host where the user was created.
echo # leaves a blank space
echo 'username'
echo "${USER_NAME}"
echo
echo 'password'
echo "${PASSWORD}"
exit 0
}
delo()
{
#USER_NAME=${2}
# DELETING USER
read -p "do you want to remove the home directory(y/n)" REMOVE
if [ "${REMOVE}" = 'y' ]
then
echo 'deleting user along with home directory'
userdel -r "${USER_NAME}"
if [ "${?}" -ne 0 ]
then
echo "user ${USER_NAME} can not be deleted " >&2
exit 1
fi
else
userdel -r "${USER_NAME}"
if [ $? != 0 ]
then
echo "user ${USER_NAME} can not be deleted " >&2
exit 1
fi
fi
exit 0
}
USER_NAME=${2}
if [[ "${1}" = 'add' ]]
then
echo $#
addo
elif [[ "${1}" = 'delete' ]]
then
delo
else
echo " usage ${0} add USER_NAME [FULL NAME] nothing"
exit 1
fi
exit 0