博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Shell脚本批量创建用户并随机生成密码
阅读量:7166 次
发布时间:2019-06-29

本文共 5090 字,大约阅读时间需要 16 分钟。

要求:批量创建10个系统账号oldboy01-oldboy10,并设置生成密码(密码不同).

实现脚本:

1
2
3
4
5
6
7
#!/bin/bash
#Question3
for 
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
[root@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
文件:
[root@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
[root@localhost q4]
# ls
passwd
.txt  q4.sh
[root@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 
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
[root@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,所以是追加而不是覆盖
[root@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 
in 
`
seq 
-w 10`
do
    
userdel -r oldboy$i
done

随机生成密码的脚本:

1
2
3
4
5
6
7
#!/bin/bash
#Question4
for 
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

 

     本文转自marbury 51CTO博客,原文链接:http://blog.51cto.com/magic3/1431581,如需转载请自行联系原作者

你可能感兴趣的文章
JVM内存区域
查看>>
DNS的视图功能的简单配置。
查看>>
linux和windows互传文件/用户配置文件和密码配置文件/用户组管理/用户管理
查看>>
通过javascript把图片转化为字符画
查看>>
OpenJPA 一些难搞的查询
查看>>
设置button的样式,使得按钮的图片在上面,文字在图片的下面
查看>>
MySQL之函数、存储过程和触发器
查看>>
完整版的OpenLDAP搭建全过程
查看>>
java反射学习总结
查看>>
104. ftl 小数位处理
查看>>
Cannot open /usr/local/varnish/var/varnish/test.localdomain/_.vsm: No such file or directory
查看>>
我的VIM -- vimrc配置
查看>>
Tengine ngx_http_upstream_check_module 健康功能检测使用
查看>>
将数组A中的内容和数组B中的内容进行交换。(数组一样大)
查看>>
Python35 events(事件)、队列
查看>>
[你必须知道的异步编程]——异步编程模型(APM)
查看>>
PHP substr()截取字符串时,中文出现乱码的问题解决
查看>>
吴治辉:分布式数据库MyCAT之前世今生
查看>>
html段落的控制
查看>>
我的友情链接
查看>>