|
| 1 | +fromargparseimportArgumentParser |
| 2 | +importrandom |
| 3 | +importstring |
| 4 | + |
| 5 | +# Setting up the Argument Parser |
| 6 | +parser=ArgumentParser( |
| 7 | +prog='Password Generator.', |
| 8 | +description='Generate any number of passwords with this tool.' |
| 9 | +) |
| 10 | + |
| 11 | +# Adding the arguments to the parser |
| 12 | +parser.add_argument("-n","--numbers",default=0,help="Number of digits in the PW",type=int) |
| 13 | +parser.add_argument("-l","--lowercase",default=0,help="Number of lowercase chars in the PW",type=int) |
| 14 | +parser.add_argument("-u","--uppercase",default=0,help="Number of uppercase chars in the PW",type=int) |
| 15 | +parser.add_argument("-s","--special-chars",default=0,help="Number of special chars in the PW",type=int) |
| 16 | + |
| 17 | +# add total pw length argument |
| 18 | +parser.add_argument("-t","--total-length",type=int, |
| 19 | +help="The total password length. If passed, it will ignore -n, -l, -u and -s, " \ |
| 20 | +"and generate completely random passwords with the specified length") |
| 21 | + |
| 22 | +# The amount is a number so we check it to be of type int. |
| 23 | +parser.add_argument("-a","--amount",default=1,type=int) |
| 24 | +parser.add_argument("-o","--output-file") |
| 25 | + |
| 26 | +# Parsing the command line arguments. |
| 27 | +args=parser.parse_args() |
| 28 | + |
| 29 | +# list of passwords |
| 30 | +passwords= [] |
| 31 | +# Looping through the amount of passwords. |
| 32 | +for_inrange(args.amount): |
| 33 | +ifargs.total_length: |
| 34 | +# generate random password with the length |
| 35 | +# of total_length based on all available characters |
| 36 | +passwords.append("".join( |
| 37 | + [random.choice(string.digits+string.ascii_letters+string.punctuation) \ |
| 38 | +for_inrange(args.total_length)])) |
| 39 | +else: |
| 40 | +password= [] |
| 41 | +# If / how many numbers the password should contain |
| 42 | +for_inrange(args.numbers): |
| 43 | +password.append(random.choice(string.digits)) |
| 44 | + |
| 45 | +# If / how many uppercase characters the password should contain |
| 46 | +for_inrange(args.uppercase): |
| 47 | +password.append(random.choice(string.ascii_uppercase)) |
| 48 | + |
| 49 | +# If / how many lowercase characters the password should contain |
| 50 | +for_inrange(args.lowercase): |
| 51 | +password.append(random.choice(string.ascii_lowercase)) |
| 52 | + |
| 53 | +# If / how many special characters the password should contain |
| 54 | +for_inrange(args.special_chars): |
| 55 | +password.append(random.choice(string.punctuation)) |
| 56 | + |
| 57 | +# Shuffle the list with all the possible letters, numbers and symbols. |
| 58 | +random.shuffle(password) |
| 59 | + |
| 60 | +# Get the letters of the string up to the length argument and then join them. |
| 61 | +password=''.join(password) |
| 62 | + |
| 63 | +# append this password to the overall list of password. |
| 64 | +passwords.append(password) |
| 65 | + |
| 66 | +# Store the password to a .txt file. |
| 67 | +ifargs.output_file: |
| 68 | +withopen(args.output_file,'w')asf: |
| 69 | +f.write('\n'.join(passwords)) |
| 70 | + |
| 71 | +print('\n'.join(passwords)) |