Last active
August 16, 2016 04:46
-
-
Save jinuljt/a8935e05da4cb1e5ce5f0725a146c2e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding:utf-8 -*- | |
# created: Mon Sep 15 13:27:15 2014 | |
# filename: mkpasswd.py | |
# author: juntao liu | |
# email: [email protected] | |
# descritpion: | |
import datetime | |
import random | |
import argparse | |
import os | |
LOG_FILENAME = os.path.expanduser("~/.mkpasswd.log") | |
LENGTH = 8 | |
CHOICES = { | |
'number': '0123456789'*10, # number | |
'upper': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'*5, # captical letter | |
'lower': 'abcdefghijklmnopqrstuvwxyz'*5, # lower case | |
'special': '`~!@#$%^&*()_+-=[{]}\\|;:\'\",<.>/?' # special character | |
} | |
def log_password(password): | |
if os.path.exists(LOG_FILENAME): | |
with open(LOG_FILENAME, "a") as f: | |
now = datetime.datetime.now() | |
f.write("%s %s\n"%( now.strftime("%Y-%m-%d %H:%M:%S"), password)) | |
def contains(s, choice): | |
for ch in s: | |
if ch in choice: | |
return True | |
return False | |
def mkpasswd(length, **kwargs): | |
choices = CHOICES.copy() | |
for k,v in kwargs.items(): | |
if v and k in choices: | |
choices.pop(k) | |
password = '' | |
for _ in range(length): | |
password += random.choice(''.join(choices.values())) | |
for choice in choices.values(): | |
if not contains(password, choice): | |
return mkpasswd(length, **kwargs) | |
return password | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser("mkpasswd") | |
parser.add_argument('-n', '--no-number', action="store_true", default=False, help="password without number") | |
parser.add_argument('-u', '--no-upper', action="store_true", default=False, help="password without captial letter") | |
parser.add_argument('-l', '--no-lower', action="store_true", default=False, help="password without lowercase") | |
parser.add_argument('-s', '--no-special', action="store_true", default=False, help="password without special character") | |
parser.add_argument('length', metavar="length", nargs="?", type=int, default=LENGTH, help="password length") | |
ns, _ = parser.parse_known_args() | |
password = mkpasswd(ns.length, number=ns.no_number, lower=ns.no_lower, upper=ns.no_upper, special=ns.no_special) | |
log_password(password) | |
print(password) |
Author
jinuljt
commented
Apr 3, 2016
write password to ~/.mkpasswd.log , only if file exists.
touch ~/.mkpasswd.log, enable this feature
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment