Added pwgen

This commit is contained in:
斟酌 鵬兄 2014-01-23 20:54:15 +08:00
parent 8318550036
commit ca3291bcb6

32
python/pwgen.py Normal file
View File

@ -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);