一、需求
- 创建10个用户并设置随便密码,密码记录到一个文件里,文件名userinfo.txt
- 用户从user_00到user_09
- 密码要求,密码长度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 ~]$