• shell生成随机数的几种方法


    一、通过内部系统变量($RANDOM)

    生成0-32767之间的整数随机数,若超过5位可以加个固定10位整数,然后进行求余。

    1 [root@web01 scripts]# echo $RANDOM
    2 8713
    3 [root@web01 scripts]# echo $RANDOM|md5sum 
    4 97e88f505ae3b0faced5a9e08ede02d2  -
    5 [root@web01 scripts]# echo $RANDOM|md5sum |cut -c 2-8
    6 1ecd073

    二、openssl rand产生随机数

    openssl rand 用于产生指定长度个bytes的随机字符。-base64或-hex对随机字符串进行base64编码或用hex格式显示

    1 [root@web01 scripts]# openssl rand -base64 8
    2 JY8vRoi3d2M=
    3 [root@web01 scripts]# openssl rand -base64 8|md5sum 
    4 f1f1709b1f3a96d101295c1d96e9a9db  -
    5 [root@web01 scripts]# openssl rand -base64 8|md5sum |cut -c 2-8
    6 5f76ee4

    三、通过时间获得随机数(date)

    1 [root@web01 scripts]# date +%s%N
    2 1475820812826278588
    3 [root@web01 scripts]# date +%s%N|md5sum 
    4 6d33c90c996b221ced709b671714ad62  -
    5 [root@web01 scripts]# date +%s%N|md5sum |cut -c 2-8
    6 985f894

    四、Linux的uuid码

    1 [root@web01 scripts]# uuidgen 
    2 5e5746cd-23f5-4b76-b240-7d7a01029612
    3 [root@web01 scripts]# uuidgen |md5sum 
    4 cb859516d530265f4ea3a90b171501c6  -
    5 [root@web01 scripts]# uuidgen |md5sum |cut -c 2-8
    6 6bb369b

    五、expect的mkpasswd

    1 [root@web01 scripts]# yum install expect
    2 [root@web01 scripts]# mkpasswd -l 8
    3 rh5Wr8J)
    4 [root@web01 scripts]# mkpasswd -l 8|md5sum 
    5 fd41f7af9a5140b7f965ab6f06c67f27  -
    6 [root@web01 scripts]# mkpasswd -l 8|md5sum |cut -c 2-8
    7 bd40e7d

    六、awk的随机函数

    1 [root@web01 scripts]# awk 'BEGIN{srand();print rand()*1000000}'
    2 918085
    3 [root@web01 scripts]# awk 'BEGIN{srand();print rand()*1000000}'|md5sum 
    4 f3199f44505732ccc177180653300171  -
    5 [root@web01 scripts]# awk 'BEGIN{srand();print rand()*1000000}'|md5sum |cut -c 2-8
    6 9e2b9a6

    为了加强密码强度,可以混合搭配使用,还要一些其他方式也可以获取随机数,在此仅列出常用几个。

    o(=•ェ•=)m纸上得来终觉浅,绝知此事要躬行o(=•ェ•=)m
  • 相关阅读:
    剑指OFFER 滑动窗口的最大值
    剑指OFFER 正则表达式匹配
    linux动态链接库的使用
    剑指OFFER 序列化二叉树
    剑指OFFER 数字在排序数组中出现的次数
    剑指OFFER 数组中的逆序对
    剑指OFFER 反转链表
    剑指OFFER 二叉树的深度
    剑指OFFER 矩形覆盖
    网络相关的命令工具-iptables
  • 原文地址:https://www.cnblogs.com/occl/p/5935930.html
Copyright © 2020-2023  润新知