Last active
October 24, 2023 21:24
-
-
Save TheFlash2k/5de7957d9ded8dfb6c22b82c1256ae5f to your computer and use it in GitHub Desktop.
CSV: team_name,user_1,user_2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import random | |
import string | |
import csv | |
import os | |
from pprint import pprint | |
token = "<TOKEN_GOES_HERE>" | |
headers = { | |
"Authorization": f"Token {token}", | |
"Content-Type": "application/json" | |
} | |
base_ = "<URL>/<IP>" | |
url = f"http://{base_}/api/v1" | |
out_file = "passwords.csv" | |
def get_random_password(len : int = 20): | |
return ''.join(random.choice(string.ascii_letters + string.digits) for i in range(len)) | |
def get_users(): | |
users = [] | |
with open("teams.csv", "r") as f: | |
reader = csv.reader(f) | |
for row in reader: | |
users.append(row) | |
return_users = {} | |
for i in range(len(users)): | |
return_users[users[i][0].strip().title()] = { | |
"password": get_random_password(), | |
"members": { | |
f"{i.strip().title()}": get_random_password() for i in users[i][1:-1] if i != '' | |
}, | |
# "email" : users[i][-1] | |
} | |
return return_users | |
def is_team(team_name : str): | |
r = requests.get(f"{url}/teams", headers=headers) | |
data = r.json()['data'] | |
for i in data: | |
if i['name'] == team_name: | |
return True | |
return False | |
def get_team_id(team_name : str): | |
r = requests.get(f"{url}/teams", headers=headers) | |
data = r.json()['data'] | |
for i in data: | |
if i['name'] == team_name: | |
return i['id'] | |
return -1 | |
def get_user_id(user_name : str): | |
r = requests.get(f"{url}/users", headers=headers) | |
data = r.json()['data'] | |
for i in data: | |
if i['name'] == user_name: | |
return i['id'] | |
return -1 | |
if __name__ == "__main__": | |
users = get_users() | |
team_id = -1 | |
if not os.path.exists(out_file): | |
with open(out_file, "w") as f: | |
writer = csv.writer(f) | |
writer.writerow(["Team Name", "Team Password", "User1:Password", "User2:Password", "Email"]) | |
for team, data in users.items(): | |
users = data['members'] | |
team_pwd = data['password'] | |
print(f"Current Team: {team}") | |
print(f"Team Password: {team_pwd}") | |
print(f"Users: {users}") | |
if not is_team(team): | |
# Create the team | |
r = requests.post(f"{url}/teams", headers=headers, json={"name": team, "password": team_pwd}) | |
resp = r.json() | |
print(resp) | |
print(f"Team created => {resp['data']['name']} ({resp['data']['id']})") | |
team_id = resp['data']['id'] | |
if team_id == -1: | |
team_id = get_team_id(team) | |
if team_id == -1: | |
print("Something went wrong. Team ID not found. Name: ", team) | |
exit(1) | |
print(f"Team ID: {team_id}") | |
# Creating the users | |
for user, pwd in users.items(): | |
r = requests.post(f"{url}/users", headers=headers, json={"name": user, "password": pwd}) | |
resp = r.json() | |
# get user id | |
uid = resp['data']['id'] | |
print(f"User created => {resp['data']['name']} ({uid})") | |
r = requests.post(f"{url}/teams/{team_id}/members", headers=headers, json={"user_id": uid}) | |
resp = r.json() | |
print(resp) | |
print("Storing passwords in file => ") | |
# Storing the passwords and users in a csv | |
with open(out_file, "a") as f: | |
writer = csv.writer(f) | |
data = [] | |
for user, pwd in users.items(): | |
data.append(f"{user}:{pwd}") | |
writer.writerow([team, team_pwd, *data]) | |
print("=" * 15) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment