要求:批量创建10个系统账号oldboy01-oldboy10,并设置生成密码(密码不同).
实现脚本:
1
2
3
4
5
6
7
#!/bin/bash
#Question3
for i in $(seq -w 10)
do
useradd -s /bin/bash oldboy$i
echo "password$i" | md5sum | tee -a passwd.txt | passwd --stdin oldboy$i
done
脚本执行效果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[iyunv@localhost q4]# sh q4.sh
Changing password for user oldboy01.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy02.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy03.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy04.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy05.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy06.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy07.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy08.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy09.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy10.
passwd: all authentication tokens updated successfully.
查看passwd文件:
[iyunv@localhost q4]# tail -n 10 /etc/passwd
oldboy01:x:501:501::/home/oldboy01:/bin/bash
oldboy02:x:502:502::/home/oldboy02:/bin/bash
oldboy03:x:503:503::/home/oldboy03:/bin/bash
oldboy04:x:504:504::/home/oldboy04:/bin/bash
oldboy05:x:505:505::/home/oldboy05:/bin/bash
oldboy06:x:506:506::/home/oldboy06:/bin/bash
oldboy07:x:507:507::/home/oldboy07:/bin/bash
oldboy08:x:508:508::/home/oldboy08:/bin/bash
oldboy09:x:509:509::/home/oldboy09:/bin/bash
oldboy10:x:510:510::/home/oldboy10:/bin/bash
账号成功创建。然后看一下密码文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[iyunv@localhost q4]# ls
passwd.txt q4.sh
[iyunv@localhost q4]# cat passwd.txt
#MD5机密后的密码
10b222970537b97919db36ec757370d2 -
f1f16683f3e0208131b46d37a79c8921 -
32a3571fa12b39266a58d42234836839 -
10b222970537b97919db36ec757370d2 -
f1f16683f3e0208131b46d37a79c8921 -
32a3571fa12b39266a58d42234836839 -
978a60e3ebbb2dd56aa5f821d7f42a2b -
cdffb1b0971ab2a3befc18c43d3ca5be -
4679834a98e77dd2cec854b51de6d886 -
ad8e8436435f93f6a7b295418889edff -
f46c891cad3974fc64b7133911404c2a -
ebde6449998d7c84ccb23725ecac782b -
6c60d92e5b9757ce16c8e238174a6cac -
5147eebead1aee9cbe761ec89b1101f1 -
16a383252c3a264b3d977592ce8cf8ee -
446174d0862f8f830ef4c54bdcf73d82 -
166335a86348834ffd4d79cd5a73867b -
要求2:批量创建10个系统账号oldboy01-oldboy10,并随机设置密码(密码为8位字符).
基于上面脚本略作修改:
1
2
3
4
5
6
7
#!/bin/bash
#Question4
for i in $(seq -w 10)
do
useradd -s /bin/bash oldboy$i
echo "password$i" | md5sum |cut -c-8 | tee -a passwd.txt | passwd --stdin oldboy$i
done
执行效果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
[iyunv@localhost q4]# sh q4.sh
Changing password for user oldboy01.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy02.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy03.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy04.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy05.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy06.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy07.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy08.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy09.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy10.
passwd: all authentication tokens updated successfully.
#看一下密码文件
#tee命令中由于使用-a,所以是追加而不是覆盖
[iyunv@localhost q4]# cat passwd.txt
10b222970537b97919db36ec757370d2 -
f1f16683f3e0208131b46d37a79c8921 -
32a3571fa12b39266a58d42234836839 -
10b222970537b97919db36ec757370d2 -
f1f16683f3e0208131b46d37a79c8921 -
32a3571fa12b39266a58d42234836839 -
978a60e3ebbb2dd56aa5f821d7f42a2b -
cdffb1b0971ab2a3befc18c43d3ca5be -
4679834a98e77dd2cec854b51de6d886 -
ad8e8436435f93f6a7b295418889edff -
f46c891cad3974fc64b7133911404c2a -
ebde6449998d7c84ccb23725ecac782b -
6c60d92e5b9757ce16c8e238174a6cac -
5147eebead1aee9cbe761ec89b1101f1 -
16a383252c3a264b3d977592ce8cf8ee -
446174d0862f8f830ef4c54bdcf73d82 -
166335a86348834ffd4d79cd5a73867b -
cdffb1b0
4679834a
ad8e8436
f46c891c
ebde6449
6c60d92e
5147eebe
16a38325
446174d0
166335a8
上面少了随机的过程:
批量删除刚才创建的用户:
1
2
3
4
5
6
#!/bin/bash
#del_user.sh
for i in `seq -w 10`
do
userdel -r oldboy$i
done
随机生成密码的脚本:
1
2
3
4
5
6
7
#!/bin/bash
#Question4
for i in $(seq -w 10)
do
useradd -s /bin/bash oldboy$i
echo "$RANDOM" | md5sum |cut -c-8 | tee passwd.txt | passwd --stdin oldboy$i
done
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com