refs #1144 rebuild ogGetHostname

pull/1/head
Antonio Guerrero 2024-11-14 07:50:51 -06:00
parent 8db98b1289
commit bc3d68e8fb
1 changed files with 29 additions and 33 deletions

View File

@ -7,8 +7,6 @@ import ogGlobals
import SystemLib
import FileLib
#/**
# ogChangeRepo IPREPO [ OgUnit ]
#@brief Cambia el repositorio para el recurso remoto images.
@ -110,44 +108,46 @@ def ogGetGroupName():
print(f"Error in ogGetGroupName: {e}")
return None
#/**
# ogGetHostname
#@brief Muestra el nombre del cliente.
#@return str_host - nombre de máquina
#*/ ##
def ogGetHostname():
HOST = ""
try:
# 1. Get hostname from the HOSTNAME environment variable
host = os.getenv("HOSTNAME", "").strip()
if len(sys.argv) >= 2 and sys.argv[1] == "help":
SystemLib.ogHelp("ogGetHostname", "ogGetHostname", "ogGetHostname => pc1")
return
# 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
# Tomar nombre de la variable HOSTNAME
HOST = os.getenv("HOSTNAME")
# 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, tomar del DHCP, opción host-name
if not HOST:
with open("/var/lib/dhcp3/dhclient.leases", "r") as f:
for line in f:
if "option host-name" in line:
HOST = line.split('"')[1]
break
# 4. Update HOSTNAME environment variable if it differs
current_hostname = os.getenv("HOSTNAME", "")
if host and current_hostname != host:
os.environ["HOSTNAME"] = host
# Si no, leer el parámetro del kernel hostname
if not HOST:
with open("/proc/cmdline", "r") as f:
cmdline = f.read()
HOST = re.search(r"hostname=([^ ]+)", cmdline)
if HOST:
HOST = HOST.group(1)
if HOSTNAME != HOST:
os.environ["HOSTNAME"] = HOST
if HOST:
print(HOST)
return host if host else None
except Exception as e:
print(f"Error in ogGetHostname: {e}")
return None
#/**
# ogGetIpAddress
@ -179,7 +179,6 @@ def ogGetIpAddress():
return 0
#/**
# ogGetMacAddress
#@brief Muestra la dirección Ethernet del cliente.
@ -241,7 +240,6 @@ def ogGetNetInterface():
return 0
#/**
# ogGetRepoIp
#@brief Muestra la dirección IP del repositorio de datos.
@ -263,7 +261,6 @@ def ogGetRepoIp():
return None
#/**
# ogGetServerIp
#@brief Muestra la dirección IP del Servidor de OpenGnSys.
@ -305,7 +302,6 @@ def ogGetServerIp():
SystemLib.ogEcho("session", "info", "No valid file system found")
return None
#/**
# ogMakeGroupDir [ str_repo ]
#@brief Crea el directorio para el grupo del cliente.