命令拼接的应用
批量添加用户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