1、批量创建10个系统账号oldboy01-oldboy10并设置密码(密码不能相同)
1 [[email protected] tmp]# cat 15-05-create_user_passwd.sh 2 #!/bin/sh 3 for n in $(seq -w 10) 4 do 5 useradd oldboy-$n 6 #密码就是最后一次 7 echo "$n"|passwd --stdin oldboy-$n 8 done
执行结果:
1 [[email protected] tmp]# sh 15-05-create_user_passwd.sh 2 Changing password for user oldboy-01. 3 passwd: all authentication tokens updated successfully. 4 Changing password for user oldboy-02. 5 passwd: all authentication tokens updated successfully. 6 Changing password for user oldboy-03. 7 passwd: all authentication tokens updated successfully. 8 Changing password for user oldboy-04. 9 passwd: all authentication tokens updated successfully. 10 Changing password for user oldboy-05. 11 passwd: all authentication tokens updated successfully. 12 Changing password for user oldboy-06. 13 passwd: all authentication tokens updated successfully. 14 Changing password for user oldboy-07. 15 passwd: all authentication tokens updated successfully. 16 Changing password for user oldboy-08. 17 passwd: all authentication tokens updated successfully. 18 Changing password for user oldboy-09. 19 passwd: all authentication tokens updated successfully. 20 Changing password for user oldboy-10. 21 passwd: all authentication tokens updated successfully. 22 23 [[email protected] tmp]# su - oldboy-01 24 25 [[email protected] ~]$ su - oldboy-10 26 口令:10
2、批量创建10个系统账号oldboy1-oldboy10 并设置密码(密码为随机8位字符串)
先补充点生成随机数的知识
Linux 随机数
[[email protected] tmp]# echo $RANDOM
15598
[[email protected] tmp]# echo $(date +%t%N)
028885282
[[email protected] tmp]# echo $(date +%t%N)$RANDOM
06024296622562
[[email protected] tmp]# echo $(date +%t%N)$RANDOM|md5sum
56a71b3a6ae0880ef4ea2a03c742353e -
取随机数
[[email protected] tmp]# echo $(date +%t%N)$RANDOM|md5sum|cut -c 2-9
e46f36ca
[[email protected] tmp]# echo $(date +%t%N)$RANDOM|md5sum|cut -c 2-9
716d731a
[[email protected] tmp]# echo $(date +%t%N)$RANDOM|md5sum|cut -c 2-9
d4c8a8bf
脚本如下:
1 [[email protected] tmp]# cat adduser.sh 2 #!/bin/sh 3 . /etc/init.d/functions 4 >/tmp/user.txt 5 6 for n in $(seq -w 10) 7 do 8 passwd=`echo $(date +%t%N)$RANDOM|md5sum|cut -c 2-9` 9 useradd oldboy-$n >&/dev/null && user_status=$? 10 echo "$passwd"|passwd --stdin oldboy-$n >&/dev/null && pass_status=$? 11 12 if [ $user_status -eq 0 -a $pass_status -eq 0 ];then 13 action "adduser oldboy-$n" /bin/true 14 echo -e "user:\toldboy-$n pass:\t$passwd" >>/tmp/user.txt 15 else 16 action "adduser oldboy-$n" /bin/false 17 echo -e "user:\toldboy-$n pass:\t$passwd" >>/tmp/fail_user.txt 18 fi 19 20 done
执行结果:
1 [[email protected] tmp]# sh adduser.sh 2 adduser oldboy-01 [ OK ] 3 adduser oldboy-02 [ OK ] 4 adduser oldboy-03 [ OK ] 5 adduser oldboy-04 [ OK ] 6 adduser oldboy-05 [ OK ] 7 adduser oldboy-06 [ OK ] 8 adduser oldboy-07 [ OK ] 9 adduser oldboy-08 [ OK ] 10 adduser oldboy-09 [ OK ] 11 adduser oldboy-10 [ OK ] 12 13 检查创建用户日志,发现创建成功 14 [[email protected] tmp]# cat user.txt 15 user: oldboy-01 pass: dd0b275b 16 user: oldboy-02 pass: 968a2c8f 17 user: oldboy-03 pass: 02b270cb 18 user: oldboy-04 pass: 905deeee 19 user: oldboy-05 pass: 138e6cf1 20 user: oldboy-06 pass: 4be5f9a4 21 user: oldboy-07 pass: e0bb693e 22 user: oldboy-08 pass: 97fa2caa 23 user: oldboy-09 pass: a08bff7d 24 user: oldboy-10 pass: e4f4d39f 25 26 检查创建错误日志,发现为空 27 [[email protected] tmp]# cat fail_user.txt 28 29 重复执行,要先删除原来用户,执行下面命令,可以批量删除 30 [[email protected] tmp]# for n in $(seq -w 10);do userdel -r oldboy-$n;done 31 32 重复执行一次,又会生成不一样的密码。 33 [[email protected] tmp]# sh test.sh 34 adduser oldboy-01 [ OK ] 35 adduser oldboy-02 [ OK ] 36 adduser oldboy-03 [ OK ] 37 adduser oldboy-04 [ OK ] 38 adduser oldboy-05 [ OK ] 39 adduser oldboy-06 [ OK ] 40 adduser oldboy-07 [ OK ] 41 adduser oldboy-08 [ OK ] 42 adduser oldboy-09 [ OK ] 43 adduser oldboy-10 [ OK ] 44 45 检查结果,发现密码变了。 46 [[email protected] tmp]# cat user.txt 47 user: oldboy-01 pass: 7152783a 48 user: oldboy-02 pass: 5f714e8a 49 user: oldboy-03 pass: 68f85e07 50 user: oldboy-04 pass: d21fa12d 51 user: oldboy-05 pass: 35cd16ed 52 user: oldboy-06 pass: 8760591b 53 user: oldboy-07 pass: 497bff6b 54 user: oldboy-08 pass: d8266b5d 55 user: oldboy-09 pass: 27404ce2 56 user: oldboy-10 pass: cc35251a
时间: 2024-10-11 18:21:48