day22-批量生成用户并设置密码

9469008-cea7e1b2417dd03b.png
用户管理.png

命令拼接的应用

批量添加用户stu01 stu02 stu03并设置密码为123456

最终目标:

useradd stu01 ;echo 123456|passwd --stdin stu01
useradd stu01 ;echo 123456|passwd --stdin stu01
useradd stu01 ;echo 123456|passwd --stdin stu01

解决

先拼接出创建用户的命令

[root@oldboy ~]# echo stu{01..3}|xargs -n1|sed -r 's#.*#useradd &#g'    
useradd stu01
useradd stu02            
useradd stu03

利用sed再拼接出无交互式设置密码

[root@oldboy ~]# echo stu{01..3}|xargs -n1|sed -r 's#.*#useradd & ;echo 123456|passwd --stdin &#g'     
useradd stu01 ;echo 123456|passwd --stdin stu01
useradd stu02 ;echo 123456|passwd --stdin stu02   
useradd stu03 ;echo 123456|passwd --stdin stu03

利用awk拼接出无交互式设置密码

echo oldboy{1..4}|xargs -n1|awk  '{print useradd,$0";echo 123456|passwd --stdin",$0}'        ##》利用awk拼接
useradd oldboy1;echo 123456|passwd --stdin stu01
useradd oldboy2;echo 123456|passwd --stdin stu01  
useradd oldboy3;echo 123456|passwd --stdin stu01
useradd oldboy4;echo 123456|passwd --stdin stu01

交给bash运行,先拿一个测试

[root@oldboy ~]# useradd stu04;echo 123456|passwd --stdin stu04
Changing password for user stu04.
passwd: all authentication tokens updated successfully.

测试命令没问题再把整条命令交给bash处理

[root@oldboy ~]# echo oldboy{1..4}|xargs -n1|sed 's#.*#useradd &;echo 123456|passwd --stdin &#g'|bash
Changing password for user stu01.
passwd: all authentication tokens updated successfully.
Changing password for user stu02.
passwd: all authentication tokens updated successfully.
Changing password for user stu03.
passwd: all authentication tokens updated successfully.
useradd: user 'stu04' already exists

在sed命令里&表示前面两个井号中间匹配到的字符

随机生成密码命令

[root@oldboy ~]# tr -cd 'a-zA-Z0-9' </dev/urandom|head -c8
u65geQEg[root@oldboy ~]# tr -cd 'a-zA-Z0-9' </dev/urandom|head -c8
jhGspl6g[root@oldboy ~]# tr -cd 'a-zA-Z0-9' </dev/urandom|head -c8
AfoiUzfK[root@oldboy ~]# tr -cd 'a-zA-Z0-9' </dev/urandom|head -c8
yQHIaHyY[root@oldboy ~]# tr -cd 'a-zA-Z0-9' </dev/urandom|head -c8
3Vk8KDIq[root@oldboy ~]# tr -cd 'a-zA-Z0-9' </dev/urandom|head -c8
WWfpEdSm[root@oldboy ~]# tr -cd 'a-zA-Z0-9' </dev/urandom|head -c8
5YcAQTog[root@oldboy ~]# tr -cd 'a-zA-Z0-9' </dev/urandom|head -c8

tr -cd 'a-zA-Z0-9' 表示对文件里的字符只保留这个范围里的。其他的都删掉。head -c8 表示只取前8个字符。

时间加指纹生成

[root@oldboy ~]# date +%N|md5sum|head -c8
255f7d51[root@oldboy ~]# date +%N|md5sum|head -c8
6c4023a4[root@oldboy ~]# date +%N|md5sum|head -c8
f71e7780[root@oldboy ~]# date +%N|md5sum|head -c8
fde18ec7[root@oldboy ~]# date +%N|md5sum|head -c8

时间加sha系列生成

[root@oldboy ~]# date +%N|sha512sum|head -c8
d545c06c[root@oldboy ~]# date +%N|sha512sum|head -c8
98524cfc[root@oldboy ~]# date +%N|sha512sum|head -c8
a0494aab[root@oldboy ~]# date +%N|sha512sum|head -c8
8949cc82[root@oldboy ~]# 

生成随机uid的命令生成

[root@oldboy ~]# uuidgen
df4ba7c2-8f6e-4781-80fc-d3511e776e80
[root@oldboy ~]# uuidgen|head -c8
97838e4b[root@oldboy ~]# uuidgen|head -c8
4b51a0a7[root@oldboy ~]# uuidgen|head -c8
e8ece8cb[root@oldboy ~]# uuidgen|head -c8
d6cf4f69[root@oldboy ~]# 
[root@oldboy ~]# openssl rand -base64 8
02u8kHV0zK0=
[root@oldboy ~]# openssl rand -base64 8
bxnpMJV7asM=
[root@oldboy ~]# openssl rand -base64 8
BfflDuGqgQU=
[root@oldboy ~]# openssl rand -base64 8
V+9bL3iP/E8=
[root@oldboy ~]# openssl rand -base64 8
scTNJwEJkgw=

批量添加用户stu04 stu05 stu06并设置密码为随机密码

想要结果

useradd stu04;p=`date +%N`;echo $p|passwd --stdin stu04;echo $p stu04 >>20.txt
useradd stu05;p=`date +%N`;echo $p|passwd --stdin stu05;echo $p stu05 >>20.txt
useradd stu06;p=`date +%N`;echo $p|passwd --stdin stu06;echo $p stu06 >>20.tx

命令拼接

[root@oldboy ~]# echo stu{04..6}|xargs -n1|sed -r 's#(.*)#useradd \1;p=`date +%N`
useradd stu04;p=`date +%N`;echo $p|passwd --stdin stu04;echo $p stu04 >>20.txt
useradd stu05;p=`date +%N`;echo $p|passwd --stdin stu05;echo $p stu05 >>20.txt
useradd stu06;p=`date +%N`;echo $p|passwd --stdin stu06;echo $p stu06 >>20.txt

先测试一条。

[root@oldboy ~]# useradd old5;p=`date +%N`;echo $p|passwd --stdin old5;echo $p old5 >>20.txt|bash
Changing password for user old5.
passwd: all authentication tokens updated successfully.
[root@oldboy ~]# cat 20.txt
902265571 stu05
828118399 old5

全部交给bash处理

[root@oldboy ~]# echo old{1..5}|xargs -n1|sed 's#.*#useradd &;p=`date +%N`;echo $p|passwd --stdin &;echo $p & >>20.txt#g' |bash
Changing password for user old1.
passwd: all authentication tokens updated successfully.
Changing password for user old2.
passwd: all authentication tokens updated successfully.
Changing password for user old3.
passwd: all authentication tokens updated successfully.
Changing password for user old4.
passwd: all authentication tokens updated successfully.
useradd: user 'old5' already exists
Changing password for user old5.
passwd: all authentication tokens updated successfully.
[root@oldboy ~]# cat 20.txt
902265571 stu05
828118399 old5
002657812 old1
172031442 old2
246596527 old3
314187391 old4
361565527 old5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值