shell生成随机数

avatar 2020年2月20日18:01:13 1 1,555 次浏览

本章学习在Centos7上,使用shell生成随机数,随机数包含整数,字符串,整数和字符串组合,整数和字符串加特殊字符。我们先分别说明一下随机数的生成方法,以及生成整数或字符串的长度,先说一下整数

随机生成整数

方法一:$RANDOM

$RANDOM变量是系统自带的随机变量,我们可以根据$RANDOM变量随机打印0~32767的随机整数,先看一下打印$RANDOM变量的效果:

[root@localhost wulaoer]# echo $RANDOM
7301
[root@localhost wulaoer]# echo $RANDOM
28663

如果想使用$RANDOM变量随机范围,例如:$(($RANDOM%27))表示0到26的范围数,是对27取模,最少是0,最大是26.

如果想获取十以内的整数,可以取模10,最大9,最少0,也就是一位数。

[root@localhost wulaoer]# echo $(($RANDOM%10))
2

如果想取两位数的随机数,只需要把10变成100即可

[root@localhost wulaoer]# echo $(($RANDOM%100))
89

取一段随机范围,先看下面的例子:

[root@localhost wulaoer]# echo $(($RANDOM%40+1))

这里要分开解释,首先是$(($RANDOM%40))取值范围是0到39,我们加1取的最大值就是40,最小值就是1。取值范围是1到40之间。再看下面的例子:

[root@localhost wulaoer]# echo $(($RANDOM%40+8))

这个首先是取值范围是0到39,加8后取最大值是47,最小值是8.

这是使用$RANDOM变量取的整数,如果想获取四位数的整数可以参考上面的方法:

[root@localhost wulaoer]# echo $(($RANDOM%9000+1000))
4444

给文件进行排序,然后查看最大的整数

[root@localhost wulaoer]# sort -n a.txt 
....................
9999
9999
9999
9999

方法二:/dev/urandom

/dev/urandom是Unix操作系统中推荐的加密种子,不会阻塞。可以随机生成数字和字符串,一下是生成数字方法:

[root@localhost wulaoer]# tr -dc "0-9" < /dev/urandom | head -c 10
5015464913[root@localhost wulaoer]# 
[root@localhost wulaoer]# head /dev/urandom | tr -dc 0-9 | head -c 10
6074787303[root@localhost wulaoer]# 

tr -dc "0-9"中"0-9"表示随机生成的数,head -c 10中的10表示随机生成数的长度。

方法三: 使用/dev/urandom配合cksum使用

[root@localhost wulaoer]# head /dev/urandom | cksum
1750151684 3829
[root@localhost wulaoer]# head /dev/urandom | cksum |  cut -c 1-10   #截取指定长度的数字
3396223911

方法四:使用openssl

openssl是一个开源的ssl加密技术,在浏览器中访问web使用的https就是使用的ssl加密技术,我们如果想随机获取数字,必须安装openssl。

[root@localhost wulaoer]# yum -y install openssl
[root@localhost wulaoer]# yum -y install openssl-devel

安装之后我们使用openssl获取随机数字

[root@localhost wulaoer]# openssl rand -base64 12 | md5sum | tr [a-z] [0-9]
12010481315217752390273300548115  -
[root@localhost wulaoer]# openssl rand -base64 12 | md5sum | tr [a-z] [0-9] | cut -c 1-10  #截取指定长度的数字
5863131743
[root@localhost wulaoer]# openssl rand -base64 12 | md5sum | tr [a-z] [A-Z] | cksum |cut -f1 -d" "
1661552658
[root@localhost wulaoer]# openssl rand -base64 12 | md5sum |  cksum |cut -c 1-10
2750913654

方法五:使用date +%s

date是系统命令,'date +%s'是获取的秒数,所以数字长度不能限制

[root@localhost wulaoer]# date +%s |cksum |cut -d " " -f 1 
2373423463

方法六:查看uuid文件

/proc/sys/kernel/random/uuid中的uuid码是内核提供的一个标准输出,正常格式是这样的:

[root@localhost wulaoer]# cat /proc/sys/kernel/random/uuid
c047c004-5bf3-401d-9889-1104121c11f0

我们使用cksum |cut -f1 -d" "处理一下就只有数字没有其他符合和字母了

[root@localhost wulaoer]# cat /proc/sys/kernel/random/uuid| cksum |cut -f1 -d" "
655970218

方法七:使用mkpasswd

mkpasswd是expect附带的,所以如果使用mkpasswd必须安装expect。

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

安装成功之后,我们就可以使用mkpasswd命令了,不过mkpasswd命令需要参数,5个参数都不能少

-l 指定密码长度
-d 指定密码中数字的数量
-c 指定密码中小写字母的数量
-C 指定密码中大写字母的数量
-s 指定密码中特殊字符的数量

举例说明一下:

[root@localhost wulaoer]# mkpasswd -l 10 -d 10 -c 0 -C 0 -s 0   #数字的数量必须和长度一致
0083600057
[root@localhost wulaoer]# mkpasswd -l 10 -d 10 -c 0 -C 0 -s 0
3063494459

注:mkpasswd的五个参数必须同时使用,少一个参数就会报错

随机生成字符串

方法一:使用mkpasswd

同上mkpasswd需要安装expect,根据参数我们根据随机打印是否有特殊字符、是否有大小写等字符串。

举例说明一下:

[root@localhost wulaoer]# mkpasswd -l 10 -d 4 -c 2 -C 2 -s 0  #没有特殊字符
Ci46o3qG1g
[root@localhost wulaoer]# mkpasswd -l 10 -d 4 -c 2 -C 0 -s 2  #没有大写
o0#x7^me99
[root@localhost wulaoer]# mkpasswd -l 10 -d 4 -c 0 -C 2 -s 2  #没有小写
U!m94y2Z;9

方法二:/dev/urandom

这里使用正则的方式,可以根据自己的需要只打印大写和数字或者大小写和数字的需求。

[root@localhost wulaoer]# tr -dc "a-z0-9" < /dev/urandom | head -c 10
2sylhng0tb[root@localhost wulaoer]# 
[root@localhost wulaoer]# 
[root@localhost wulaoer]# tr -dc "A-Z0-9" < /dev/urandom | head -c 10
SMCC172HLR[root@localhost wulaoer]# tr -dc "A-Za-z0-9" < /dev/urandom | head -c 10
OvUFPTV7Ev
[root@localhost wulaoer]# tr -dc "A-Za-z0-9#@" < /dev/urandom | head -c 10
#FKTf0Lbyw

方法三:openssl方法

说明同上

[root@localhost wulaoer]# openssl rand -hex 10
5ab5974411a74d316701
[root@localhost wulaoer]# openssl rand -base64 12 | md5sum | tr [a-z] [A-Z] | cut -c 1-10   #截取指定长度的数字
0318E45622

这里没有看到支持特殊字符的方法,后期找到在补上。

方法四:使用pwgen

pwgen是一个简单的命令行工具,可以在断时间生成一个高强度的密码,需要安装

[root@localhost wulaoer]# yum install -y epel-release
[root@localhost wulaoer]# yum -y install pwgen

参数:

-c或–capitalize密码中至少包含一个大写字母
-A或–no-capitalize密码中不包含大写字母
-n或–numerals密码中至少包含一个数字
-0或–no-numerals密码中不包含数字
-y或–symbols密码中至少包含一个特殊符号
-s或–secure生成完全随机密码
-B或–ambiguous密码中不包含歧义字符(例如1,l,O,0)
-H或–sha1 =路径/到/文件[#seed]使用SHA1哈希给定的文件作为一个随机种子
-C在列中打印生成的密码
-1不要在列 打印生成的密码,即一行一个密码
-v或-no元音不要使用任何元音,以避免偶然的脏话

看下例子:

[root@localhost wulaoer]# pwgen 14 1 -n 14
bieN0oocei9Ish
[root@localhost wulaoer]# pwgen 14 1 -n 4 -A 3
paqu0heek7veic
[root@localhost wulaoer]# pwgen 14 1 -n 4 -c
aithu9eiHe3ohv

方法五:使用uuid

uuid使用说明同上

[root@localhost wulaoer]#  cat /proc/sys/kernel/random/uuid
7d9d00fa-e00f-48e1-9ede-e4afa679aaff
[root@localhost wulaoer]#  cat /proc/sys/kernel/random/uuid
584db186-5cc6-4a35-8a80-6b070a3379f0

方法六:使用/dev/urandom

[root@localhost wulaoer]#  tr -dc "a-z" < /dev/urandom | head -c 10		#小写字符
npowcyeipp[root@localhost wulaoer]# tr -dc "A-Z" < /dev/urandom | head -c 10 	#大写字符
VBAIMBKLBO[root@localhost wulaoer]# tr -dc "A-Za-z0-9" < /dev/urandom | head -c 10 	#生成大小写和数字的字符串
[root@localhost wulaoer]# tr -dc "A-Za-z0-9#@" < /dev/urandom | head -c 10   #生成大小写和数字以及特殊字符的字符串
SO5OPLy#rT

方法七:使用date +%s

可以利用正则根据需求生成字符串

[root@localhost wulaoer]# date +%s| md5sum | tr -dc "a-z" |head -c 10  #小写字符
bacbaccabb
[root@localhost wulaoer]# date +%s| md5sum | head -c 10     #数字和字母组合字符串
12925185d4
[root@localhost wulaoer]# date +%s| md5sum | tr -dc "a-z0-9"  |head -c 10   #小写和数字组合
dc6b4aa924

因date就是打印字符串的,所以没有大写组合。

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:1   其中:访客  1   博主  0

    • avatar imjamin 4

      date +%s | rev | cut -c 1-4 取4位随机数,虽然不是真随机