Sie sind hier: Startseite /

Plain Text icon pwdgen3 — Plain Text, 2 KB (2450 bytes)


#!/usr/bin/env python
# (c) Copyright 2005-2007 by Hartmut Goebel <>

import random
import string
from string import ascii_letters, digits, punctuation

__transtab = string.maketrans('','')

ALLCHARS  = digits + ascii_letters + punctuation
SHELLSAVE = digits + ascii_letters + '+,-./:=?@[]^_{}~'

NOT_FONT_SAVE = '0O1l' # characters are hard to tell the difference between in some fonts

righthand = '23456qwertasdfgzxcvbQWERTASDFGZXCVB'
lefthand = '789yuiophjknmYUIPHJKLNM'
allchars = righthand + lefthand

def generate(length=6, shell_save=0, font_save=0, numeric=0,
    base = ALLCHARS
    if numeric:
        base = digits
        if shell_save:
            base = SHELLSAVE
        if font_save:
            base = base.translate(__transtab, NOT_FONT_SAVE)
    if random_length:
        length += random.randint(0, length)

    pwd = [ random.choice(base)
            for i in range(length) ]

    if  shell_save:
        while pwd[0] in punctuation:
            pwd.insert(0, random.choice(base))
        pwd = pwd[:length]

    return ''.join(pwd)

if __name__ == '__main__':
    from optparse import OptionParser
    parser = OptionParser('usage: %prog [options] url')
    parser.add_option('-S', '--shell-save', default=False,
                      action="store_true", dest='shell_save')
    parser.add_option('-N', '--numeric', default=False,
                      action="store_true", dest='numeric')
    parser.add_option('-F', '--font-save', default=False,
                      action="store_true", dest='font_save')
    parser.add_option('-n', '--length', default=8,
                      action="store", dest='length')
    parser.add_option('-r', '--random-length', default=False,
                      action="store_true", dest='random_length')
    parser.add_option('-R', '--no-random-length', default=False,
                      action="store_false", dest='random_length')

    options, args = parser.parse_args()
    if len(args) != 0:
        parser.error("Dieses Programm nimmt keine Argumente.")

    if options.numeric:
        if options.shell_save:
            parser.error("Nur eines von -S/--shell-save oder -N/--numeric angeben.")
        if options.font_save:
            parser.error("-F/--font-save geht nicht mit -N/--numeric")

    options.length = int(options.length)

    for i in range(10):
        print generate(**options.__dict__)