问题1:批量创建10个系统账号ucode01-ucode10并设置密码(密码不能相同)

#!/bin/bash
for n in $(seq -w 10)
do
      useradd ucode-$n
      echo "$n"|passwd --stdin ucode-$n
done

[root@localhost scripts]# sh adduser01.sh
Changing password for user ucode-01.
passwd: all authentication tokens updated successfully.
Changing password for user ucode-02.
passwd: all authentication tokens updated successfully.
Changing password for user ucode-03.
passwd: all authentication tokens updated successfully.
Changing password for user ucode-04.
passwd: all authentication tokens updated successfully.
Changing password for user ucode-05.
passwd: all authentication tokens updated successfully.
Changing password for user ucode-06.
passwd: all authentication tokens updated successfully.
Changing password for user ucode-07.
passwd: all authentication tokens updated successfully.
Changing password for user ucode-08.
passwd: all authentication tokens updated successfully.
Changing password for user ucode-09.
passwd: all authentication tokens updated successfully.
Changing password for user ucode-10.
passwd: all authentication tokens updated successfully.

[root@localhost scripts]# su - ucode01
su: user ucode01 does not exist
[root@localhost scripts]# su - ucode-01
[ucode-01@localhost ~]$ su - ucode-02
Password:
[ucode-02@localhost ~]$


问题2:批量创建10个系统账号ucode01-ucode10并设置密码(密码为随机8位字符串)

随机数变量$RANDOM和时间$(date +%t%N):

[root@localhost scripts]# echo $RANDOM
30942

[root@localhost scripts]# echo $RANDOM
21755
[root@localhost scripts]# echo $(date +%N)
361278349
[root@localhost scripts]# echo $(date +%t%N)
821466599
[root@localhost scripts]# echo $(date +%t%N)

将两个随机数组合

[root@localhost scripts]# echo $(date +%t%N)$RANDOM
73420786120340

将结果交给md5加密处理一下

[root@localhost scripts]# echo $(date +%t%N)$RANDOM|md5sum
eca0ba74cc88c3accd864250be70aa0a  -
[root@localhost scripts]#

取8位字符串

[root@localhost scripts]# echo $(date +%t%N)$RANDOM|md5sum |cut -c 2-9

9202fb1d
[root@localhost scripts]# echo $(date +%t%N)$RANDOM|md5sum |cut -c 2-9
7e5247a9
[root@localhost scripts]# echo $(date +%t%N)$RANDOM|md5sum |cut -c 2-9
67d9b364
[root@localhost scripts]# echo $(date +%t%N)$RANDOM|md5sum |cut -c 2-9
808d5c06

编写脚本:

vim userdeluser02.sh   
#!/bin/bash
. /etc/init.d/functions
>/mnt/user.txt
for n in $(seq -w 10)
do
     passwd=`echo $(date +%t%N)$RANDOM|md5sum |cut -c 2-9`
     useradd ucode-$n >&/dev/null && user_status=$?
     echo "$passwd"|passwd --stdin ucode-$n >&/dev/null && passwd_status=$?
     if [ $user_status -eq 0 -a $passwd_status -eq 0 ];then
         action "adduser ucode-$n" /bin/true
         echo -e "user:\tucode-$n pass:\t$passwd" >>/mnt/user.txt
     else
         action "adduser ucode-$n" /bin/false
         echo -e "user:\tucode-$n pass:\t$passwd">>/mnt/fail_user.txt
     fi
done

[root@localhost scripts]# sh  userdeluser02.sh                             
adduser ucode-01                                        [  OK  ]
adduser ucode-02                                        [  OK  ]
adduser ucode-03                                        [  OK  ]
adduser ucode-04                                        [  OK  ]
adduser ucode-05                                        [  OK  ]
adduser ucode-06                                        [  OK  ]
adduser ucode-07                                        [  OK  ]
adduser ucode-08                                        [  OK  ]
adduser ucode-09                                        [  OK  ]
adduser ucode-10                                        [  OK  ]
[root@localhost scripts]# cat /mnt/user.txt
user:   ucode-01 pass: a3410e42
user:   ucode-02 pass: 0e38b5b2
user:   ucode-03 pass: 9512c1a0
user:   ucode-04 pass: 7fd7b5fe
user:   ucode-05 pass: 384f4c6c
user:   ucode-06 pass: 611525b8
user:   ucode-07 pass: cfc69fdb
user:   ucode-08 pass: ddddbbe9
user:   ucode-09 pass: aeb10ac0
user:   ucode-10 pass: 7f64bf48
[root@localhost scripts]# su - ucode-01
[ucode-01@localhost ~]$ su - ucode-02
Password:



拓展:linux生产系统产生随机数的6种方法

方法1:通过系统环境变量($RANDOM)

示例:

[root@localhost scripts]# echo $RANDOM
5016
[root@localhost scripts]#
[root@localhost scripts]# echo $RANDOM
14107
[root@localhost scripts]# 


方法2:通过openssl产生随机数

示例:

[root@localhost scripts]# openssl rand -base64 8
9boFTwuEkRM=
[root@localhost scripts]# openssl rand -base64 10
vYsKdKydyVrSrw==
[root@localhost scripts]#

[root@localhost scripts]# openssl rand -base64 10|md5sum 
fe581a8bd8787fa6e85a7afac5f1a66b  -
[root@localhost scripts]#


方法3:通过时间获取随机数(date) 

示例:

[root@localhost scripts]# date +%s%N
1437991244124034732
[root@localhost scripts]# date +%s%N
1437991246221217844
[root@localhost scripts]# date +%s%N%m
143799125181633289607
[root@localhost scripts]# 


方法4:通过/dev/random设备来获取

说明:/dev/random 设备,存储着系统当前运行的环境实时数据。它可以看做是系统某个时候的唯一数据,因此可以用作随机数元数据。他们可以通过文件读取方式,读得里面数据。/dev/urandom这个设备数据与random里面一样。只是,它是非塞的随机数发生器。读取操作不会产生阻塞。

示例:

[root@localhost scripts]# head /dev/urandom |cksum
3711806028 1786
[root@localhost scripts]# head /dev/urandom |cksum
1097231128 1469
[root@localhost scripts]# head /dev/urandom |cksum
1494175762 2776
[root@localhost scripts]# head /dev/urandom |cksum
176794259 4099
[root@localhost scripts]# 

[root@localhost scripts]# head /dev/urandom |cksum|md5sum 

885e794c0ff6b821c7a94b6f4623ac76  -


方法5:利用UUID码来获取随机数

说明:UUID码全称是通用唯一识别码(Universally Unique Identifiter,UUID)它是一个软件构建的标准,亦为自由软件基金会(Open Software Foundation,OSF)的组织在分布式计算环境(Distributed Computing Enveronment)领域的一部分。

UUID的目的是让分布式系统中的所有元素都能有唯一的辨识信息,而不需要通过中央控制端来做辨识信息的指定,如此一来,每个人都可以建立不与其他人冲突的UUID。在这样的情况下,就不需要考虑数据创建 时的名称重复问题,它会让网络内任何一台计算机所生成的uuid码都是互联网整个服务器网络中是唯一的。它的原信息会加入硬件,时间,机器当前运行信息等。

UUID的格式是:包含32个16进位数字,以"-"连接号分为五段,形式为8-4-4-4-12的32个字符。范例:550e8400-e29b-41d4-a716-446655440000,所以:UUID理论上的总素为216 x 8 =2128,约等于3.4 x 1038,也就是说若每秒产生1兆个UUID,则要花100亿年才会将所有的UUID用完。

示例:

[root@localhost scripts]# cat /proc/sys/kernel/random/uuid
4e5118d3-e262-48d7-9e19-148b1c7da81f
[root@localhost scripts]# cat /proc/sys/kernel/random/uuid
c2fe4d92-55e7-45da-b4cf-c972483bed02
[root@localhost scripts]# cat /proc/sys/kernel/random/uuid
c002946e-db84-44a9-a170-1f285b3ff117
[root@localhost scripts]# 

也可以用md5进行加密

[root@localhost scripts]# cat /proc/sys/kernel/random/uuid |md5sum
4d0e0f6799ad6c15d27bd17f1483bf58  -
[root@localhost scripts]#

方法6:通过expect获取随机数

[root@localhost scripts]# yum -y install expect

mkpasswd -l 8

[root@localhost scripts]# mkpasswd -l 8
qGQ4ee5#
[root@localhost scripts]# mkpasswd -l 8
4lmMx+V2
[root@localhost scripts]# mkpasswd -l 8
sL2jj3I\
[root@localhost scripts]# mkpasswd -l 8
HL9)o0rd
[root@localhost scripts]# mkpasswd -l 8
Nmi0hL2@
[root@localhost scripts]# 

或者加参数 -s -l等

[root@localhost scripts]# mkpasswd  -s 0
VTk9gb3em
[root@localhost scripts]# mkpasswd  -s 0
0NQvx8ivg
[root@localhost scripts]# mkpasswd  -s 0
C6kUps5zf
[root@localhost scripts]# mkpasswd  -s 0
9rBbhQn9n
[root@localhost scripts]# 


查看mkpasswd参数设置 mkpasswd命令的常用参数含义:

usage: mkpasswd [args] [user]

 where arguments are:

-l #      (length of password, default = 7)

                   指定密码的长度,默认是7位数

-d #      (min # of digits, default = 2)

                   指定密码中数字最少位数,默认是2位

-c #      (min # of lowercase chars, default = 2)

                   指定密码中小写字母最少位数,默认是2位

-C #      (min # of uppercase chars, default = 2)

                   指定密码中大写字母最少位数,默认是2位

-s #      (min # of special chars, default = 1)

                   指定密码中特殊字符最少位数,默认是1位

-v        (verbose, show passwd interaction)

                   这个参数在实验的时候报错,具体不知道。


参数:

    -l #      (密码的长度定义, 默认是 9)

    -d #      (分位数, 默认是 16)

    -c #      (小写字符, 默认是 3)

    -C #      (大写字符, 默认是 2)

    -s #      (特殊字符, 默认是  1)

    -v        (详细。。。)

    -p prog   (程序设置密码, 默认是 passwd)

代码示例:

#mkpasswd  -l 20 -d 5 -c 5 -C 5 -s 5 root

Z}K7hp0UPJ6v@&,c5{d3


批量生成用户脚本

#!/bin/bash

for n in $(seq -w 3)

do   

          useradd ucode-$n

          passwd=`/usr/bin/mkpasswd -l 16 -c 3 -C 4 -d 4 -s 5`

          echo $passwd|passwd --stdin ucode-$n

          echo "$passwd:ucode-$n" >> /opt/userlist.txt

done



上面的随机数长短不一,如何统一格式化呢?答:使用md5sum 命令

[root@localhost scripts]# cat /proc/sys/kernel/random/uuid |md5sum |cut -c 1-9
873c50fe2
[root@localhost scripts]# head /dev/urandom |cksum |md5sum |cut -c 1-9
2a2b35b18
[root@localhost scripts]# date +%s%N|md5sum |cut -c 1-9
6687443ee
[root@localhost scripts]# openssl rand -base64 10|md5sum |cut -c 1-9
6d2d2618c
[root@localhost scripts]# echo $RANDOM|md5sum |cut -c 1-9
e7109ef67
[root@localhost scripts]# 

[root@localhost scripts]# mkpasswd -s 0|md5sum |cut -c 1-9 
624d2bb67
[root@localhost scripts]# 


自定义创建用户账户并生成密码:

[root@Python shell]# sh create.sh adminsys
Changing password for user adminsys.
passwd: all authentication tokens updated successfully.
[root@Python shell]# cat /opt/userlist.txt 
+60j9Qv[TCb'<8I/:ricks
username:adminsys ## password:t6+&B)q29[WP9T[w
[root@Python shell]# cat  create.sh            
#!/bin/bash
useradd $1
passwd=`/usr/bin/mkpasswd -l 16 -c 3 -C 4 -d 4 -s 5`
echo $passwd|passwd --stdin $1
echo "username:$1 ## password:$passwd" >> /opt/userlist.txt
[root@Python shell]#