-
Notifications
You must be signed in to change notification settings - Fork 0
/
lec5vid4Project
executable file
·76 lines (58 loc) · 1.75 KB
/
lec5vid4Project
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
#!/bin/bash
#
# this script creates a new user on the local system
# this script takes username as an argument to the script
# optionally , you can also provide a comment as an argument for the account
# password will be automatically genrated for the account
# username , password , host will be displayed
# script will conform to Linux program standard conventions.
# conform to standard UNIX/Linux program conventions. Namely, sending errors to STDERR.
# make sure the script run with super user privilages
if [ $UID -ne 0 ]
then
echo "please run the script as root" >&2 # >&2 same as 1>&2
exit 1
fi
# if they don't supply atleast one argument , then give them help
if [ $# -lt 1 ]
then
echo " usage ${0} USER_NAME [COMMENT] .. " >&2
exit 1
fi
# the first parameter is the username
USER_NAME="${1}"
# the rest parameter is comment
shift
COMMENT="${@}"
# Generate a password
PASSWORD=$(date +%s%N$RANDOM | sha256sum | head -c48)
# Create the user with the password
useradd -c "${COMMENT}" -m $USER_NAME &> /dev/null
# Check to see if the useradd command succeded
# we dont want to tell the user that the account is created even if it was not created
if [ $? -ne 0 ]
then
echo 'the account could not be created' >&2
exit 1
fi
# SET the password
echo $PASSWORD | passwd --stdin ${USER_NAME} &> /dev/null
# 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 &> /dev/null
# 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}"
echo
echo 'Host'
echo "${HOSTNAME}"
exit 0