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


实现脚本:

#!/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


脚本执行效果:

[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

账号成功创建。然后看一下密码文件:

[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位字符).


基于上面脚本略作修改:

#!/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


执行效果:

[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


上面少了随机的过程:

批量删除刚才创建的用户:

#!/bin/bash
#del_user.sh
for i in `seq -w 10`
do
    userdel -r oldboy$i
done

随机生成密码的脚本:

#!/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