工作中經常要產生隨機密碼,參考了一些網上的做法,自己用python寫了個小指令碼,非常小巧。
#! /usr/bin/env python # -*- coding: utf-8 -*- #@author jinqinghua@gmail.com #@version 2012-11-07import stringfrom os import urandomfrom random import choice'''Linux正則命名參考:http://vbird.dic.ksu.edu.tw/linux_basic/0330regularex.php#lang[:alnum:]代表英文大小寫位元組及數字,亦即 0-9, A-Z, a-z[:alpha:]代表任何英文大小寫位元組,亦即 A-Z, a-z[:blank:]代表空白鍵與 [Tab] 按鍵兩者[:cntrl:]代表鍵盤上面的控制按鍵,亦即包括 CR, LF, Tab, Del.. 等等[:digit:]代表數字而已,亦即 0-9[:graph:]除了空白位元組 (空白鍵與 [Tab] 按鍵) 外的其他所有按鍵[:lower:]代表小寫位元組,亦即 a-z[:print:]代表任何可以被列印出來的位元組[:punct:]代表標點符號 (punctuation symbol),亦即:" ' ? ! ; : # $...[:upper:]代表大寫位元組,亦即 A-Z[:space:]任何會產生空白的位元組,包括空白鍵, [Tab], CR 等等[:xdigit:]代表 16 進位的數字類型,因此包括: 0-9, A-F, a-f 的數字與位元組Python內建常量(本例中改用這個,不用手工定義了)string.digits #十進位數字:0123456789string.octdigits #八位元字:01234567string.hexdigits #十六進位數字:0123456789abcdefABCDEFstring.ascii_lowercase #小寫字母(ASCII):abcdefghijklmnopqrstuvwxyzstring.ascii_uppercase #大寫字母(ASCII):ABCDEFGHIJKLMNOPQRSTUVWXYZstring.ascii_letters #字母:(ASCII)abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZstring.punctuation #標點符號:!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~以下的不用,有locale問題string.lowercase #abcdefghijklmnopqrstuvwxyzstring.uppercase #ABCDEFGHIJKLMNOPQRSTUVWXYZstring.letters #ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz以下的不能用string.whitespace #On most systems this includes the characters space, tab, linefeed, return, formfeed, and vertical tab.string.printable #digits, letters, punctuation, and whitespace'''#請在此設定您要產生的密碼需求passwd_length = 20 #密碼長度passwd_count = 100 #密碼個數passwd_seed = string.digits + string.ascii_letters + string.punctuation #密碼種子#passwd_seed = string.digits#passwd_seed = string.digits + string.ascii_lettersdef generate_passwd(): '''Function to generate a password''' passwd = [] while (len(passwd) < passwd_length): passwd.append(choice(passwd_seed)) return ''.join(passwd)def print_string_constants(): '''Test Case''' print string.digits print string.octdigits print string.hexdigits print string.ascii_lowercase print string.ascii_uppercase print string.ascii_letters print string.punctuation print string.lowercase print string.uppercase print string.letters print (string.printable + "ooo") print string.whitespace print '\n\n' if __name__ == '__main__': #print_string_constants() for i in range(0, passwd_count): print generate_passwd() #print "\ndone...python is great!"
密碼產生效果:
rWvR1MsPtso&9}K:.F[m<pgLB~EfD_N1bgkAlr\:*>,S;B5}_,MIlF894\z9o[lW{gOxF8aE5rR{b=*}kC4VedLzt>HaZ12)_c+k88f@Gqto8.I[EiKh!TnI0Rcf:#[SQd0G$lHh1HX~6VL<Vcu^E?]+5|!VpMRm-MqkDuMFt8Y-FvIl&s<WH)J]@@y%}3]~oOBy#8'D49=+Ct;GCrpWx%iJ2(h8f&GU9(;1zrEuvw"y"SK7{JsiN:5*q<VEp_i>Y(0<oEC<P3&Z3Q=l6]RKivB&W,]p,4rG%#DgYMWN5dMStxw0MLG<.p'!"T+4rK*0zf3zaaJ4lN8+_Gru9I6{g)w%B~l3TQqJn:VE)<>cpk^~;bmOP-[1wb%O?~W}Oh_[u6Lb"R2*bD^C|~oo;b\UDBm/~S|}Z~OwN@QX79aZ/SGT:QDsRX6h/J?MXvC~KLyUJ"SY6P"$g.j0dJ,Q(+vaPW":Ny.a*SwG!<;?&Ll[qXdu%`\e$P/0~l\ef(~9~1?:KhO_@`H}QR&m!\?g"^0LNoOlTVnmfl3V>}oe-"$Is9[dd#!J*#&4)_UXCz;n-u-X&euB5A]E&W~{EG_H$xfm,<rck,@F^eZYgSN/_w(~->K|H1n[gK%9ElbQg!-CsY;lp3mgSw7{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