diff --git a/client/lib/engine/bin/NetLib.py b/client/lib/engine/bin/NetLib.py index 2bdbc08..32e9e5e 100755 --- a/client/lib/engine/bin/NetLib.py +++ b/client/lib/engine/bin/NetLib.py @@ -196,26 +196,44 @@ def ogGetIpAddress(): #@return str_ether - Dirección Ethernet #*/ ## def ogGetMacAddress(): - MAC = "" + try: + if "DEVICE" in os.environ: + device = os.environ["DEVICE"] + result = subprocess.run( + ["ip", "-o", "link", "show", "up", "dev", device], + capture_output=True, + text=True, + check=True + ).stdout + else: + result = subprocess.run( + ["ip", "-o", "link", "show", "up"], + capture_output=True, + text=True, + check=True + ).stdout - if len(sys.argv) >= 2 and sys.argv[1] == "help": - SystemLib.ogHelp("ogGetMacAddress", "ogGetMacAddress", "ogGetMacAddress => 00:11:22:33:44:55") - return + mac_addresses = [] + for line in result.splitlines(): + if "link/ether" in line: + parts = line.split() + for i, part in enumerate(parts): + if part == "link/ether": + mac_addresses.append(parts[i + 1].upper()) - # Obtener direcciones Ethernet. - if "DEVICE" in os.environ: - MAC = subprocess.run(["ip", "-o", "link", "show", "up", "dev", os.environ["DEVICE"]], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout.decode().split() - MAC = [addr.upper() for addr in MAC if "ether" in addr] - else: - MAC = subprocess.run(["ip", "-o", "link", "show", "up"], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout.decode().split() - MAC = [addr.upper() for addr in MAC if "ether" in addr and "lo" not in addr] - - # Mostrar solo la primera. - if MAC: - print(MAC[0]) - - return 0 + if mac_addresses: + print(mac_addresses[0]) + return mac_addresses[0] + else: + print("No active mac address found") + return None + except subprocess.CalledProcessError as e: + print(f"Error executing ip command: {e.stderr}") + return None + except Exception as e: + print(f"Unexpected error: {str(e)}") + return None #/** # ogGetNetInterface