Quick Tip: Generate Random Passwords Using Python

Posted September 11, 2009 @ 7:32pm by Phil

I recently moved some of my other sites that were being hosted on a Linux box in my room to a new web host (I will write a more detailed article on this later on). With that move I have had to generate several new passwords for the ftp accounts and MySQL databases. For this purpose I have created the following Python script to generate random passwords of the specified length.

import sys
import string
import random

if len(sys.argv) != 2:
  print "Syntax: python pass_gen.py [length]"
  sys.exit()

n = int(sys.argv[1])

chars = string.lowercase + string.uppercase + string.digits

print ''.join([random.choice(chars) for i in range(0, n)])

Download Source: pass_gen.py

Tags: Python, Quick Tip