forked from Twingate-Labs/Twingate-CLI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal90-showuser.py
executable file
·132 lines (93 loc) · 4.26 KB
/
final90-showuser.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
#!/usr/bin/env python3
import csv
import subprocess
import os
import random
import string
import re
import secrets
import json
import argparse
import time
import datetime
from datetime import datetime
#Twingate-CLI
logintenat = os.environ["TG_TENANT"]
loginapi = os.environ["TG_API"]
loginoutput = subprocess.check_output('python3 ./tgcli.py auth login -t ' + logintenat + ' -a ' + loginapi, shell=True)
session = loginoutput.decode("utf-8").split(":")[1].strip()
#print(f"Session created: {session}")
datainputjson = ["python3", "./tgcli.py", "-s", session, "device", "list"]
subprocess.call(datainputjson, stdout=subprocess.DEVNULL)
output = subprocess.check_output(datainputjson, encoding='UTF-8')
json_data = json.loads(output)
#json_data = json.loads(output.decode("utf-8"))
# Print the JSON data
#print(json.dumps(json_data, indent=4))
def check_timestamp(timestamp):
created_at = datetime.datetime.fromisoformat(timestamp)
# Convert the timestamp to UTC if it's not already in UTC
if created_at.tzinfo is None:
created_at = created_at.replace(tzinfo=datetime.timezone.utc)
time_diff = datetime.datetime.now(datetime.timezone.utc) - created_at
seconds_diff = time_diff.total_seconds()
#return seconds_diff > 24 * 60 * 60
return seconds_diff > 30 * 24 * 60 * 60
# Loop through each element in the outer list
for element in json_data:
for item in element:
# Access the nested "node" dictionary
node = item["node"]
#print(element)
## Extract and print the "createdAt" value
#createdat = node["createdAt"]
id = node["id"]
email = node["user"]["email"]
state = node["activeState"]
slogin = node["lastSuccessfulLoginAt"]
#print(f"ID: {id} {email} {state} {slogin}")
if state == "ACTIVE":
print(f"ID: {id} EMAIL: {email} STATE: {state} DATE: {slogin}")
#print(id, email ,state, slogin)
print(f"_____________________________")
from datetime import datetime, timedelta, timezone
def get_newest_record_per_email(json_data):
"""
Finds the newest record for each unique email address in the given JSON data.
Args:
json_data: A list of dictionaries, where each dictionary represents a record.
Returns:
A list of dictionaries containing the newest record for each email address.
"""
newest_records = {}
days_ago = datetime.now(timezone.utc) - timedelta(days=90)
for element in json_data:
for item in element:
node = item["node"]
id = node["id"]
email = node["user"]["email"]
state = node["activeState"]
slogin = node["lastSuccessfulLoginAt"]
if state == "ACTIVE":
try:
date = datetime.fromisoformat(slogin)
except ValueError:
print(f"Invalid date format for email: {email}")
continue
# Initialize record with default values for new emails
if email not in newest_records:
newest_records[email] = {'ID': id, 'EMAIL': email, 'STATE': state, 'DATE': date}
elif date > newest_records[email]['DATE']:
newest_records[email] = {'ID': id, 'EMAIL': email, 'STATE': state, 'DATE': date}
# Filter out records newer than 90 days
filtered_records = [record for record in newest_records.values() if record['DATE'] < days_ago]
return filtered_records
newest_records = get_newest_record_per_email(json_data)
for record in newest_records:
print(f"ID: {record['ID']}, EMAIL: {record['EMAIL']}, STATE: {record['STATE']}, DATE: {record['DATE']}")
print(f"****************************")
animals = ['BlueFly', 'BlackEel', 'RedBoa', 'BlackBat', 'BlackBoa', 'OrangeFox', 'OrangeApe', 'GreenApe', 'WhiteApe', 'PurpleElk', 'RedCow', 'GreenFox', 'YellowFox', 'PinkBoa', 'YellowElk', 'PinkFox', 'GreenBoa', 'RedBat', 'PurpleApe', 'OrangeBat', 'YellowEel', 'OrangeYak', 'RedDog', 'PinkEel', 'PurpleBat', 'OrangeElk', 'BlueBoa', 'OrangeEel', 'GreenCat', 'WhiteDog', 'OrangeCat', 'BlueCat', 'YellowCat', 'GreenCow', 'BlackYak', 'RedCat', 'WhiteFox']
# Print the list
#for animal in animals:
# subprocess.call(["python3", "./tgcli.py", "auth", "logout", "-s", animal])
subprocess.call(["python3", "./tgcli.py", "auth", "logout", "-s", session])