refs #1728 move ogGetIpAddress around

thin-wrappers
Natalia Serrano 2025-03-19 13:14:35 +01:00
parent 8b7fa556c6
commit a5a0b2bdb7
4 changed files with 29 additions and 49 deletions

View File

@ -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()

View File

@ -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()
#/**

View File

@ -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

View File

@ -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:])