shell命令可以生成隨機密碼我在很早以前就介紹過一些例子了,這里看到一站長寫的文章再整理一下與大家一起學習他的方法.
為了生成更加無序及相應復雜的密碼,因此寫了個生成隨機密碼的腳本,在此之前生成密碼通常我是通過如下命令實現的:
cat /dev/urandom | head -n 1 | md5sum | head -c 16
好了,不說所了,直接上腳本,代碼如下:
- [root@liufofu shell]# cat make_random_passwd.sh
- #!/bin/bash
- #########################################
- # author m.survivalescaperooms.com
- # email Vevb.com@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 m.survivalescaperooms.com
- # email Vevb@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
新聞熱點
疑難解答