-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestrict.py
More file actions
92 lines (71 loc) · 2.41 KB
/
Copy pathrestrict.py
File metadata and controls
92 lines (71 loc) · 2.41 KB
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
#!/usr/bin/env python3
import csv
import os
import sys
from concurrent.futures import ThreadPoolExecutor, as_completed
import paramiko
STUDENTS_FILE = "students.csv"
TARGETS_FILE = "eval_targets.txt"
NEW_PASSWORD = "restricted"
def change_password(student):
name = student["name"]
urn = student["urn"]
ip = student["ip"]
username = student["username"]
password = student["password"]
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(
hostname=ip,
username=username,
password=password,
timeout=15,
allow_agent=False,
look_for_keys=False,
)
_, stdout, _ = client.exec_command(
f"echo '{username}:{NEW_PASSWORD}' | sudo chpasswd && echo OK || echo FAIL"
)
result = stdout.read().decode().strip()
client.close()
print(f" {'Done ' if result == 'OK' else 'FAIL '} {urn} ({name}) at {ip}")
except Exception as e:
print(f" ERROR {urn} ({name}) at {ip} → {e}")
def load_students(targets=None):
students = []
with open(STUDENTS_FILE) as f:
for row in csv.DictReader(f):
if targets is None or row["urn"] in targets:
students.append(row)
return students
def load_targets():
if not os.path.exists(TARGETS_FILE):
return None
with open(TARGETS_FILE) as f:
return {line.strip() for line in f if line.strip()}
def main():
run_all = "--all" in sys.argv
if run_all:
students = load_students()
print(f"Restricting ALL {len(students)} instance(s)...")
else:
targets = load_targets()
if not targets:
print(
f"Error: {TARGETS_FILE} is empty. Use --all to restrict all instances."
)
sys.exit(1)
students = load_students(targets)
print(f"Restricting {len(students)} targeted instance(s)...")
if not students:
print("No matching students found.")
sys.exit(1)
print()
with ThreadPoolExecutor(max_workers=len(students)) as executor:
futures = {executor.submit(change_password, s): s for s in students}
for future in as_completed(futures):
future.result()
print("\nDone. Password changed to 'restricted' on all targeted instances.")
if __name__ == "__main__":
main()