批量添加用户并设置8位随机密码
1.1批量添加用户stu01 stu02 stu03 并设置123456
化简为 添加3个用户设置固定密码
最终目标:
useradd stu01 ;echo 123456|passwd --stdin stu01
useradd stu02 ;echo 123456|passwd --stdin stu02
useradd stu03 ;echo 123456|passwd --stdin stu03
第1个里程碑-生成用户名
方法1
[root@oldboyedu59 ~]# echo stu{01..3}|xargs -n1
stu01
stu02
stu03
第2个里程碑-命令拼接
利用sed/awk 里面
sed 's###g' 后两个井号之间写什么 屏幕就显示什么
awk 利用 "" 双引号里面写什么 屏幕就显示什么
[root@oldboyedu59 ~]# echo stu{01..3}|xargs -n1|sed -r 's#(.*)#useradd \1#g'
useradd stu01
useradd stu02
useradd stu03
[root@oldboyedu59 ~]# echo stu{01..3}|xargs -n1|sed -r 's#(.*)#useradd \1;echo 123456|passwd --stdin \1#g'
useradd stu01;echo 123456|passwd --stdin stu01
useradd stu02;echo 123456|passwd --stdin stu02
useradd stu03;echo 123456|passwd --stdin stu03
第3个里程碑-把内容运行
bash 命令解释器
[root@oldboyedu59 ~]# echo pwd
pwd
[root@oldboyedu59 ~]# echo pwd|bash
/root
[root@oldboyedu59 ~]# echo hostname
hostname
[root@oldboyedu59 ~]# echo hostname |bash
oldboyedu59
找一个 进行测试 测试成功最终交给bash运行
[root@oldboyedu59 ~]# useradd stu02;echo 123456|passwd --stdin stu02
Changing password for user stu02.
passwd: all authentication tokens updated successfully.
交给bash运行
[root@oldboyedu59 ~]# echo stu{01..3}|xargs -n1|sed -r 's#(.*)#useradd \1;echo 123456|passwd --stdin \1#g' |bash
Changing password for user stu01.
passwd: all authentication tokens updated successfully.
useradd: user 'stu02' already exists
Changing password for user stu02.
passwd: all authentication tokens updated successfully.
Changing password for user stu03.
passwd: all authentication tokens updated successfully.
2、批量添加用户stu04 stu05 stu06 随机8位密码
化简为 添加3个用户设置固定密码
第1个里程碑-生成用户名
方法1
[root@oldboyedu59 ~]# echo stu{01..3}|xargs -n1
stu01
stu02
stu03
第二个里程碑 生成随机密码
[root@oldboyedu59 ~]# date +%N
211379317
第3个里程碑-命令拼接
useradd stu06;
pass=date +%N
;
echo pass stu06 >>/root/pass.txt
想要的结果为:
useradd stu04; pass=date +%N
; echo pass stu04 >>/root/pass.txt
useradd stu05; pass=date +%N
; echo pass stu05 >>/root/pass.txt
useradd stu06; pass=date +%N
; echo pass stu06 >>/root/pass.txt
[root@oldboyedu59 ~]# echo stu{04..6}|xargs -n1 |sed 's#.*#useradd &;p=$(date +%N);echo $p|passwd --stdin &;echo $p & >>/p.txt#g'
useradd stu04;p=$(date +%N);echo $p|passwd --stdin stu04;echo $p stu04 >>/p.txt
useradd stu05;p=$(date +%N);echo $p|passwd --stdin stu05;echo $p stu05 >>/p.txt
useradd stu06;p=$(date +%N);echo $p|passwd --stdin stu06;echo $p stu06 >>/p.txt
上面随便找1条 执行 测试是否ok
[root@oldboyedu59 ~]# useradd stu05;p=$(date +%N);echo $p|passwd --stdin stu05;echo $p stu05 >>/p.txt
Changing password for user stu05.
passwd: all authentication tokens updated successfully.
[root@oldboyedu59 ~]# id stu05
uid=1008(stu05) gid=1009(stu05) groups=1009(stu05)
[root@oldboyedu59 ~]# su - oldboy
Last login: Thu Apr 25 09:28:20 CST 2019 from 10.0.0.1 on pts/2
[oldboy@oldboyedu59 ~]$ logout
[root@oldboyedu59 ~]# cat /p.txt
209574583 stu05
[root@oldboyedu59 ~]# su - oldboy
Last login: Thu Apr 25 12:03:36 CST 2019 on pts/0
[oldboy@oldboyedu59 ~]$ su - stu05
Password:
[stu05@oldboyedu59 ~]$
第4个里程碑-交给bash执行 并 检查
[root@oldboyedu59 ~]# echo stu{04..6}|xargs -n1 |sed 's#.*#useradd &;p=$(date +%N);echo $p|passwd --stdin &;echo $p & >>/p.txt#g'
useradd stu04;p=$(date +%N);echo $p|passwd --stdin stu04;echo $p stu04 >>/p.txt
useradd stu05;p=$(date +%N);echo $p|passwd --stdin stu05;echo $p stu05 >>/p.txt
useradd stu06;p=$(date +%N);echo $p|passwd --stdin stu06;echo $p stu06 >>/p.txt
[root@oldboyedu59 ~]# echo stu{04..6}|xargs -n1 |sed 's#.*#useradd &;p=$(date +%N);echo $p|passwd --stdin &;echo $p & >>/p.txt#g'|bash
Changing password for user stu04.
passwd: all authentication tokens updated successfully.
useradd: user 'stu05' already exists
Changing password for user stu05.
passwd: all authentication tokens updated successfully.
Changing password for user stu06.
passwd: all authentication tokens updated successfully.
[root@oldboyedu59 ~]# cat /p.txt
209574583 stu05
570581626 stu04
600781084 stu05
660376548 stu06
2、Linux下面生成随机密码方法
方法1 tr + head
[root@oldboyedu59 ~]# tr -cd 'a-zA-Z0-9' </dev/urandom |head -c8
B0iV36fZ[root@oldboyedu59 ~]#
方法2 date +md5sum/sha512sum + head
[root@oldboyedu59 ~]# date +%N
211379317
date +%N |md5sum |head -c10
[root@oldboyedu59 ~]# date +%N|md5sum |head -c8
888b0ea8[root@oldboyedu59 ~]#
方法3 RANDOM环境变量
RANDOM生成随机数字
echo $RANDOM
RANDOM + md5sum
RANDOM +数字
[root@oldboyedu59 ~]# echo $((RANDOM+10000000))
10017665
方法4 uuidgen
[root@oldboyedu59 ~]# uuidgen
dabd39c6-db3d-4371-9470-266bfb13bcc8
[root@oldboy59 ~]# uuidgen|cut -b 1-8
5f6b9ca5
方法5mkpasswd
mkpasswd|cut -b 1-8
方法6 openssl
openssl rand -base64 8
[root@oldboyedu59 ~]# openssl rand -base64 8
XnVoCkFJ0f4=
方法7
string字符转换 转换为人类可读
strings /dev/urandom | tr -cd 'a-zA-Z0-9' |head -c8
方法8 gpg
[root@oldboyedu59 ~]# gpg --gen-random -armor 1 12
gGdc3IQH5WFjvLXL
网友评论