Several examples of Python generating random passwords

Source: Internet
Author: User
Tags lowercase

Before writing a shell to generate random passwords, in order to follow more automation, part of the work has been transferred to the automation of Python processing, so many of the original shell tools have to be transformed
The following is written by Python to generate random passwords, the use of knowledge points are: Random used to generate random numbers, Sys used to get the parameters of the command line, the default without parameters, generate 16-bit password, but also need to improve the place are:
1, the level of generation of passwords, such as only to generate numbers or lowercase letters, etc.

The code is as follows Copy Code
[Root@liufofu python]# Cat ff_mkpasswd.py
#!/usr/bin/env python
#coding =utf-8
##########################################
######### descprition ##################
# 1.make Random Password
# 2.
########################################
Import Sys
Import Random

Def print_usage ():
Help_info= ' "
NAME:
ff_mkpasswd.py

SYNTAX:
ff_mkpasswd.py arg1

FUNCTION:
Make random password

'''
def mk_random_passwd (Arylist,rlen):
Randpasswd= ""
I=0
While I<rlen:
Rindex=int (Random.random () *10000)%len (arylist)
Randpasswd=randpasswd+str (Arylist[rindex])
I=i+1
Return RANDPASSWD


If __name__== ' __main__ ':
Arylist=[' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' k ', ' l ', ' m ', ' n ', ' O '
, ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z '
, 0,1,2,3,4,5,6,7,8,9, '! ', ' @ ', ' # ', ' $ ', '% ', ' ^ ', ' & ', ' * ', ' (', ') ', ' _ ', ' = '
, ' + ', ' [', '] ', ' {', '} ', '. ', ' < ', ' > ', '? '
' A ', ' B ', ' C ', ' D ', ' E ', ', F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' R ', ' S ',
' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z '
Rlen=0
If Len (SYS.ARGV) <2:
Rlen=16
Else
If Sys.argv[1].isdigit ():
Rlen=int (Sys.argv[1])
Else
Rlen=16


Print mk_random_passwd (Arylist,rlen)

The result of the call is as follows:
[Root@liufofu python]#./ff_mkpasswd.py
$[2zg%%wcg4&t.sk
[Root@liufofu python]#./ff_mkpasswd.py 5
vzm#$
[Root@liufofu python]#./ff_mkpasswd.py 10
43nmlo%noz
[Root@liufofu python]#./ff_mkpasswd.py 1
^
[Root@liufofu python]#./ff_mkpasswd.py 2
2N

Example 2

The code is as follows Copy Code

#-*-Coding:utf-8-*-
'''
Briefly generates random passwords, including uppercase and lowercase letters, numbers, and can specify password lengths
'''
#生成随机密码
From random import choice
Import string

#python3中为string. Ascii_letters, while Python2 can use String.letters and string.ascii_letters

def genpassword (length=8,chars=string.ascii_letters+string.digits):
Return '. Join ([Choice (chars) to I in range (length)])

If __name__== "__main__":
#生成10个随机密码
For I in range (10):
#密码的长度为8
Print (Genpassword (8))


Example 3

  code is as follows copy code

#!/usr/bin /env python
#-*-coding:utf-8-*-
#导入random和string模块
Import Random, String
def genpassword (length):
& nbsp;   #随机出数字的个数
    numofnum = Random.randint (1,length-1)
    Numofletter = Length-numofnum
    #选中numOfNum个数字
    slcnum = [Random.choice ( String.digits) for I in Range (numofnum)]
    #选中numOfLetter个字母
    slcletter = [ Random.choice (string.ascii_letters) for I in Range (Numofletter)]
    #打乱这个组合
     Slcchar = slcnum + slcletter
    random.shuffle (slcchar)
    #生成密码
& nbsp;   genPwd = '. Join ([I for I on Slcchar])
    return genPwd
If __name__ = ' __main__ ':
    print Genpassword (6)

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.