创建100个用户并生成12个字符的随机密码
要求
创建100个用户,每个用户分配一个12个字符的随机密码,密码包括数字和大小写字母
利用/dev/urandom生成随机数
/dev/urandom是系统中一个特殊的字符文件,内容是无穷无尽的随机字符
[07:28:05 root@C8-3-55 ~]#ll /dev/urandom
crw-rw-rw-. 1 root root 1, 9 3月 6 07:26 /dev/urandom
[07:28:09 root@C8-3-55 ~]#less /dev/urandom
/dev/urandom is not a regular file (use -f to see it)
[07:28:39 root@C8-3-55 ~]#less -f /dev/urandom
[07:29:12 root@C8-3-55 ~]#
[07:29:12 root@C8-3-55 ~]#cat -A /dev/urandom
/^?M--hM-XM-gM-sM-HM-[M-`^M^\<2XSM-DM-beM-^@kM-T^IM-IM-^^^@BM-TbM-^CM-^W^VM-Q/M-^LM-^HTaM-X]M-|U@4^MSjWEqM-uM-^M^RM-^MM-RM-^TVM-"M-#M-s^YM-X^EM-q^FOhM-|-%dM-0M-^TM-zvM-#M-9M-gM-]M-^@:(-M-O uM-JM-@M-R@~#EM-N^N>M-^AM-,M-w,eM-.M-^EM-f!yM-"M-ZM-^EM-(M-^HKz^AnM-^W^GM-%M-^M*M-^NM-H^?M-U=M-DM-bM-9qvM-bM-^FM-EM-[09L:M-^R{>M-IM-1^VGM-OiM-lM-8M-"M-^CM-,M-^QM-=vM-^FM-^R^]M-^AM-^AM-^[M-b^TM-LM-D$
M-EM-^Ri^Ez^M-onM-^IM-?lM-1@acM-\ba^?^@^AM-yM-+M-J!M-^X'M-5M-^UM-^U$
M-UmM-GM-f$
利用tr命令筛选处数字和字母
tr 命令用于转换或删除文件中的字符。 tr 指令从标准输入设备读取数据,经过字符串转译后,将结果输出到标准输出设备。
tr(选项)(参数) 选项
- -c或——complerment:取代所有不属于第一字符集的字符;
- -d或——delete:删除所有属于第一字符集的字符;
[07:29:36 root@C8-3-55 ~]#cat /dev/urandom | tr -dc '[:alnum:]' | head -c 12
Uei0Vv43XfR1
反选设定字符。也就是符合 SET1 的部份不做处理,不符合的剩余部份才进行转换
也就是说,从cat到的urandom文件中对不属于[:alnum:]的字符进行-d删除处理,
剩下的字符取head -c 12 就行了
利用for循环 生成100个密码并输出到PWD.txt文件
[07:54:04 root@C8-3-55 ~]#for i in {1..100}; do echo PWD$i=`cat /dev/urandom | tr -dc '[:alnum:]' | head -c 12` >> PWD.txt;done
[07:54:26 root@C8-3-55 ~]#cat -n PWD.txt
1 PWD100=DOq15KGPpP4I
2 PWD1=cMhB5843tBpT
3 PWD2=moe0J5WLVoD1
4 PWD3=UmNHWkeGJasc
5 PWD4=YXx4dpJBReYU
6 PWD5=URepCseiUk4B
7 PWD6=2l0rYKVFOVJ5
8 PWD7=Fz1BKogoWs1O
9 PWD8=362I0X2XroLI
10 PWD9=UOM3tFPvJKOP
11 PWD10=rZwNngkgdenp
12 PWD11=klprUdThyo31
13 PWD12=EY2FmcwVu4ED
14 PWD13=srG62LPI3pVI
15 PWD14=LJqjh5DleBsn
16 PWD15=QwguJUl0p8XS
17 PWD16=RPxXzc1mFJoC
18 PWD17=ChhjmoWjkbf9
19 PWD18=sEvNVca4LFXN
20 PWD19=qkX4wthxEG4C
21 PWD20=qv3KSAeouHwD
22 PWD21=94EjCC4OYvYX
23 PWD22=E0s7QiCBn2z4
24 PWD23=Hx50g0JW5wf2
25 PWD24=BmwlNQN59mk8
26 PWD25=yHAZCVpTiBRs
27 PWD26=xMnz9Sbcj7WW
28 PWD27=zCoL2EgVIalV
29 PWD28=ixBzUG2Xa5e8
30 PWD29=KFOOfWBhZVzo
31 PWD30=D78vyvaYHbE8
32 PWD31=OjxMqZ8NLeWy
33 PWD32=AljxoUFBDllL
34 PWD33=G8rqqz4mjJJa
35 PWD34=hVLTZKqL6SKa
36 PWD35=0B7PJbOwvxQP
37 PWD36=JUiZDGXAc01T
38 PWD37=lIkVCAOMtFkX
39 PWD38=wqQKztnDIqrm
40 PWD39=2ihtwWBODMsj
41 PWD40=2o1peyJUD8Um
42 PWD41=2SPv6K1vW8nC
43 PWD42=8cHxlA3Em3Yi
44 PWD43=NOqlvolSmT2T
45 PWD44=HjQ27GticDHc
46 PWD45=cjVU0khkgRAk
47 PWD46=eS9KMmcOdUCY
48 PWD47=vf3imqNpgkbX
49 PWD48=QLgXCNpg06Gt
50 PWD49=btGi5zKr5VLp
51 PWD50=EJpLx722h0tp
52 PWD51=6G0pObiBFO9Q
53 PWD52=veBSywJ2dXwd
54 PWD53=gy3NFZu9KgnS
55 PWD54=0Ka2S587zNEM
56 PWD55=QRffSKW3lHV0
57 PWD56=JFUUqRnrSI5Z
58 PWD57=PIwTBgS95f3r
59 PWD58=zs1AAKRhDCb2
60 PWD59=mBAMhbglbrgw
61 PWD60=8aZV3HYcCF4x
62 PWD61=j7nmAeodGjXX
63 PWD62=iToOy6XEoJva
64 PWD63=bHJQMdZTGSwb
65 PWD64=1eCvgEU93FrR
66 PWD65=fZZPLELkE98V
67 PWD66=229mXlqFPzk3
68 PWD67=HD0eFcBQBYYi
69 PWD68=nahVJSgxfecL
70 PWD69=8dvZZj7Py11D
71 PWD70=WuYjPZPuh1pv
72 PWD71=6Gg4Tw53hqvU
73 PWD72=TfLMBrPNI1oO
74 PWD73=JRfBTJ7LctSr
75 PWD74=WrcOjYwuZwU1
76 PWD75=Xm4Y0XC7KdoD
77 PWD76=34CiCPwuGM5o
78 PWD77=732v5E8LjXM0
79 PWD78=eOxNnHTHEx2m
80 PWD79=d34AO1CMGYjp
81 PWD80=eflejVHmmbU9
82 PWD81=fnX5TchU8Nft
83 PWD82=gJuG8V4z2bFd
84 PWD83=47ek8Z96zgwI
85 PWD84=u0tdB6mZXKLi
86 PWD85=OLYD5OlK0jlk
87 PWD86=EmozfCz2La7b
88 PWD87=1m5WJHqvvh95
89 PWD88=lcEAh2EK3JYE
90 PWD89=NEakcrYSLfEC
91 PWD90=nYx0HWOgD1L8
92 PWD91=srT7gucwL9dl
93 PWD92=o9fYRVQ8usYy
94 PWD93=kZ7Greaac9iy
95 PWD94=vKly4CjVSzoy
96 PWD95=71y8Y20TgeqB
97 PWD96=5XqZyIdycbn9
98 PWD97=OyhH4AbAX04A
99 PWD98=UJimAumjE70o
100 PWD99=tJ1t81EHTYgp
101 PWD100=pXYroFB4v6Uw
实例:生成100随机密码用户
批量生成100个用户,同时生成100个随机密码,并将100个随机密码付给用户,同时输出文本
1 #!/bin/bash
2 #
3 #***************************
4 #Author:BaiPZ
5 #Date:2021-03-06
6 #URL:https://www.cnblogs.com/bpzblog/
7 #***************************
8 #Start
9 for i in {1..100};do
10 useradd user$i
11 PWD=`cat /dev/urandom | tr -dc '[:alnum:]' | head -c 12`
12 echo $PWD | passwd --stdin user$1 &> /dev/null
13 echo user$i:$PWD >> /home/userAdd100.log
14 echo "hahaha,i creat $i user by coding !!!"
15 done
查看创建成功
[08:09:27 root@C8-3-55 ~]#tail -100 /etc/passwd
user1:x:8892:8892::/home/user1:/bin/bash
user2:x:8892:8892::/home/user2:/bin/bash
user3:x:8893:8893::/home/user3:/bin/bash
user4:x:8894:8894::/home/user4:/bin/bash
user5:x:8895:8895::/home/user5:/bin/bash
user6:x:8896:8896::/home/user6:/bin/bash
user7:x:8897:8897::/home/user7:/bin/bash
user8:x:8898:8898::/home/user8:/bin/bash
user9:x:8899:8899::/home/user9:/bin/bash
user10:x:8900:8900::/home/user10:/bin/bash
user11:x:8901:8901::/home/user11:/bin/bash
user12:x:8902:8902::/home/user12:/bin/bash
user13:x:8903:8903::/home/user13:/bin/bash
user14:x:8904:8904::/home/user14:/bin/bash
user15:x:8905:8905::/home/user15:/bin/bash
user16:x:8906:8906::/home/user16:/bin/bash
user17:x:8907:8907::/home/user17:/bin/bash
user18:x:8908:8908::/home/user18:/bin/bash
user19:x:8909:8909::/home/user19:/bin/bash
user20:x:8910:8910::/home/user20:/bin/bash
user21:x:8911:8911::/home/user21:/bin/bash
user22:x:8912:8912::/home/user22:/bin/bash
user23:x:8913:8913::/home/user23:/bin/bash
user24:x:8914:8914::/home/user24:/bin/bash
user25:x:8915:8915::/home/user25:/bin/bash
user26:x:8916:8916::/home/user26:/bin/bash
user27:x:8917:8917::/home/user27:/bin/bash
user28:x:8918:8918::/home/user28:/bin/bash
user29:x:8919:8919::/home/user29:/bin/bash
user30:x:8920:8920::/home/user30:/bin/bash
user31:x:8921:8921::/home/user31:/bin/bash
user32:x:8922:8922::/home/user32:/bin/bash
user33:x:8923:8923::/home/user33:/bin/bash
user34:x:8924:8924::/home/user34:/bin/bash
user35:x:8925:8925::/home/user35:/bin/bash
user36:x:8926:8926::/home/user36:/bin/bash
user37:x:8927:8927::/home/user37:/bin/bash
user38:x:8928:8928::/home/user38:/bin/bash
user39:x:8929:8929::/home/user39:/bin/bash
user40:x:8930:8930::/home/user40:/bin/bash
user41:x:8931:8931::/home/user41:/bin/bash
user42:x:8932:8932::/home/user42:/bin/bash
user43:x:8933:8933::/home/user43:/bin/bash
user44:x:8934:8934::/home/user44:/bin/bash
user45:x:8935:8935::/home/user45:/bin/bash
user46:x:8936:8936::/home/user46:/bin/bash
user47:x:8937:8937::/home/user47:/bin/bash
user48:x:8938:8938::/home/user48:/bin/bash
user49:x:8939:8939::/home/user49:/bin/bash
user50:x:8940:8940::/home/user50:/bin/bash
user51:x:8941:8941::/home/user51:/bin/bash
user52:x:8942:8942::/home/user52:/bin/bash
user53:x:8943:8943::/home/user53:/bin/bash
user54:x:8944:8944::/home/user54:/bin/bash
user55:x:8945:8945::/home/user55:/bin/bash
user56:x:8946:8946::/home/user56:/bin/bash
user57:x:8947:8947::/home/user57:/bin/bash
user58:x:8948:8948::/home/user58:/bin/bash
user59:x:8949:8949::/home/user59:/bin/bash
user60:x:8950:8950::/home/user60:/bin/bash
user61:x:8951:8951::/home/user61:/bin/bash
user62:x:8952:8952::/home/user62:/bin/bash
user63:x:8953:8953::/home/user63:/bin/bash
user64:x:8954:8954::/home/user64:/bin/bash
user65:x:8955:8955::/home/user65:/bin/bash
user66:x:8956:8956::/home/user66:/bin/bash
user67:x:8957:8957::/home/user67:/bin/bash
user68:x:8958:8958::/home/user68:/bin/bash
user69:x:8959:8959::/home/user69:/bin/bash
user70:x:8960:8960::/home/user70:/bin/bash
user71:x:8961:8961::/home/user71:/bin/bash
user72:x:8962:8962::/home/user72:/bin/bash
user73:x:8963:8963::/home/user73:/bin/bash
user74:x:8964:8964::/home/user74:/bin/bash
user75:x:8965:8965::/home/user75:/bin/bash
user76:x:8966:8966::/home/user76:/bin/bash
user77:x:8967:8967::/home/user77:/bin/bash
user78:x:8968:8968::/home/user78:/bin/bash
user79:x:8969:8969::/home/user79:/bin/bash
user80:x:8970:8970::/home/user80:/bin/bash
user81:x:8971:8971::/home/user81:/bin/bash
user82:x:8972:8972::/home/user82:/bin/bash
user83:x:8973:8973::/home/user83:/bin/bash
user84:x:8974:8974::/home/user84:/bin/bash
user85:x:8975:8975::/home/user85:/bin/bash
user86:x:8976:8976::/home/user86:/bin/bash
user87:x:8977:8977::/home/user87:/bin/bash
user88:x:8978:8978::/home/user88:/bin/bash
user89:x:8979:8979::/home/user89:/bin/bash
user90:x:8980:8980::/home/user90:/bin/bash
user91:x:8981:8981::/home/user91:/bin/bash
user92:x:8982:8982::/home/user92:/bin/bash
user93:x:8983:8983::/home/user93:/bin/bash
user94:x:8984:8984::/home/user94:/bin/bash
user95:x:8985:8985::/home/user95:/bin/bash
user96:x:8986:8986::/home/user96:/bin/bash
user97:x:8987:8987::/home/user97:/bin/bash
user98:x:8988:8988::/home/user98:/bin/bash
user99:x:8989:8989::/home/user99:/bin/bash
user100:x:8990:8990::/home/user100:/bin/bash
* * *
胖并快乐着的死肥宅
* * *