Skip to content

Commit ee16048

Browse files
committed
Add Tkinter GUI for AD connection
1 parent a6b4c6a commit ee16048

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ Usage of the script:
7171
for ./ADconnection.sh do a
7272

7373
sudo chmod +x ADconnection.sh
74+
You can also use the Python version:
75+
sudo python3 adconnection_app.py [domain] [--user ADMIN] [--ou OU]
76+
sudo python3 adconnection_gui.py
77+
7478

7579

7680
# Complete steps

adconnection_app.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
import argparse
3+
import subprocess
4+
import sys
5+
from pathlib import Path
6+
7+
8+
def run_cmd(cmd):
9+
try:
10+
result = subprocess.run(cmd, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
11+
return result.stdout.strip()
12+
except subprocess.CalledProcessError as e:
13+
print(e.output)
14+
sys.exit(e.returncode)
15+
16+
17+
def discover_domain():
18+
"""Attempt to discover the AD domain using realm"""
19+
try:
20+
output = run_cmd("realm discover 2>/dev/null | awk '/realm.name/ {print $2; exit}'")
21+
return output if output else None
22+
except SystemExit:
23+
return None
24+
25+
26+
def join_domain(domain, admin_user, ou=None):
27+
cmd = ["realm", "join", "-v", f"--user={admin_user}"]
28+
if ou:
29+
cmd.append(f"--computer-ou={ou}")
30+
cmd.append(domain)
31+
run_cmd(" ".join(cmd))
32+
33+
34+
def main():
35+
parser = argparse.ArgumentParser(description="Join Linux host to Active Directory")
36+
parser.add_argument("domain", nargs="?", help="Domain to join")
37+
parser.add_argument("-u", "--user", required=False, help="Admin user for the join")
38+
parser.add_argument("-o", "--ou", help="OU for computer object")
39+
parser.add_argument("--discover", action="store_true", help="Only discover domain")
40+
args = parser.parse_args()
41+
42+
if args.discover:
43+
domain = discover_domain()
44+
if domain:
45+
print(domain)
46+
else:
47+
print("No domain discovered")
48+
return
49+
50+
domain = args.domain or discover_domain()
51+
if not domain:
52+
print("Domain could not be discovered. Please specify the domain as argument.")
53+
sys.exit(1)
54+
55+
admin_user = args.user or input("Admin user: ")
56+
join_domain(domain, admin_user, args.ou)
57+
print(f"Successfully joined {domain}")
58+
59+
60+
if __name__ == "__main__":
61+
main()
62+

adconnection_gui.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

Comments
 (0)