refs #1166 add ogGetHostname()
parent
6e53c31113
commit
b4b2ab09cb
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue