linux批量添加用户shell并随机生成用户密码

3年前 (2017-07-11) gtj linux, shell 0评论 已收录 212℃

交互式输入要创建的用户并产生随机密码。密码位数可以自己修改。

#!/bin/bash
#*****************************************************
#         Author: suixiaofeng
#           blog:https://bk.devopstack.cn
#          Email: 258818040@qq.com 
#  Last modified: 2017-07-11 15:20
#       Filename: 20170711.sh
#    Description: 
#****************************************************
. /etc/init.d/functions
#user="test"
passfile="/tmp/user.log"
read -p "pls input username:" user
[ -z $user ] && {
    echo "Usage:$0  user"
    exit 1
}

read -p "pls input create two num:" num1 num2
[ -z $num1 -o -z  $num2 ] && {

    echo "Usage:$0  NUM1 NUM2 "
    exit 1
}

for num in `seq -w $num1 $num2`
 do
   pass="`echo "test$RANDOM" |md5sum |cut -c 3-11`"
   useradd $user$num &>/dev/null &&\
   echo "$pass"|passwd --stdin $user$num &>/dev/null &&\
   echo  -e "user:$user$num\tpasswd:$pass" >>$passfile
   if [ $? -eq 0 ] 
    then 
         action  "$user$num is ok" /bin/true
    else 
        if [ `id $user$num |wc -l` -eq 1 ] ; then
 
          action  "$user$num is exits " /bin/false
       fi
   fi
done 
 echo --------------------------------------------------
cat $passfile && >$passfile

测试:

[root@sf106232 script]# sh 20170711.sh  
pls input username:sddsb
pls input create two num:11 15
sddsb11 is ok                                              [  OK  ]
sddsb12 is ok                                              [  OK  ]
sddsb13 is ok                                              [  OK  ]
sddsb14 is ok                                              [  OK  ]
sddsb15 is ok                                              [  OK  ]
--------------------------------------------------
user:sddsb11	passwd:d3124a198
user:sddsb12	passwd:4fd2607b8
user:sddsb13	passwd:d046ca964
user:sddsb14	passwd:5b21daf09
user:sddsb15	passwd:0d828efaa
博主

这货来去如风,什么鬼都没留下!!!

相关推荐

嗨、骚年、快来消灭0回复。

×
订阅图标按钮
Less is more!!!