Skip to content

Instantly share code, notes, and snippets.

@esammer
Created December 28, 2010 21:39
Show Gist options
  • Save esammer/757763 to your computer and use it in GitHub Desktop.
Save esammer/757763 to your computer and use it in GitHub Desktop.
A utility to generate passwords that conform to some standards
#!/usr/bin/env python
import random
import string
from optparse import OptionParser
class GenPassword:
_CHARSET = string.letters + string.digits
_SPECIALS = '!@#$%^&*()_+-=,.'
def __init__(self):
self._count = 10
self._length = 8
self._specials = 1
def parse_options(self):
parser = OptionParser()
parser.add_option('-c', '--count', dest = 'count', help = 'number of passwords to generate')
parser.add_option('-l', '--length', dest = 'length', help = 'number of passwords to generate')
parser.add_option('-s', '--specials', dest = 'specials', help = 'number of special characters')
(options, args) = parser.parse_args()
if (options.count):
self._count = int(options.count)
if (options.length):
self._length = int(options.length)
if (options.specials):
self._specials = int(options.specials)
print options
def generate(self):
password = ''
special_count = 0
while len(password) < self._length:
if special_count < self._specials and random.randint(0, self._length - len(password)) == 0:
source_set = GenPassword._SPECIALS
special_count = special_count + 1
else:
source_set = GenPassword._CHARSET
password = password + random.choice(source_set)
return password
def generate_group(self):
group = [ ]
for i in range(0, self._count):
group.append(self.generate())
return group
if __name__ == '__main__':
app = GenPassword()
try:
app.parse_options()
print "\n".join(app.generate_group())
except Exception as e:
print "Error:", e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment