annotate python/password.py @ 921:02b31a76e2c4

get DEST from command line
author Jeff Hammel <k0scist@gmail.com>
date Sun, 02 Jun 2024 16:35:01 -0700
parents 5e05c41337e3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
853
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 password generator
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 """
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 import argparse
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import random
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import string
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import sys
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 def generate_password(population=string.ascii_letters+string.digits,
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 length=12):
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 """
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 returns a random password `length` characters long
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 sampled from `population`
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 """
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 return ''.join(random.sample(population, length))
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 def main(args=sys.argv[1:]):
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 # parse command line
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 parser = argparse.ArgumentParser(description=__doc__)
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 parser.add_argument('-l', '--length', dest='length',
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 type=int, default=12,
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 help="length of password to generate [DEFAULT: %(default)s]")
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 options = parser.parse_args(args)
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 # print generated password
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 print (generate_password(length=options.length))
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 if __name__ == '__main__':
5e05c41337e3 Example: generate passwords
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 main()