使用:
mkpass -l [password length] [-nu|-nl|-ns]
获取帮助:
mkpass -h
点击(此处)折叠或打开
- #!/usr/bin/env python3
- # _*_coding:utf–8_*_
- # Auth by raysuen
- import sys,string,re
- import random
- class MakePass(object):
- rPassInfo={
- “Length”:8,
- “NoUpper”:False,
- “NoLower”:False,
- “NoSysbol”:False
- }
- def __init__(self,passlen=8,nu=False,nl=False,ns=False):
- if passlen != 8:
- self.rPassInfo[“Length”] = passlen
- if nu == True:
- self.rPassInfo[“NoUpper”] = True
- if nl == True:
- self.rPassInfo[“NoLower”] = True
- if ns == True:
- self.rPassInfo[“NoSysbol”] = True
- def MkPass(self):
- rPassStr={
- “passStr”:”,
- “AllChar”:[],
- “pwIndex”:[],
- “num”:0
- }
- if self.rPassInfo[“NoUpper”] == False:
- rPassStr[“AllChar”].append(random.choice(string.ascii_uppercase))
- rPassStr[“passStr”] = rPassStr[“passStr”]+”.join(string.ascii_lowercase)
- rPassStr[“num”] += 1
- if self.rPassInfo[“NoLower”] == False:
- rPassStr[“AllChar”].append(random.choice(string.ascii_lowercase))
- rPassStr[“passStr”] = rPassStr[“passStr”] + ”.join(string.ascii_lowercase)
- rPassStr[“num”] += 1
- if self.rPassInfo[“NoSysbol”] == False:
- rPassStr[“AllChar”].append(random.choice(“!@#$%&*()+.,”))
- rPassStr[“passStr”] = rPassStr[“passStr”] + “!@#$%&*()+.,”
- rPassStr[“num”] += 1
- rPassStr[“passStr”] = rPassStr[“passStr”] + ”.join(string.digits)
- rPassStr[“AllChar”].append(random.choice(string.digits))
- rPassStr[“num”] += 1
- rPassStr[“pwIndex”].extend(random.sample(range(1,self.rPassInfo[“Length”]),rPassStr[“num”]))
- rPassStr[“pwIndex”].sort()
- n = 0
- i = 0
- res = ”
- while i < self.rPassInfo[“Length”]:
- isMatch = False
- for j in rPassStr[“pwIndex”]:
- if i == j:
- res = res + ”.join(rPassStr[“AllChar”][n])
- n += 1
- i += 1
- isMatch = True
- break
- if isMatch == False:
- res = res + ”.join(random.choice(rPassStr[“passStr”]))
- i += 1
- return res
- def func_help():
- print(“”“
- Name:
- mkpass –make password
- Synopsis:
- mkpass -l [password length] [-nu|-nl|-ns]
- Description:
- -l Specify a number for the length of password,defaul 8
- -nu Don’t use upper
- -nl Don’t use lower
- -ns Don’t use special character
- -h Get help
- ““”)
- if __name__==‘__main__’:
- i = 1
- mp = MakePass()
- while i < len(sys.argv):
- if sys.argv[i] == ‘-l’: #–l means length
- if i+1 > len(sys.argv):
- print(“The value of -l must be specified!!!”)
- exit(1)
- elif re.match(“^-“, sys.argv[i+1]) != None: #判断下个参数是否以–开头
- print(“The value of -l must be specified!!!”)
- exit(1)
- elif sys.argv[i+1].isdecimal() == False: #判断–l下一个参数是否为数字
- print(“The value of -l must be digits”)
- exit(1)
- i += 1
- mp.rPassInfo[“Length”] = int(sys.argv[i]) #获取–l参数的value
- elif sys.argv[i] == ‘-nu’: #–nu 指定不适用大写字符
- mp.rPassInfo[“NoUpper”] = True
- elif sys.argv[i] == ‘-nl’: #–nu 指定不适用小写字符
- mp.rPassInfo[“NoLower”] = True
- elif sys.argv[i] == ‘-ns’: #–nu 指定不适用特殊字符
- mp.rPassInfo[“NoSysbol”] = True
- elif sys.argv[i] == ‘-h’:
- func_help()
- exit(0)
- i += 1
- print(mp.MkPass())