linux中shell產生隨機密碼樣本

來源:互聯網
上載者:User

為了產生更加無序及相應複雜的密碼,因此寫了個產生隨機密碼的指令碼,在此之前產生密碼通常我是通過如下命令實現的
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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.