|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Simple Tkinter GUI to join a Linux host to Active Directory.""" |
| 3 | +import subprocess |
| 4 | +import tkinter as tk |
| 5 | +from tkinter import messagebox |
| 6 | + |
| 7 | + |
| 8 | +def run_cmd(cmd): |
| 9 | + """Run a shell command and return output and exit code.""" |
| 10 | + result = subprocess.run(cmd, shell=True, capture_output=True, text=True) |
| 11 | + return result.stdout.strip(), result.returncode |
| 12 | + |
| 13 | + |
| 14 | +def discover_domain(): |
| 15 | + """Try to auto-discover the domain using realm.""" |
| 16 | + out, _ = run_cmd("realm discover 2>/dev/null | awk '/realm.name/ {print $2; exit}'") |
| 17 | + return out |
| 18 | + |
| 19 | + |
| 20 | +def join_domain(domain, admin_user, ou): |
| 21 | + cmd = ["realm", "join", "-v", f"--user={admin_user}"] |
| 22 | + if ou: |
| 23 | + cmd.append(f"--computer-ou={ou}") |
| 24 | + cmd.append(domain) |
| 25 | + return run_cmd(" ".join(cmd)) |
| 26 | + |
| 27 | + |
| 28 | +def on_join(): |
| 29 | + domain = domain_var.get().strip() |
| 30 | + user = user_var.get().strip() |
| 31 | + ou = ou_var.get().strip() |
| 32 | + if not domain: |
| 33 | + messagebox.showerror("Error", "Domain is required") |
| 34 | + return |
| 35 | + if not user: |
| 36 | + messagebox.showerror("Error", "Admin user is required") |
| 37 | + return |
| 38 | + output, code = join_domain(domain, user, ou) |
| 39 | + if code == 0: |
| 40 | + messagebox.showinfo("Success", f"Successfully joined {domain}") |
| 41 | + else: |
| 42 | + messagebox.showerror("Join Failed", output or "Unknown error") |
| 43 | + |
| 44 | + |
| 45 | +root = tk.Tk() |
| 46 | +root.title("AD Connection") |
| 47 | + |
| 48 | +# Variables |
| 49 | +domain_var = tk.StringVar(value=discover_domain() or "") |
| 50 | +user_var = tk.StringVar() |
| 51 | +ou_var = tk.StringVar() |
| 52 | + |
| 53 | +# Layout |
| 54 | + |
| 55 | +tk.Label(root, text="Domain:").grid(row=0, column=0, sticky="e", padx=5, pady=5) |
| 56 | +tk.Entry(root, textvariable=domain_var, width=40).grid(row=0, column=1, padx=5, pady=5) |
| 57 | +tk.Label(root, text="Admin User:").grid(row=1, column=0, sticky="e", padx=5, pady=5) |
| 58 | +tk.Entry(root, textvariable=user_var, width=40).grid(row=1, column=1, padx=5, pady=5) |
| 59 | +tk.Label(root, text="Computer OU:").grid(row=2, column=0, sticky="e", padx=5, pady=5) |
| 60 | +tk.Entry(root, textvariable=ou_var, width=40).grid(row=2, column=1, padx=5, pady=5) |
| 61 | +tk.Button(root, text="Join Domain", command=on_join).grid(row=3, column=0, columnspan=2, pady=10) |
| 62 | + |
| 63 | +root.mainloop() |
0 commit comments