-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.py
executable file
·64 lines (53 loc) · 1.88 KB
/
users.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import random
import string
import argparse
from pathlib import Path
from passlib.hash import sha512_crypt
passwd_folder = Path("jupyterhub/passwords")
def get_users(file: str) -> list:
user_file: str = file
user_list = []
with open(user_file, 'r') as f:
for line in f:
user = line.strip('\n')
user_list.append(user)
return user_list
def randomString(stringLength=10):
"""Generate a random string of fixed length """
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(stringLength))
def generate_list(user_list):
wrapped_user_list = []
for user in user_list:
wrapped_user_list.append(f"\"{user}\"")
user_str = ", ".join(wrapped_user_list)
print(user_str)
def generate_passwd(user_list, force=False):
passwd_dict = {}
for user in user_list:
passwd_dict[user] = randomString(20)
pass_file = f"{user}.password"
if force or (not os.path.isfile(passwd_folder / pass_file)) :
with open(passwd_folder / pass_file, 'w') as f:
f.write(passwd_dict[user])
print(f"password for {user} was created in the {passwd_folder/pass_file}")
with open(passwd_folder / f"{pass_file}_hashed", 'w') as f:
f.write(sha512_crypt.using(rounds=5000).hash(passwd_dict[user]))
else:
print(f"Pass for {user} exists in the {passwd_folder / pass_file}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Give me files with users")
parser.add_argument('--file', help='Specify file with users')
parser.add_argument('--passwd', help='generate pass', action="store_true")
parser.add_argument('--force', help='Force generate pass', action="store_true")
parser.add_argument('--user', help="user")
args = parser.parse_args()
if args.file:
user_list = get_users(args.file)
if args.user:
user_list = [str(args.user)]
if args.file:
generate_list(user_list)
if args.passwd:
generate_passwd(user_list, args.force)