问题1:批量创建10个系统账号ucode01-ucode10并设置密码(密码不能相同)
#!/bin/bash
for n in $(seq -w 10)
do
useradd ucode-$n
echo "$n"|passwd --stdin ucode-$n
done
[[email protected] 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.
[[email protected] scripts]# su - ucode01
su: user ucode01 does not exist
[[email protected] scripts]# su - ucode-01
[[email protected] ~]$ su - ucode-02
Password:
[[email protected] ~]$
问题2:批量创建10个系统账号ucode01-ucode10并设置密码(密码为随机8位字符串)
随机数变量$RANDOM和时间$(date +%t%N):
[[email protected] scripts]# echo $RANDOM
30942
[[email protected] scripts]# echo $RANDOM
21755
[[email protected] scripts]# echo $(date +%N)
361278349
[[email protected] scripts]# echo $(date +%t%N)
821466599
[[email protected] scripts]# echo $(date +%t%N)
将两个随机数组合
[[email protected] scripts]# echo $(date +%t%N)$RANDOM
73420786120340
将结果交给md5加密处理一下
[[email protected] scripts]# echo $(date +%t%N)$RANDOM|md5sum
eca0ba74cc88c3accd864250be70aa0a -
[[email protected] scripts]#
取8位字符串
[[email protected] scripts]# echo $(date +%t%N)$RANDOM|md5sum |cut -c 2-9
9202fb1d
[[email protected] scripts]# echo $(date +%t%N)$RANDOM|md5sum |cut -c 2-9
7e5247a9
[[email protected] scripts]# echo $(date +%t%N)$RANDOM|md5sum |cut -c 2-9
67d9b364
[[email protected] 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
[[email protected] 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 ]
[[email protected] 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
[[email protected] scripts]# su - ucode-01
[[email protected] ~]$ su - ucode-02
Password:
拓展:linux生产系统产生随机数的6种方法
方法1:通过系统环境变量($RANDOM)
示例:
[[email protected] scripts]# echo $RANDOM
5016
[[email protected] scripts]#
[[email protected] scripts]# echo $RANDOM
14107
[[email protected] scripts]#
方法2:通过openssl产生随机数
示例:
[[email protected] scripts]# openssl rand -base64 8
9boFTwuEkRM=
[[email protected] scripts]# openssl rand -base64 10
vYsKdKydyVrSrw==
[[email protected] scripts]#
[[email protected] scripts]# openssl rand -base64 10|md5sum
fe581a8bd8787fa6e85a7afac5f1a66b -
[[email protected] scripts]#
方法3:通过时间获取随机数(date)
示例:
[[email protected] scripts]# date +%s%N
1437991244124034732
[[email protected] scripts]# date +%s%N
1437991246221217844
[[email protected] scripts]# date +%s%N%m
143799125181633289607
[[email protected] scripts]#
方法4:通过/dev/random设备来获取
说明:/dev/random 设备,存储着系统当前运行的环境实时数据。它可以看做是系统某个时候的唯一数据,因此可以用作随机数元数据。他们可以通过文件读取方式,读得里面数据。/dev/urandom这个设备数据与random里面一样。只是,它是非塞的随机数发生器。读取操作不会产生阻塞。
示例:
[[email protected] scripts]# head /dev/urandom |cksum
3711806028 1786
[[email protected] scripts]# head /dev/urandom |cksum
1097231128 1469
[[email protected] scripts]# head /dev/urandom |cksum
1494175762 2776
[[email protected] scripts]# head /dev/urandom |cksum
176794259 4099
[[email protected]localhost scripts]#
[[email protected] 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用完。
示例:
[[email protected] scripts]# cat /proc/sys/kernel/random/uuid
4e5118d3-e262-48d7-9e19-148b1c7da81f
[[email protected] scripts]# cat /proc/sys/kernel/random/uuid
c2fe4d92-55e7-45da-b4cf-c972483bed02
[[email protected] scripts]# cat /proc/sys/kernel/random/uuid
c002946e-db84-44a9-a170-1f285b3ff117
[[email protected] scripts]#
也可以用md5进行加密
[[email protected] scripts]# cat /proc/sys/kernel/random/uuid |md5sum
4d0e0f6799ad6c15d27bd17f1483bf58 -
[[email protected] scripts]#
方法6:通过expect获取随机数
[[email protected] scripts]# yum -y install expect
mkpasswd -l 8
[[email protected] scripts]# mkpasswd -l 8
qGQ4ee5#
[[email protected] scripts]# mkpasswd -l 8
4lmMx+V2
[[email protected] scripts]# mkpasswd -l 8
sL2jj3I\
[[email protected] scripts]# mkpasswd -l 8
HL9)o0rd
[[email protected] scripts]# mkpasswd -l 8
[email protected]
[[email protected] scripts]#
或者加参数 -s -l等
[[email protected] scripts]# mkpasswd -s 0
VTk9gb3em
[[email protected] scripts]# mkpasswd -s 0
0NQvx8ivg
[[email protected] scripts]# mkpasswd -s 0
C6kUps5zf
[[email protected] scripts]# mkpasswd -s 0
9rBbhQn9n
[[email protected] 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}[email protected]&,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 命令
[[email protected] scripts]# cat /proc/sys/kernel/random/uuid |md5sum |cut -c 1-9
873c50fe2
[[email protected] scripts]# head /dev/urandom |cksum |md5sum |cut -c 1-9
2a2b35b18
[[email protected] scripts]# date +%s%N|md5sum |cut -c 1-9
6687443ee
[[email protected] scripts]# openssl rand -base64 10|md5sum |cut -c 1-9
6d2d2618c
[[email protected] scripts]# echo $RANDOM|md5sum |cut -c 1-9
e7109ef67
[[email protected] scripts]#
[[email protected] scripts]# mkpasswd -s 0|md5sum |cut -c 1-9
624d2bb67
[[email protected] scripts]#