It is said on the Internet that there was once a fast thunder pen question to add users in batches. The procedure is very simple. There is a small knowledge point. Set a password for the user without interaction. There are two solutions: 1. expect2 and passwd -- stdin. The first type is common, and the second type is not applicable to redhat. The greatest truths are simple, and the second approach is used. I have also seen related scripts on the Internet. I don't feel perfect. At least, what should I do if one of the added users already exists. Many people do not handle the issue. For a Coder with code cleanliness. I decided
It is said on the Internet that there was once a fast thunder pen question to add users in batches. The procedure is very simple. There is a small knowledge point. Set a password for the user without interaction. There are two solutions: 1. Reverse ct 2. passwd -- stdin. The first type is common, and the second type is not applicable to redhat. The greatest truths are simple, and the second approach is used. I have also seen related scripts on the Internet. I don't feel perfect. At least, what should I do if one of the added users already exists. Many people do not handle the issue. For a Coder with code cleanliness. I decided to write one by myself. Code:
#!/bin/bash#load system functions[ -f /etc/init.d/functions ] && . /etc/init.d/functionsfori in`seq -w 10`do#generate passwd,8random charspasswd=`echo $(date +%t%N)$RANDOM|md5sum|cut -c 2-9`#check wheather the username isinthe /etc/passwd#important awk usethe shell variablecheck=`awk -F ':''/^'gccmx$i'/ {print $1}'/etc/passwd`if[ -z "$check"];thenuseradd gccmx$i &>/dev/null&& user_status=$?echo $passwd | passwd --stdin gccmx$i &>/dev/null&& passwd_status=$?elseuser_status=1passwd_status=1fiif[ "$user_status"-eq 0-a "$passwd_status"-eq 0];thenaction "The user gccmx$i is add successfully!"/bin/trueecho -e "username:gccmx${i}\tpassword:${passwd}">>/tmp/userinfo.txtelseaction "The user gccmx$i is add faild!"/bin/falseecho -e "username:gccmx${i}\tpassword:${passwd}">>/tmp/faild_info.txtfiunset user_status && unset passwd_statussleep 1done
Running Effect (gccmx05 already exists, so adding failed. The username and password information is stored in/tmp/user_info.txt ):