Shell实操(二):批量创建用户并设置密码

一、需求

  1. 创建10个用户并设置随便密码,密码记录到一个文件里,文件名userinfo.txt
  2. 用户从user_00到user_09
  3. 密码要求,密码长度15,包含1个特殊字符。

二、实现及关键知识点

1、脚本内容

# cat 2.sh 
#! /bin/bash
if [ -f /tmp/userinfo.txt ]
then 
    rm -f /tmp/userinfo.txt
fi

if ! which mkpasswd
then
    yum install -y expect
fi

for num in `seq -w 0 09`
do
    password=`mkpasswd -l 15 -s 1`
    useradd user_${num} && echo "${password}" | passwd  --stdin  user_${num}
    echo "user_${num} ${password}"  >>  /tmp/userinfo.txt
done

2、关键知识点

(1)mkpasswd的用法

(2)Seq的用法

(3)Passwd的用法

三、验证

#执行脚本
# bash 2.sh 
/usr/bin/mkpasswd
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.

# 查看执行后的记录文档
# cat /tmp/userinfo.txt 
user_00 zekya6msXc=n2Ue
user_01 P0bw[jmaqG4rpmn
user_02 7lgJH6>eymocfrd
user_03 "u5mkfxgvCRwg3e
user_04 k1pUqkxq4w-pDjy
user_05 2lkbdyrZ~3pspdA
user_06 2LIfpedno0kp\or
user_07 He5bmvy5pU*mkbu
user_08 gWAyst}edm76joo
user_09 bpa2=Bvczcsd6qG

#使用创建后的用户登录,登录成功
# ssh user_01@10.17.20.11
Password: 
[user_01@localhost ~]$ 

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值