From a5a0b2bdb7ff2340d35b4394b26693913239f9e3 Mon Sep 17 00:00:00 2001 From: Natalia Serrano Date: Wed, 19 Mar 2025 13:14:35 +0100 Subject: [PATCH] refs #1728 move ogGetIpAddress around --- ogclient/interfaceAdm/getIpAddress.py | 11 +++-------- ogclient/lib/python3/NetLib.py | 24 +----------------------- ogclient/lib/python3/ogGlobals.py | 27 +++++++++++++++++++++++++-- ogclient/scripts/getIpAddress.py | 16 ---------------- 4 files changed, 29 insertions(+), 49 deletions(-) delete mode 100755 ogclient/scripts/getIpAddress.py diff --git a/ogclient/interfaceAdm/getIpAddress.py b/ogclient/interfaceAdm/getIpAddress.py index 0ba1389..cc18939 100755 --- a/ogclient/interfaceAdm/getIpAddress.py +++ b/ogclient/interfaceAdm/getIpAddress.py @@ -1,10 +1,5 @@ -#!/usr/bin/env python3 -import socket +#!/usr/bin/python3 -def get_ip_address(): - hostname = socket.gethostname() - ip_address = socket.gethostbyname(hostname) - return ip_address +from NetLib import ogGetIpAddress -if __name__ == "__main__": - print(get_ip_address()) +ogGetIpAddress() diff --git a/ogclient/lib/python3/NetLib.py b/ogclient/lib/python3/NetLib.py index 5bd7cc5..e356a45 100644 --- a/ogclient/lib/python3/NetLib.py +++ b/ogclient/lib/python3/NetLib.py @@ -142,29 +142,7 @@ def ogGetHostname(): #@note Usa las variables utilizadas por el initrd "/etc/net-ethX.conf #*/ ## def ogGetIpAddress(): - if "IPV4ADDR" in os.environ: - ip = os.environ["IPV4ADDR"] - if '/' in ip: ip = ip.split ('/')[0] - return ip - - extra_args = [] - if "DEVICE" in os.environ: - extra_args = [ "dev", os.environ["DEVICE"] ] - ipas = subprocess.run (['ip', '-json', 'address', 'show', 'up'] + extra_args, capture_output=True, text=True).stdout - - ipasj = json.loads (ipas) - addresses = [] - for e in ipasj: - if 'lo' == e['ifname']: continue - if 'addr_info' not in e: continue - addrs = e['addr_info'] - for a in addrs: - if 'inet' != a['family']: continue - addresses.append ({ 'local': a['local'], 'prefixlen': a['prefixlen'] }) - - if 1 != len (addresses): - raise Exception ('more than one local IP address found') - return addresses[0] + return ogGlobals.ogGetIpAddress() #/** diff --git a/ogclient/lib/python3/ogGlobals.py b/ogclient/lib/python3/ogGlobals.py index c744b2c..7e978fc 100644 --- a/ogclient/lib/python3/ogGlobals.py +++ b/ogclient/lib/python3/ogGlobals.py @@ -7,8 +7,31 @@ import locale import importlib.util ## required for defining OGLOGFILE -import NetLib -ip = NetLib.ogGetIpAddress() +def ogGetIpAddress(): + if "IPV4ADDR" in os.environ: + ip = os.environ["IPV4ADDR"] + if '/' in ip: ip = ip.split ('/')[0] + return ip + + extra_args = [] + if "DEVICE" in os.environ: + extra_args = [ "dev", os.environ["DEVICE"] ] + ipas = subprocess.run (['ip', '-json', 'address', 'show', 'up'] + extra_args, capture_output=True, text=True).stdout + + ipasj = json.loads (ipas) + addresses = [] + for e in ipasj: + if 'lo' == e['ifname']: continue + if 'addr_info' not in e: continue + addrs = e['addr_info'] + for a in addrs: + if 'inet' != a['family']: continue + addresses.append ({ 'local': a['local'], 'prefixlen': a['prefixlen'] }) + + if 1 != len (addresses): + raise Exception ('more than one local IP address found') + return addresses[0] +ip = ogGetIpAddress() def load_lang (name): global lang diff --git a/ogclient/scripts/getIpAddress.py b/ogclient/scripts/getIpAddress.py deleted file mode 100755 index 65f62aa..0000000 --- a/ogclient/scripts/getIpAddress.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python3 -import sys -import NetLib - -def get_ip_address(*args): - try: - # Llama a ogGetIpAddress desde NetLib y captura el resultado - result = NetLib.ogGetIpAddress(*args) - print(result.strip()) - except Exception as e: - print(f"Error ejecutando ogGetIpAddress: {e}", file=sys.stderr) - sys.exit(1) - -if __name__ == "__main__": - get_ip_address(*sys.argv[1:]) -