-
Notifications
You must be signed in to change notification settings - Fork 1
/
creation.py
executable file
·148 lines (134 loc) · 4.92 KB
/
creation.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import subprocess
import concurrent.futures
import time
def createuserThreading(user):
print(f"\nCreation Of User {user[2]} ,with id {user[0]} ...")
subprocess.call(f'useradd -u {user[0]} -c "{user[1]}" {user[2]}', shell=True)
return f"{user[2]} created!"
def askcreateuser():
while True:
try:
ask = str(input("\nCreate More Users [Y/N] : "))
except TypeError:
print("\n[DATATYPE ERROR] try again! ")
continue
except ValueError:
print("\n[VALUE ERROR] try again! ")
continue
except:
print("\n[ERROR] try again! ")
continue
else:
if ask.lower() == 'y':
createuser()
continue
elif ask.lower() == 'n':
print("END.")
break
else:
print("\n[WRONG VALUE] try again! ")
continue
def createuser():
no_duplication = False
while True:
try:
users_number = int(input("How Many Users You Want To Create : "))
except TypeError:
print("\n[DATATYPE ERROR] try again! ")
continue
except ValueError:
print("\n[VALUE ERROR] try again! ")
continue
except:
print("\n[ERROR] try again! ")
continue
else:
if users_number == 1:
no_duplication = True
break
while True:
if no_duplication:
duplicate_user = 'n'
break
else:
try:
duplicate_user = str(input("Do You Want To Duplicate The Same Parameters For All Users [Y/N] : "))
except TypeError:
print("\n[DATATYPE ERROR] try again! ")
continue
except ValueError:
print("\n[VALUE ERROR] try again! ")
continue
except:
print("\n[ERROR] try again! ")
continue
else:
if duplicate_user.lower() == 'y':
break
elif duplicate_user.lower() == 'n':
break
else:
print("\n[WRONG VALUE] try again! ")
continue
initial_increment = False
list_of_users_to_create=[]
for i in range(1, users_number + 1):
if duplicate_user.lower() == 'y':
while initial_increment == False:
while True:
print("\nParameters For All Users : ")
try:
username = str(
input("username (username will be genereate like this username1,username2... ) : "))
userid = int(input("range beginning user-id : "))
comment = str(input("comment (comments will be generated like this comment1,comment2... ) : "))
except TypeError:
print("\n[DATATYPE ERROR] try again! ")
continue
except ValueError:
print("\n[VALUE ERROR] try again! ")
continue
except:
print("\n[ERROR] try again! ")
continue
else:
initial_increment = True
break
username_u = username + str(i)
comment_u = comment + str(i)
list_of_users_to_create.append((userid,comment_u,username_u))
userid = userid + 1
continue
elif duplicate_user.lower() == 'n':
while True:
print(f"\nCreation Of User Number {i} ... ")
try:
username = str(input("username : "))
userid = int(input("user-id : "))
comment = str(input("comment : "))
except TypeError:
print("\n[DATATYPE ERROR] try again! ")
continue
except ValueError:
print("\n[VALUE ERROR] try again! ")
continue
except:
print("\n[ERROR] try again! ")
continue
else:
subprocess.call(f'useradd -u {userid} -c "{comment}" {username}', shell=True)
print("success!")
break
else:
print("\n[WRONG VALUE] try again! ")
break
if duplicate_user.lower() == 'y':
start = time.perf_counter()
with concurrent.futures.ThreadPoolExecutor() as executor:
results = executor.map(createuserThreading, list_of_users_to_create)
for result in results:
print(result)
finish = time.perf_counter()
print(f'Finished in {round(finish - start, 2)} second(s)')
if __name__ == '__main__':
askcreateuser()