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 subprocess
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
@ -122,40 +131,26 @@ def ogGetGroupName():
|
||||||
#@return str_host - nombre de máquina
|
#@return str_host - nombre de máquina
|
||||||
#*/ ##
|
#*/ ##
|
||||||
def ogGetHostname():
|
def ogGetHostname():
|
||||||
try:
|
# Tomar nombre de la variable HOSTNAME
|
||||||
# 1. Get hostname from the HOSTNAME environment variable
|
|
||||||
host = os.getenv("HOSTNAME", "").strip()
|
host = os.getenv("HOSTNAME", "").strip()
|
||||||
|
if host: return host
|
||||||
|
|
||||||
# 2. If not set, read from the DHCP leases file
|
# Si no, tomar del DHCP, opción host-name /* (comentario para Doxygen)
|
||||||
if not host:
|
|
||||||
dhcp_file = "/var/lib/dhcp3/dhclient.leases"
|
dhcp_file = "/var/lib/dhcp3/dhclient.leases"
|
||||||
if os.path.exists(dhcp_file):
|
if os.path.exists(dhcp_file):
|
||||||
with open(dhcp_file, "r") as f:
|
with open(dhcp_file, "r") as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
if 'option host-name' in line:
|
if 'option host-name' in line:
|
||||||
host = line.split('"')[1].strip(";")
|
return line.split('"')[1].strip(";")
|
||||||
break
|
|
||||||
|
|
||||||
# 3. If still not set, read from kernel parameters in /proc/cmdline
|
# Si no, leer el parámetro del kernel hostname (comentario para Doxygen) */
|
||||||
if not host:
|
|
||||||
cmdline_file = "/proc/cmdline"
|
cmdline_file = "/proc/cmdline"
|
||||||
if os.path.exists(cmdline_file):
|
if os.path.exists(cmdline_file):
|
||||||
with open(cmdline_file, "r") as f:
|
with open(cmdline_file, "r") as f:
|
||||||
for entry in f.read().split():
|
for entry in f.read().split():
|
||||||
if entry.startswith("hostname="):
|
if entry.startswith("hostname="):
|
||||||
host = entry.split("=")[1].strip()
|
return entry.split("=")[1].strip()
|
||||||
break
|
|
||||||
|
|
||||||
# 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
|
# ogGetIpAddress
|
||||||
|
|
Loading…
Reference in New Issue