Shell batch add users

Source: Internet
Author: User
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 ):

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.