批量添加用户并设置8位随机密码
化简为 添加3个用户设置固定密码
useradd stu01 ;echo 123456|passwd --stdin stu01
useradd stu02 ;echo 123456|passwd --stdin stu02
useradd stu03 ;echo 123456|passwd --stdin stu03
方法1
[root@oldboyedu59 ~]# echo stu{01..3}|xargs -n1
stu01
stu02
stu03
其他方法待添加
利用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
sed中&含义
& sed命令中s###g 表示前面正则匹配到的内容
[root@oldboyedu59 ~]# echo {1..10}
1 2 3 4 5 6 7 8 9 10
[root@oldboyedu59 ~]# echo {1..10}|sed -r 's#([0-9]+)#<\1>#g'
<1> <2> <3> <4> <5> <6> <7> <8> <9> <10>
[root@oldboyedu59 ~]# echo {1..10}|sed -r 's#[0-9]+#<&>#g'
<1> <2> <3> <4> <5> <6> <7> <8> <9> <10>
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.
化简为 添加3个用户设置固定密码
方法1
[root@oldboyedu59 ~]# echo stu{01..3}|xargs -n1
stu01
stu02
stu03
其他方法待添加
[root@oldboyedu59 ~]# tr -cd 'a-zA-Z0-9' B0iV36fZ[root@oldboyedu59 ~]#
[root@oldboyedu59 ~]# date +%N
211379317
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
echo stu{04..6}|xargs -n1 |sed 's#.*#
useradd &;
p=p|passwd --stdin &;
echo $p & >>/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'
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 ~]$
[root@oldboyedu59 ~]# echo stu{04..6}|xargs -n1 |sed 's#.#useradd &;p=p|passwd --stdin &;echo (date +%N);echo p stu04 >>/p.txt
useradd stu05;p=p|passwd --stdin stu05;echo (date +%N);echo p stu06 >>/p.txt
[root@oldboyedu59 ~]# echo stu{04..6}|xargs -n1 |sed 's#.#useradd &;p=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
[root@oldboyedu59 ~]# cat /p.txt
209574583 stu05
570581626 stu04
600781084 stu05
660376548 stu06
[root@oldboyedu59 ~]#
[root@oldboyedu59 ~]# su - oldboy
Last login: Thu Apr 25 12:03:45 CST 2019 on pts/0
[oldboy@oldboyedu59 ~] su - stu05
Password:
Last login: Thu Apr 25 12:04:01 CST 2019 on pts/0
[stu05@oldboyedu59 ~] logout
小结:
1.理解命令拼接:sed/awk 形式命令 最后交给bash执行
2.sed &
[故障集合]Linux必备故障及原因50个
https://www.jianshu.com/p/99ec5bb4183f
Linux下面生成随机密码方法
[root@oldboyedu59 ~]# tr -cd 'a-zA-Z0-9' B0iV36fZ[root@oldboyedu59 ~]#
[root@oldboyedu59 ~]# date +%N
211379317
date +%N |md5sum |head -c10
[root@oldboyedu59 ~]# date +%N|md5sum |head -c8
888b0ea8[root@oldboyedu59 ~]#
RANDOM生成随机数字
echo $RANDOM
RANDOM + md5sum
RANDOM +数字
[root@oldboyedu59 ~]# echo $((RANDOM+10000000))
10017665
[root@oldboyedu59 ~]# uuidgen
dabd39c6-db3d-4371-9470-266bfb13bcc8
[root@oldboy59 ~]# uuidgen|cut -b 1-8
5f6b9ca5
mkpasswd|cut -b 1-8
openssl rand -base64 8
[root@oldboyedu59 ~]# openssl rand -base64 8
XnVoCkFJ0f4=
string字符转换 转换为人类可读
strings /dev/urandom | tr -cd 'a-zA-Z0-9' |head -c8
[root@oldboyedu59 ~]# gpg --gen-random -armor 1 12
gGdc3IQH5WFjvLXL