This commit is contained in:
58
scripts/hostname.py
Normal file
58
scripts/hostname.py
Normal file
@@ -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')
|
||||||
Reference in New Issue
Block a user