shell习题-批量创建用户并设置密码

1.要求:

添加user_00 – user_09 10个用户,并且给他们设置一个随机密码,
密码要求10位包含大小写字母以及数字,
注意需要把每个用户的密码记录到一个日志文件里。

2.脚本答案:

[root@liang ~]# cat /scritp/2018-06-14/usercreate,sh 

#!/bin/bash
for i in `seq -w 00 09`
do
        password=`/usr/bin/echo $RANDOM |md5sum |cut -c 1-10` 
        echo "$password" >>/test/password.log
        useradd user_${i}
        if [ $? -eq 0 ];then
                echo "$password"|passwd --stdin user_${i}
        fi
done


3.执行脚本:

[root@liang 2018-06-14]# bash usercreate.sh 
Changing password for user user_00.
passwd: all authentication tokens updated successfully.
Changing password for user user_01.
passwd: all authentication tokens updated successfully.
Changing password for user user_02.
passwd: all authentication tokens updated successfully.
Changing password for user user_03.
passwd: all authentication tokens updated successfully.
Changing password for user user_04.
passwd: all authentication tokens updated successfully.
Changing password for user user_05.
passwd: all authentication tokens updated successfully.
Changing password for user user_06.
passwd: all authentication tokens updated successfully.
Changing password for user user_07.
passwd: all authentication tokens updated successfully.
Changing password for user user_08.
passwd: all authentication tokens updated successfully.
Changing password for user user_09.
passwd: all authentication tokens updated successfully.

4.密码日志

[root@liang 2018-06-14]# cat -n /test/password.log 
     1  8f173d44c5
     2  3191176109
     3  eb69246aac
     4  8e779f3fcd
     5  64ce0eea97
     6  37dab49727
     7  0e0b3ba522
     8  8845012401
     9  4f80f29512
    10  4f244042b3

5.删除用户:

[root@liang 2018-06-14]# tail -10 /etc/passwd|awk -F ":" '{print "userdel -r "$1}'|bash
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值