创建多个用户并添加随机密码

#!/bin/bash
echo "创建成功的用户" > /root/user_name
read -ep "请输入要创建的用户:" num
for i in `seq 1 $num` do pw=`cat /dev/urandom | head -1|md5sum |head -c 5` #生成随机5位密码 id user-$i > /dev/null 2>&1 #id 用户名 查看是否存在用户 if [ $? -eq 0 ];then while true do read -ep "user-$i用户已经存在,是否要删除此用户(y/n):" choice case $choice in y|Y|yes) userdel -rf user-$i > /dev/null echo "user-$i删除成功" break ;; n|N|no) break ;; *) echo "输入的字符有误,请重新输入" esac done else useradd user-$i > /dev/null 2>&1 if [ $? -eq 0 ];then echo "user-$i用户创建成功" echo "user-$pw" | passwd --stdin user-$i #给用户添加
echo "user-$i $pw" >> /root/user_name #把账号密码添加到文件user_name中
else
      echo "用户创建失败"
    fi
  fi
done
cat /root/user_name

 

posted @ 2019-09-28 10:43  |思念谁|  阅读(111)  评论(0)    收藏  举报