Preface
Random passwords are often used in operation and maintenance, such as server, Mysql, Redis and other password settings. Random passwords are relatively safe. Here we use Python to implement a random cipher generator
Recognizing random module
random module can randomly choose one character from multiple characters. We can put all the numbers into one string. random.choice can randomly select a number from them, as follows
>>> import random >>> random.choice('1234567890') '9' >>> random.choice('1234567890') '2'
Write a cyclic random generation
If we need to generate a 30-bit random password, including numbers, upper and lower case letters, special symbols. We can divide it into four kinds, and then randomly generate it and put it in the list. The program is as follows:
import random result = [] for i in range(0, 20): if i % 4 == 0: result.append(random.choice('1234567890')) if i % 4 == 1: result.append(random.choice('abcdefghijklmnopqrstuvwxyz')) if i % 4 == 2: result.append(random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ')) if i % 4 == 3: result.append(random.choice('!$%()+,-.:;>?@[]`{}')) print("".join(result))
The results are as follows:
[root@devops root]# python /tmp/shijiange.py 0iQ+3eC]5oA}0aJ@2mJ: [root@devops root]# python /tmp/shijiange.py 0qX>9cG-4pY`3tT.0gN` [root@devops root]# python /tmp/shijiange.py 1xN%3rG{6aA]0cD.2nL- [root@devops root]# python /tmp/shijiange.py 5jB]1xA]2yW,5vM:0bH%
Disrupt random passwords
Because of the cipher generated by the loop, the first is a number, the second is a lowercase letter, the third is a capital letter, and the fourth is a character. Although it's complicated, there's a rule that we can randomly scramble 20 letters.
import random result = [] for i in range(0, 20): if i % 4 == 0: result.append(random.choice('1234567890')) if i % 4 == 1: result.append(random.choice('abcdefghijklmnopqrstuvwxyz')) if i % 4 == 2: result.append(random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ')) if i % 4 == 3: result.append(random.choice('!$%()+,-.:;>?@[]`{}')) random.shuffle(result) print("".join(result))
Random passwords are generated. The results are as follows. Random passwords are generated in each run.
[root@devops ~]# python /tmp/shijiange.py h27;XMo$w;lpAQ7:J.08 [root@devops ~]# python /tmp/shijiange.py 1ptQ%8c:ED7C8[m$(7yD [root@devops ~]# python /tmp/shijiange.py `ri0+(CwP3Wd0P}-4Yj2 [root@devops ~]# python /tmp/shijiange.py qLcJ-F2>)S0K11{fq$q8
Following the practice of learning Python, and mastering Python technology from practice, you can listen to two episodes free of charge.
https://edu.51cto.com/sd/b72ab