记录日常工作关于系统运维,虚拟化云计算,数据库,网络安全等各方面问题。

linux中shell生成随机密码示例


shell命令可以生成随机密码我在很早以前就介绍过一些例子了,这里看到一站长写的文章再整理一下与大家一起学习他的方法。

为了生成更加无序及相应复杂的密码,因此写了个生成随机密码的脚本,在此之前生成密码通常我是通过如下命令实现的
cat /dev/urandom | head -n 1 | md5sum | head -c 16
好了,不说所了,直接上脚本

 代码如下复制代码
 
[root@liufofu shell]# cat make_random_passwd.sh
#!/bin/bash
#########################################
# author        www.111cn.net
# email         14158286@qq.com
# date          2014-08-15
######### descprition ##################
# 1.生成随机密码
# 2.
########################################
#init variables
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
 
ff_outputdir=/tmp/liufofu
curdate=$(date +%Y%m%d)
curtime=$(date +%H%M%S)
ff_logfile=${ff_outputdir}/${curdate}.log
 
if [ ! -e ${ff_outputdir} ];then
    mkdir -p ${ff_outputdir}
fi
 
#处理过程中产生的日志由日志函数来进行处理记录
[root@liufofu shell]# cat make_random_passwd.sh
#!/bin/bash
#########################################
# author        www.111cn.net
# email         14158286@qq.com
# date          2014-08-15
######### descprition ##################
# 1.生成随机密码
# 2.
########################################
#init variables
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
 
ff_outputdir=/tmp/liufofu
curdate=$(date +%Y%m%d)
curtime=$(date +%H%M%S)
ff_logfile=${ff_outputdir}/${curdate}.log
 
if [ ! -e ${ff_outputdir} ];then
    mkdir -p ${ff_outputdir}
fi
 
#处理过程中产生的日志由日志函数来进行处理记录
function log()
{
    echo "`date +"%Y:%m:%d %H-%M-%S"` $1 "  >> ${ff_logfile}
}
rpasswd=""
if [ -z $1 ];then
    rlen=16
else
    rlen=$1
fi
ary=(0 1 2 3 4 5 6 7 8 9 \( a b c d e f g h i i \) j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z @ % \# \!)
for ((i=1;i<=${rlen};i++));do
    rpasswd=${rpasswd}${ary[$RANDOM % ${#ary[*]}]}
    #echo -n ${ary[$RANDOM % ${#ary[*]}]}
done
echo ${rpasswd}

在这个脚本中,你可以自行定义ary这个数组,生成你自己所要的密码类型。
 
脚本的运行效果如下:

 代码如下复制代码

[root@liufofu shell]# sh make_random_passwd.sh
z%J7Jy7EE@YrWi8E
[root@liufofu shell]# sh make_random_passwd.sh 10
lW6IiCcJyi
[root@liufofu shell]# sh make_random_passwd.sh 6
ZiEIqj
[root@liufofu shell]# sh make_random_passwd.sh 1
Z
[root@liufofu shell]# sh make_random_passwd.sh 7
Jyw28dB
[root@liufofu shell]# sh make_random_passwd.sh
39eZkiTrp1e1kDb%
[root@liufofu shell]# sh make_random_passwd.sh
#Aw%Jn@PPcO9bH)r



转载请标明出处【linux中shell生成随机密码示例】。

《www.micoder.cc》 虚拟化云计算,系统运维,安全技术服务.

网站已经关闭评论