Welcome, guest | Sign In | My Account | Store | Cart

A short, readable password generator that can be launched from the command line. Just launch it from the shell and it will print out an 8-character password. You can also specify the length and whether the password should be typed with alternating hands on a qwerty keyboard.

Python, 39 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
"""
A simple script for making random passwords, WITHOUT 1,l,O,0.  Because
those characters are hard to tell the difference between in some fonts.

"""

#Import Modules
import sys
from random import Random

rng = Random()

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

try:
 passwordLength = int(sys.argv[1])
except:
 #user didn't specify a length.  that's ok, just use 8
 passwordLength = 8
try:
 alternate_hands = sys.argv[2] == 'alt'
 if not alternate_hands:
  print "USAGE:"
  print sys.argv[0], "[length of password]",
  print "[alt (if you want the password to alternate hands]"
except:
 alternate_hands = False

for i in range(passwordLength):
 if not alternate_hands:
  sys.stdout.write( rng.choice(allchars) )
 else:
  if i%2:
   sys.stdout.write( rng.choice(lefthand) )
  else:
   sys.stdout.write( rng.choice(righthand) )

I regularly need to generate new passwords and give them to other people. Particular requirements are to not use the characters 1,l,0, and O because they can easily be confused, depending on the font used to display them. Also, it is usually beneficial if the order of the characters allow the user to alternate hands when typing in the password.

3 comments

Martin Jenkins 13 years, 9 months ago  # | flag

TCL version of password generator. Thanks, most useful.

Here's straight conversion to tcl

#!/usr/local/bin/tclsh

set righthand "23456qwertasdfgzxcvbQWERTASDFGZXCVB"
set lefthand "789yuiophjknmYUIPHJKLNM"
set allchars $righthand$lefthand

set passwordLength 8
set password ""

for {set i 0} { $i Thanks, most useful.

Here's straight conversion to tcl

<pre>
#!/usr/local/bin/tclsh

set righthand "23456qwertasdfgzxcvbQWERTASDFGZXCVB"
set lefthand "789yuiophjknmYUIPHJKLNM"
set allchars $righthand$lefthand

set passwordLength 8
set password ""

for {set i 0} { $i

</pre>

Anton Angelo 12 years, 3 months ago  # | flag

I modded a little to output any number of passwords (I needed 70,000).

#!/usr/bin/env python
"""
A simple script for making random passwords, WITHOUT 1,l,O,0.  Because
those characters are hard to tell the difference between in some fonts.

"""

#Import Modules
import sys    
from random import Random

rng = Random()

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

try:
 passwordLength = int(sys.argv[1])
except:
 #user didn't specify a length.  that's ok, just use 8
 passwordLength = 8
try:
 number_of_passwords = int(sys.argv[2])
except:
 # we only want one
 number_of_passwords = 1

try:
 alternate_hands = sys.argv[3] == 'alt'
 if not alternate_hands:
  print "USAGE:"
  print sys.argv[0], "[length of password]",
  print "[number of passwords required]",
  print "[alt (if you want the password to alternate hands)]"
except:
 alternate_hands = False


for i in range(number_of_passwords):
 for j in range(passwordLength):
  if not alternate_hands:
   sys.stdout.write( rng.choice(allchars) )
  else:
   if j%2:
    sys.stdout.write( rng.choice(lefthand) )
   else:
    sys.stdout.write( rng.choice(righthand) )
 sys.stdout.write("\n")
amir naghavi 10 years, 5 months ago  # | flag

it is better to use random.SystemRandom for creating passwords.

Created by shandy on Mon, 6 Feb 2006 (PSF)
Python recipes (4591)
shandy's recipes (1)

Required Modules

Other Information and Tasks