import secrets
import string
import argparse
def generate_password(length=12, upper=True, digits=True, special=True):
"""Generate a secure random password with customizable complexity rules."""
chars = string.ascii_lowercase
if upper:
chars += string.ascii_uppercase
if digits:
chars += string.digits
if special:
chars += string.punctuation
while True:
password = ''.join(secrets.choice(chars) for _ in range(length))
# Ensure password meets all complexity requirements
if (not upper or any(c.isupper() for c in password)) and \
(not digits or any(c.isdigit() for c in password)) and \
(not special or any(c in string.punctuation for c in password)):
return password
def main():
parser = argparse.ArgumentParser(description='Generate secure random passwords.')
parser.add_argument('-l', '--length', type=int, default=12, help='Password length')
parser.add_argument('--no-upper', action='store_false', help='Exclude uppercase letters')
parser.add_argument('--no-digits', action='store_false', help='Exclude digits')
parser.add_argument('--no-special', action='store_false', help='Exclude special characters')
parser.add_argument('-n', '--count', type=int, default=1, help='Number of passwords to generate')
args = parser.parse_args()
for _ in range(args.count):
print(generate_password(
length=args.length,
upper=not args.no_upper,
digits=not args.no_digits,
special=not args.no_special
))
if __name__ == '__main__':
main()
This Python script generates secure, random passwords with customizable complexity rules. Here’s why it’s useful:
secrets module (cryptographically secure random generator) instead of the standard random modulepassword_generator.py)python password_generator.pypython password_generator.py -l 16 -n 5python password_generator.py --no-specialpython password_generator.py --no-upper --no-digits --no-specialThis tool is particularly useful for system administrators, developers, and anyone who needs to create secure credentials while maintaining control over password complexity requirements.