|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Comparison script to test ifcfg vs getmac behavior |
| 4 | +""" |
| 5 | +import locale |
| 6 | +import os |
| 7 | +import platform |
| 8 | +import sys |
| 9 | + |
| 10 | + |
| 11 | +def get_system_language(): |
| 12 | + """Get system language settings""" |
| 13 | + try: |
| 14 | + # Try to get system language settings |
| 15 | + lang_info = { |
| 16 | + "locale": locale.getlocale(), |
| 17 | + "default_locale": locale.getdefaultlocale(), |
| 18 | + "environment": {}, |
| 19 | + } |
| 20 | + |
| 21 | + # Check environment variables |
| 22 | + for var in ["LANG", "LC_ALL", "LC_MESSAGES", "LANGUAGE"]: |
| 23 | + if var in os.environ: |
| 24 | + lang_info["environment"][var] = os.environ[var] |
| 25 | + |
| 26 | + return lang_info |
| 27 | + except Exception as e: |
| 28 | + return {"error": str(e)} |
| 29 | + |
| 30 | + |
| 31 | +def get_ifcfg_macs(): |
| 32 | + """Get MAC addresses using ifcfg""" |
| 33 | + try: |
| 34 | + import ifcfg |
| 35 | + |
| 36 | + interfaces = ifcfg.interfaces().values() |
| 37 | + results = {} |
| 38 | + for iface in interfaces: |
| 39 | + ether = iface.get("ether") |
| 40 | + if ether: |
| 41 | + results[iface.get("device", "unknown")] = ether |
| 42 | + return results |
| 43 | + except Exception as e: |
| 44 | + return {"error": str(e)} |
| 45 | + |
| 46 | + |
| 47 | +def get_getmac_macs(): |
| 48 | + """Get MAC addresses using getmac""" |
| 49 | + try: |
| 50 | + import getmac |
| 51 | + |
| 52 | + results = {} |
| 53 | + |
| 54 | + # Default interface |
| 55 | + try: |
| 56 | + mac = getmac.get_mac_address() |
| 57 | + if mac: |
| 58 | + results["default"] = mac |
| 59 | + except Exception as e: |
| 60 | + results["default_error"] = str(e) |
| 61 | + |
| 62 | + # Common interfaces |
| 63 | + common_interfaces = ["eth0", "en0", "wlan0", "wi-fi", "Ethernet"] |
| 64 | + for interface in common_interfaces: |
| 65 | + try: |
| 66 | + mac = getmac.get_mac_address(interface=interface) |
| 67 | + if mac: |
| 68 | + results[interface] = mac |
| 69 | + except Exception as e: |
| 70 | + results[f"{interface}_error"] = str(e) |
| 71 | + |
| 72 | + return results |
| 73 | + except Exception as e: |
| 74 | + return {"error": str(e)} |
| 75 | + |
| 76 | + |
| 77 | +def main(): |
| 78 | + print(f"Platform: {platform.platform()}") |
| 79 | + print(f"System: {platform.system()}") |
| 80 | + print(f"Python: {sys.version}") |
| 81 | + |
| 82 | + # Get and display system language information |
| 83 | + print(f"\n=== System Language Information ===") |
| 84 | + lang_info = get_system_language() |
| 85 | + for key, value in lang_info.items(): |
| 86 | + if key == "environment": |
| 87 | + print(f"{key}:") |
| 88 | + for env_var, env_value in value.items(): |
| 89 | + print(f" {env_var}: {env_value}") |
| 90 | + else: |
| 91 | + print(f"{key}: {value}") |
| 92 | + print() |
| 93 | + |
| 94 | + print("=== ifcfg results ===") |
| 95 | + ifcfg_results = get_ifcfg_macs() |
| 96 | + for interface, mac in ifcfg_results.items(): |
| 97 | + print(f"{interface}: {mac}") |
| 98 | + print() |
| 99 | + |
| 100 | + print("=== getmac results ===") |
| 101 | + getmac_results = get_getmac_macs() |
| 102 | + for interface, mac in getmac_results.items(): |
| 103 | + print(f"{interface}: {mac}") |
| 104 | + print() |
| 105 | + |
| 106 | + # Compare default interfaces |
| 107 | + ifcfg_default = ifcfg_results.get("eth0") or next( |
| 108 | + iter(ifcfg_results.values()), None |
| 109 | + ) |
| 110 | + getmac_default = getmac_results.get("default") |
| 111 | + |
| 112 | + print("=== Comparison ===") |
| 113 | + print(f"ifcfg default: {ifcfg_default}") |
| 114 | + print(f"getmac default: {getmac_default}") |
| 115 | + print(f"Match: {ifcfg_default == getmac_default}") |
| 116 | + |
| 117 | + # Additional analysis |
| 118 | + print(f"\n=== Analysis ===") |
| 119 | + ifcfg_count = len( |
| 120 | + [ |
| 121 | + v |
| 122 | + for v in ifcfg_results.values() |
| 123 | + if not isinstance(v, str) or not v.startswith("error") |
| 124 | + ] |
| 125 | + ) |
| 126 | + getmac_count = len( |
| 127 | + [ |
| 128 | + v |
| 129 | + for v in getmac_results.values() |
| 130 | + if not isinstance(v, str) or not v.startswith("error") |
| 131 | + ] |
| 132 | + ) |
| 133 | + print(f"ifcfg found {ifcfg_count} interfaces") |
| 134 | + print(f"getmac found {getmac_count} interfaces") |
| 135 | + |
| 136 | + # Check for errors |
| 137 | + ifcfg_errors = [ |
| 138 | + v |
| 139 | + for v in ifcfg_results.values() |
| 140 | + if isinstance(v, str) and v.startswith("error") |
| 141 | + ] |
| 142 | + getmac_errors = [ |
| 143 | + v |
| 144 | + for v in getmac_results.values() |
| 145 | + if isinstance(v, str) and v.startswith("error") |
| 146 | + ] |
| 147 | + |
| 148 | + print(f"ifcfg errors: {len(ifcfg_errors)}") |
| 149 | + print(f"getmac errors: {len(getmac_errors)}") |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + main() |
0 commit comments