生成用户名密码的脚本

脚本简介

这是一个用来生成用户名密码的软件

脚本演示

1
2
3
4
5
6
7
8
9
#sh adduser.sh
usage: adduser.sh [add | del ] username
## 添加用户
#sh adduser.sh add niu1
USERNAME : niu1
PASSWORD : kvgSOxUMJQb6k69M
## 删除用户
#sh adduser.sh del niu1
delete niu1 successful ....

脚本具体内容

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
#!/bin/bash

USERNAME="$2"
RETVAL="0"

color_shell(){

RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'

}

usage(){
echo $"usage: $0 [add | del ] username "

}

create_passwd(){

GENPASSWD=`strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 16 | tr -d '\n'; echo`

}

create_username(){

id $USERNAME &>/dev/null
RETVAL="$?"
if [[ $RETVAL -eq 0 ]]; then
echo "$USERNAME cunzai"
else
useradd $USERNAME
echo $GENPASSWD | passwd $USERNAME --stdin &>/dev/null
echo -e "$GREEN_COLOR USERNAME : $USERNAME $RES"
echo -e "$GREEN_COLOR PASSWORD : $GENPASSWD $RES"
fi
mkdir /home/$USERNAME/tools -p

}

delete_username(){

userdel -r $USERNAME
RETVAL=$?
if [[ $RETVAL -eq 0 ]]; then
echo -e "$GREEN_COLOR delete $USERNAME successful .... $RES"
else
echo -e "$RED_COLOR $USERNAME not delete ..... $RES"
fi
}

color_shell

case $1 in
add )
create_passwd
create_username
;;
del )
delete_username
;;
* )
usage
;;
esac