-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathadconnection_gui.py
More file actions
63 lines (49 loc) · 2.05 KB
/
adconnection_gui.py
File metadata and controls
63 lines (49 loc) · 2.05 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
#!/usr/bin/env python3
"""Simple Tkinter GUI to join a Linux host to Active Directory."""
import subprocess
import tkinter as tk
from tkinter import messagebox
def run_cmd(cmd):
"""Run a shell command and return output and exit code."""
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
return result.stdout.strip(), result.returncode
def discover_domain():
"""Try to auto-discover the domain using realm."""
out, _ = run_cmd("realm discover 2>/dev/null | awk '/realm.name/ {print $2; exit}'")
return out
def join_domain(domain, admin_user, ou):
cmd = ["realm", "join", "-v", f"--user={admin_user}"]
if ou:
cmd.append(f"--computer-ou={ou}")
cmd.append(domain)
return run_cmd(" ".join(cmd))
def on_join():
domain = domain_var.get().strip()
user = user_var.get().strip()
ou = ou_var.get().strip()
if not domain:
messagebox.showerror("Error", "Domain is required")
return
if not user:
messagebox.showerror("Error", "Admin user is required")
return
output, code = join_domain(domain, user, ou)
if code == 0:
messagebox.showinfo("Success", f"Successfully joined {domain}")
else:
messagebox.showerror("Join Failed", output or "Unknown error")
root = tk.Tk()
root.title("AD Connection")
# Variables
domain_var = tk.StringVar(value=discover_domain() or "")
user_var = tk.StringVar()
ou_var = tk.StringVar()
# Layout
tk.Label(root, text="Domain:").grid(row=0, column=0, sticky="e", padx=5, pady=5)
tk.Entry(root, textvariable=domain_var, width=40).grid(row=0, column=1, padx=5, pady=5)
tk.Label(root, text="Admin User:").grid(row=1, column=0, sticky="e", padx=5, pady=5)
tk.Entry(root, textvariable=user_var, width=40).grid(row=1, column=1, padx=5, pady=5)
tk.Label(root, text="Computer OU:").grid(row=2, column=0, sticky="e", padx=5, pady=5)
tk.Entry(root, textvariable=ou_var, width=40).grid(row=2, column=1, padx=5, pady=5)
tk.Button(root, text="Join Domain", command=on_join).grid(row=3, column=0, columnspan=2, pady=10)
root.mainloop()