refs #1728 move ogGetIpAddress around
parent
8b7fa556c6
commit
a5a0b2bdb7
|
@ -1,10 +1,5 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/python3
|
||||||
import socket
|
|
||||||
|
|
||||||
def get_ip_address():
|
from NetLib import ogGetIpAddress
|
||||||
hostname = socket.gethostname()
|
|
||||||
ip_address = socket.gethostbyname(hostname)
|
|
||||||
return ip_address
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
ogGetIpAddress()
|
||||||
print(get_ip_address())
|
|
||||||
|
|
|
@ -142,29 +142,7 @@ def ogGetHostname():
|
||||||
#@note Usa las variables utilizadas por el initrd "/etc/net-ethX.conf
|
#@note Usa las variables utilizadas por el initrd "/etc/net-ethX.conf
|
||||||
#*/ ##
|
#*/ ##
|
||||||
def ogGetIpAddress():
|
def ogGetIpAddress():
|
||||||
if "IPV4ADDR" in os.environ:
|
return ogGlobals.ogGetIpAddress()
|
||||||
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]
|
|
||||||
|
|
||||||
|
|
||||||
#/**
|
#/**
|
||||||
|
|
|
@ -7,8 +7,31 @@ import locale
|
||||||
import importlib.util
|
import importlib.util
|
||||||
|
|
||||||
## required for defining OGLOGFILE
|
## required for defining OGLOGFILE
|
||||||
import NetLib
|
def ogGetIpAddress():
|
||||||
ip = NetLib.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):
|
def load_lang (name):
|
||||||
global lang
|
global lang
|
||||||
|
|
|
@ -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:])
|
|
||||||
|
|
Loading…
Reference in New Issue