From ca3291bcb60a4c63c8d3aed41e0b8bc4d44566f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=9F=E9=85=8C=20=E9=B5=AC=E5=85=84?= Date: Thu, 23 Jan 2014 20:54:15 +0800 Subject: [PATCH] Added pwgen --- python/pwgen.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 python/pwgen.py diff --git a/python/pwgen.py b/python/pwgen.py new file mode 100644 index 0000000..b48832d --- /dev/null +++ b/python/pwgen.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +import string; +import random; + +import os; +import sys; + + +if len(sys.argv) < 2: + sys.exit('Usage: ' + os.path.basename(__file__) + ' length'); + +l = sys.argv[1]; + +if not l.isdigit(): + sys.exit('"' + l + '" is not a valid length.'); + +# cast l into intergers +l = int(l); + +# define the character set +charSet = string.ascii_letters + string.digits; + +# generate stack +out_stack = ''.join(random.choice(charSet) for x in range(l)); + +# Am I being piped? +if sys.stdout.isatty(): + print(out_stack); +else: + sys.stdout.write(out_stack); +