| 
853
 | 
     1 #!/usr/bin/env python
 | 
| 
 | 
     2 
 | 
| 
 | 
     3 """
 | 
| 
 | 
     4 password generator
 | 
| 
 | 
     5 """
 | 
| 
 | 
     6 
 | 
| 
 | 
     7 import argparse
 | 
| 
 | 
     8 import random
 | 
| 
 | 
     9 import string
 | 
| 
 | 
    10 import sys
 | 
| 
 | 
    11 
 | 
| 
 | 
    12 
 | 
| 
 | 
    13 def generate_password(population=string.ascii_letters+string.digits,
 | 
| 
 | 
    14                       length=12):
 | 
| 
 | 
    15     """
 | 
| 
 | 
    16     returns a random password `length` characters long
 | 
| 
 | 
    17     sampled from `population`
 | 
| 
 | 
    18     """
 | 
| 
 | 
    19 
 | 
| 
 | 
    20     return ''.join(random.sample(population, length))
 | 
| 
 | 
    21 
 | 
| 
 | 
    22 
 | 
| 
 | 
    23 def main(args=sys.argv[1:]):
 | 
| 
 | 
    24 
 | 
| 
 | 
    25     # parse command line
 | 
| 
 | 
    26     parser = argparse.ArgumentParser(description=__doc__)
 | 
| 
 | 
    27     parser.add_argument('-l', '--length', dest='length',
 | 
| 
 | 
    28                         type=int, default=12,
 | 
| 
 | 
    29                         help="length of password to generate [DEFAULT: %(default)s]")
 | 
| 
 | 
    30     options = parser.parse_args(args)
 | 
| 
 | 
    31 
 | 
| 
 | 
    32     # print generated password
 | 
| 
 | 
    33     print (generate_password(length=options.length))
 | 
| 
 | 
    34 
 | 
| 
 | 
    35 if __name__ == '__main__':
 | 
| 
 | 
    36     main()
 |