#!/usr/bin/env python3 import os import subprocess import sys import re # GUI import tkinter as tk from tkinter import messagebox, simpledialog def get_domain(): try: with open("/etc/resolv.conf", "r") as f: for line in f: if line.strip().startswith("search"): parts = line.split() if len(parts) > 1: return parts[1] except: pass return None def update_hosts_file(fqdn, short_hostname): hosts_path = "/etc/hosts" try: with open(hosts_path, "r") as f: lines = f.readlines() new_lines = [line for line in lines if not line.strip().startswith("127.0.")] new_lines.insert(0, f"127.0.1.1 {fqdn} {short_hostname}\n") new_lines.insert(0, "127.0.0.1 localhost localhost.localdomain\n") with open(hosts_path, "w") as f: f.writelines(new_lines) except Exception as e: raise Exception(f"Ошибка редактирования /etc/hosts: {e}") def update_fly_dmrc(domain): path = "/etc/X11/fly-dm/fly-dmrc" try: with open(path, "r") as f: lines = f.readlines() new_lines = [line for line in lines if not line.strip().startswith("PluginOptions=")] new_lines.append(f"PluginOptions=winbind.DefaultDomain={domain},winbind.Domains={domain}:\n") with open(path, "w") as f: f.writelines(new_lines) except Exception as e: print(f"Предупреждение: не удалось отредактировать fly-dmrc: {e}") def main(): root = tk.Tk() root.withdraw() try: if os.geteuid() != 0: messagebox.showerror("Ошибка", "Запустите скрипт с правами root (sudo)") sys.exit(1) domain = get_domain() if not domain: messagebox.showerror("Ошибка", "Не удалось определить домен из /etc/resolv.conf") sys.exit(1) if os.path.exists("/etc/krb5.keytab"): messagebox.showerror("Ошибка", "АРМ уже в домене. Сначала выведите его из домена.") sys.exit(1) hostname = simpledialog.askstring("Ввод", "Имя АРМ (короткое):") if not hostname: sys.exit(0) username = simpledialog.askstring("Ввод", "Имя пользователя для ввода в домен:") if not username: sys.exit(0) ### Ввод пароля (show='*' скрывает символы) password = simpledialog.askstring("Авторизация", f"Введите пароль для пользователя {username}:", show='*') if not password: sys.exit(0) if not hostname.strip() or not username.strip(): messagebox.showerror("Ошибка", "Поля не могут быть пустыми") sys.exit(1) fqdn = f"{hostname.strip()}.{domain}" if not messagebox.askyesno("Подтверждение", f"FQDN: {fqdn}\nДомен: {domain}\nПользователь: {username}\n\nПродолжить?"): sys.exit(0) if os.path.exists("/opt/config/ad_setup.sh"): subprocess.run("/opt/config/ad_setup.sh", check=False) subprocess.run(f"hostnamectl set-hostname {fqdn}", shell=True, check=True) update_hosts_file(fqdn, hostname.strip()) # adcli join (ПЕРЕДАЕМ ПАРОЛЬ ЧЕРЕЗ stdin) astra_version = "unknown" try: with open("/etc/astra_version", "r") as f: astra_version = f.read().strip() except: pass cmd = [ "adcli", "join", "--os-name=astralinux", f"--os-version={astra_version}", "-U", username, "-D", domain, "--stdin-password" ] # ВАЖНО: input=password передает пароль в stdin команды subprocess.run(cmd, input=password, text=True, check=True) update_fly_dmrc(domain) subprocess.run("systemctl enable sssd", shell=True, check=True) subprocess.run("systemctl restart sssd", shell=True, check=True) messagebox.showinfo("Успех", "АРМ успешно введен в домен и настроен.") except subprocess.CalledProcessError as e: messagebox.showerror("Ошибка", f"Команда завершилась с ошибкой.\nВозможно, неверный пароль или имя пользователя.\n\nКоманда: {e.cmd}") sys.exit(1) except Exception as e: messagebox.showerror("Ошибка", f"Произошла ошибка:\n{e}") sys.exit(1) finally: root.destroy() if __name__ == "__main__": main()