From b4b2ab09cb40a94c919bfc29de461b25b307ed9c Mon Sep 17 00:00:00 2001 From: Natalia Serrano Date: Thu, 21 Nov 2024 16:07:30 +0100 Subject: [PATCH] refs #1166 add ogGetHostname() --- client/lib/engine/bin/NetLib.py | 57 +++++++++++++++------------------ 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/client/lib/engine/bin/NetLib.py b/client/lib/engine/bin/NetLib.py index d9e8908..4020c2e 100755 --- a/client/lib/engine/bin/NetLib.py +++ b/client/lib/engine/bin/NetLib.py @@ -1,3 +1,12 @@ +#/** +#@file Net.lib +#@brief Librería o clase Net +#@class Net +#@brief Funciones básicas de red. +#@version 1.0.6 +#@warning License: GNU GPLv3+ +#*/ + import subprocess import sys import os @@ -122,40 +131,26 @@ def ogGetGroupName(): #@return str_host - nombre de máquina #*/ ## def ogGetHostname(): - try: - # 1. Get hostname from the HOSTNAME environment variable - host = os.getenv("HOSTNAME", "").strip() +# Tomar nombre de la variable HOSTNAME + host = os.getenv("HOSTNAME", "").strip() + if host: return host - # 2. If not set, read from the DHCP leases file - if not host: - dhcp_file = "/var/lib/dhcp3/dhclient.leases" - if os.path.exists(dhcp_file): - with open(dhcp_file, "r") as f: - for line in f: - if 'option host-name' in line: - host = line.split('"')[1].strip(";") - break +# Si no, tomar del DHCP, opción host-name /* (comentario para Doxygen) + dhcp_file = "/var/lib/dhcp3/dhclient.leases" + if os.path.exists(dhcp_file): + with open(dhcp_file, "r") as f: + for line in f: + if 'option host-name' in line: + return line.split('"')[1].strip(";") - # 3. If still not set, read from kernel parameters in /proc/cmdline - if not host: - cmdline_file = "/proc/cmdline" - if os.path.exists(cmdline_file): - with open(cmdline_file, "r") as f: - for entry in f.read().split(): - if entry.startswith("hostname="): - host = entry.split("=")[1].strip() - break +# Si no, leer el parámetro del kernel hostname (comentario para Doxygen) */ + cmdline_file = "/proc/cmdline" + if os.path.exists(cmdline_file): + with open(cmdline_file, "r") as f: + for entry in f.read().split(): + if entry.startswith("hostname="): + return entry.split("=")[1].strip() - # 4. Update HOSTNAME environment variable if it differs - current_hostname = os.getenv("HOSTNAME", "") - if host and current_hostname != host: - os.environ["HOSTNAME"] = host - - return host if host else None - - except Exception as e: - print(f"Error in ogGetHostname: {e}") - return None #/** # ogGetIpAddress