From 5214c3697baac78bd0a465e33d6f4f3c1e30862e Mon Sep 17 00:00:00 2001 From: zkk <1007518571@qq.com> Date: Tue, 24 Dec 2024 10:57:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AE=BE=E7=BD=AEhostname?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ks_includes/ModelConfig.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ks_includes/ModelConfig.py b/ks_includes/ModelConfig.py index 1ec978d3..0ef3bb5d 100644 --- a/ks_includes/ModelConfig.py +++ b/ks_includes/ModelConfig.py @@ -131,6 +131,17 @@ class ModelConfig: except Exception as e: logging.error(f"Copy error printer file: {e.stderr}") + def wirte_hostname(self, device_name): + + try: + current_hostname = subprocess.check_output(["hostname"], text=True).strip() + logging.info(f"Current hostname: {current_hostname}") + subprocess.run(["hostnamectl", "set-hostname", device_name], check=True) + logging.info(f"Hostname has been changed to: {device_name}") + except subprocess.CalledProcessError as e: + logging.error(f"Error while executing command: {e}") + except Exception as e: + logging.error(f"An unexpected error occurred: {e}") def generate_config(self, model): model_name = model @@ -139,6 +150,7 @@ class ModelConfig: self.write_mdns_config(device_name) self.write_device_name_config(device_name) self.wirte_printer_config(model) + self.wirte_hostname(device_name) os.system("systemctl restart klipper.service") os.system("systemctl restart moonraker.service") os.system("systemctl restart KlipperScreen.service")