批量添加用户并设置随机密码,把添加的用户的名字和密码保存到文件中。
[root@lamp scripts]# cat user.sh #!/bin/sh for i in `seq -w 10` do pass=$(echo "`date`$RANDOM"|md5sum|cut -c 1-8) //取随机数的方法是date结果和random随机数拼接并计算md5值取前8位 useradd kg$i echo "$pass"|passwd --stdin kg$i echo -e "kg$i $pass">>/tmp/pass.txt done [root@lamp scripts]# seq -w 10 01 02 03 04 05 06 07 08 09 10 [root@lamp scripts]# sh user.sh Changing password for user kg01. passwd: all authentication tokens updated successfully. Changing password for user kg02. passwd: all authentication tokens updated successfully. Changing password for user kg03. passwd: all authentication tokens updated successfully. Changing password for user kg04. passwd: all authentication tokens updated successfully. Changing password for user kg05. passwd: all authentication tokens updated successfully. Changing password for user kg06. passwd: all authentication tokens updated successfully. Changing password for user kg07. passwd: all authentication tokens updated successfully. Changing password for user kg08. passwd: all authentication tokens updated successfully. Changing password for user kg09. passwd: all authentication tokens updated successfully. Changing password for user kg10. passwd: all authentication tokens updated successfully. [root@lamp scripts]# cat /tmp/pass.txt kg01 3577b000 kg02 3ddefcfe kg03 c1ec8c1e kg04 0bdcdafc kg05 f172c380 kg06 99471166 kg07 fbe82aeb kg08 cad21c1c kg09 5f7cdc56 kg10 b60958ad [root@lamp scripts]# tail -10 /etc/passwd kg01:x:504:504::/home/kg01:/bin/bash kg02:x:505:505::/home/kg02:/bin/bash kg03:x:506:506::/home/kg03:/bin/bash kg04:x:507:507::/home/kg04:/bin/bash kg05:x:508:508::/home/kg05:/bin/bash kg06:x:509:509::/home/kg06:/bin/bash kg07:x:510:510::/home/kg07:/bin/bash kg08:x:511:511::/home/kg08:/bin/bash kg09:x:512:512::/home/kg09:/bin/bash kg10:x:513:513::/home/kg10:/bin/bash [root@lamp scripts]#
linux中取随机数的几种方法:
[root@lamp scripts]# echo $RANDOM 10474 [root@lamp scripts]# openssl rand -base64 8 C8ZBCmMhlKc= [root@lamp scripts]# openssl rand -base64 6 BSaZWw8v [root@lamp scripts]# openssl rand -base64 3 +S4K [root@lamp scripts]# openssl rand -base64 4 daNeuw== [root@lamp scripts]# date +%s%N 1448293383949936583 [root@lamp scripts]# date +%s%N 1448293385095579398 [root@lamp scripts]# date +%s%N 1448293386596694243 [root@lamp scripts]# head /dev/urandom|cksum 1336286598 1963 [root@lamp scripts]# head /dev/urandom|cksum 3913907857 2588 [root@lamp scripts]# head /dev/urandom|cksum 321627736 1498 [root@lamp scripts]# cat /proc/sys/kernel/random/uuid b8238fed-be13-47ac-8e30-b86ea0f822ff [root@lamp scripts]# cat /proc/sys/kernel/random/uuid 0bb0cb5d-157e-45f9-889a-0d6a93d3d328 [root@lamp scripts]# cat /proc/sys/kernel/random/uuid 526c2cb3-a335-4013-bf71-108e7517c4d9 [root@lamp scripts]#