From 3156426e8a8925e119e50b12fa19bfef8eff4c32 Mon Sep 17 00:00:00 2001 From: Hubert Cornet Date: Sat, 14 Feb 2026 10:42:25 +0100 Subject: [PATCH] Add scripts/hostname.py --- scripts/hostname.py | 58 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 scripts/hostname.py diff --git a/scripts/hostname.py b/scripts/hostname.py new file mode 100644 index 0000000..2ae2f23 --- /dev/null +++ b/scripts/hostname.py @@ -0,0 +1,58 @@ +import os +import string +import random +import xml.etree.ElementTree as ET +import logging +import socket + +def generate_random_string(length): + letters = string.ascii_lowercase + result_str = ''.join(random.choice(letters) for i in range(length)) + return result_str + +# Set up logging +logging.basicConfig(filename='/sdsat/logs/change_hostname.log', level=logging.INFO, format='%(asctime)s %(message)s') + +host = socket.gethostname() +print (host) +logging.info(f"Hostname recupere : {host}") + +if host == "sd-satxx.dtx.io" : + new_hostname = "DTX-SDSAT-" + generate_random_string(10) + logging.info('New hostname is: ' + new_hostname) + + # Parse the XML configuration file + + tree = ET.parse('/conf/config.xml') + + root = tree.getroot() + + + # Find the hostname element and update it + + for elem in root.iter('hostname'): + old_hostname = elem.text + elem.text = new_hostname + + + # Log the old and new hostname + + logging.info('Changed hostname from ' + str(old_hostname) + ' to ' + new_hostname) + + + # Save the modified XML configuration file + + tree.write('/conf/config.xml') + + + # Apply the new hostname immediately (this will require root privileges) + + os.system("sudo hostname " + new_hostname) + + + # Log the completion of the script + + logging.info('Hostname change completed successfully') + +else: + logging.info('No need to change, changed already YBT')