Example of shell generation random password in Linux

Source: Internet
Author: User
Tags mkdir

To generate a more unordered and complex password, write a script that generates a random password, which is usually done by using the following command
Cat/dev/urandom | Head-n 1 | md5sum | Head-c 16
All right, no, no, just go straight to the script.

The code is as follows Copy Code

[Root@liufofu shell]# Cat make_random_passwd.sh
#!/bin/bash
#########################################
# author Www.111cn.net
# email 14158286@qq.com
# date 2014-08-15
######### descprition ##################
# 1. Generate Random passwords
# 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. Generate Random passwords
# 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 "$" >> ${ff_logfile}
}
Rpasswd= ""
If [-Z $];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++));d o
rpasswd=${rpasswd}${ary[$RANDOM% ${#ary [*]}]}
#echo-N ${ary[$RANDOM% ${#ary [*]}]}
Done
Echo ${RPASSWD}

In this script, you can define your own ary this array and generate the type of password you want.

The script works as follows:

  code is as follows copy code

[Root@liufofu shell]# sh make_random_passwd.sh
z%j7jy7ee@yrwi8e
[root@liufofu shell]# sh make_random_passwd.sh ten
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_rando m_passwd.sh
39ezkitrp1e1kdb%
[root@liufofu shell]# sh make_random_passwd.sh
#Aw%jn@ppco9bh) r

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.