随机生成指定位数密码包括大小写数字特殊符号

分别用python和bash实现

python1

import string
from random import choice
for i in range(10):
    print(''.join([choice(string.ascii_letters + string.digits + string.punctuation) for i in range(12)]))

python2

In [1]: import random
In [2]: def password(length):
   ...:     pw = str()
   ...:     characters = "qwertyuiopasdfghjklzxcvbnm" + "1234567890"+"!@#$%^&*,."
   ...:     for i in range(length):
   ...:         pw = pw +  random.choice(characters)
   ...:     return pw
   ...:
   ...:
In [3]: password(8)
Out[3]: '#!c6!gcd'

bash

#!/bin/bash

for (( i=1;i<=10;i++))
do
  mkpasswd -l 12 -d 1 -c 1 -C 1 -s 1

done

------------------------------------------------------------------

bash

mkpasswd 的使用 常用的选项, -l 指定 长度

-d 指定 数字的个数


-c 指定 小写字符个数 -C 指定大写字符个数


-s 指定特殊字符个数


usage: mkpasswd [args] [user]


where arguments are:


-l (length of password, default = 7)


指定密码的长度,默认是7位数


-d (min # of digits, default = 2)


指定密码中数字最少位数,默认是2位


-c (min # of lowercase chars, default = 2)


指定密码中小写字母最少位数,默认是2位


-C (min # of uppercase chars, default = 2)


指定密码中大写字母最少位数,默认是2位


-s (min # of special chars, default = 1)


指定密码中特殊字符最少位数,默认是1位


-v (verbose, show passwd interaction)

这个参数在实验的时候报错,具体不知道。
原文地址:https://www.cnblogs.com/soymilk2019/p/15072208.html