diff --git a/config/ansible/tasks/global/utils/ipaddr b/config/ansible/tasks/global/utils/ipaddr index edefc99..3a9552b 100755 --- a/config/ansible/tasks/global/utils/ipaddr +++ b/config/ansible/tasks/global/utils/ipaddr @@ -160,12 +160,13 @@ def display_interface_details(show_physical=False, show_virtual=False, show_all= Notes: - By default, only IPv4 addresses are shown unless `-6` is specified. + - IPv6 addresses are displayed in a separate column if `-6` is specified. """ if show_external_ip: ipv4, ipv6 = get_external_ips() print(f"External IPv4: {ipv4}") print(f"External IPv6: {ipv6}") - print("-" * 50) + print("-" * 70) interfaces = [] @@ -185,15 +186,20 @@ def display_interface_details(show_physical=False, show_virtual=False, show_all= # Define column widths based on expected maximum content length col_widths = { 'interface': 15, - 'ip': 18, + 'ipv4': 18, + 'ipv6': 40 if show_ipv6 else 0, # Hide IPv6 column if not showing IPv6 'subnet': 10, 'state': 10, 'mac': 18 } # Print header with proper formatting - print(f"{'Interface':<{col_widths['interface']}} {'IP Address':<{col_widths['ip']}} {'Subnet':<{col_widths['subnet']}} {'State':<{col_widths['state']}} {'MAC Address':<{col_widths['mac']}}") - print("-" * (col_widths['interface'] + col_widths['ip'] + col_widths['subnet'] + col_widths['state'] + col_widths['mac'])) + header = f"{'Interface':<{col_widths['interface']}} {'IPv4 Address':<{col_widths['ipv4']}}" + if show_ipv6: + header += f" {'IPv6 Address':<{col_widths['ipv6']}}" + header += f" {'Subnet':<{col_widths['subnet']}} {'State':<{col_widths['state']}} {'MAC Address':<{col_widths['mac']}}" + print(header) + print("-" * (col_widths['interface'] + col_widths['ipv4'] + (col_widths['ipv6'] if show_ipv6 else 0) + col_widths['subnet'] + col_widths['state'] + col_widths['mac'])) for interface in interfaces: try: @@ -202,25 +208,27 @@ def display_interface_details(show_physical=False, show_virtual=False, show_all= state, mac = get_interface_state(interface) if result.returncode == 0: lines = result.stdout.strip().splitlines() + ipv4 = "N/A" + ipv6 = "N/A" + subnet = "" + for line in lines: parts = line.split() if len(parts) >= 3: - name = parts[0] ip_with_mask = parts[2] - # Skip IPv6 addresses unless `-6` is specified - if ":" in ip_with_mask and not show_ipv6: - continue + # Check if the address is IPv4 or IPv6 + if ":" in ip_with_mask: # IPv6 + ipv6 = ip_with_mask.split('/')[0] + else: # IPv4 + ipv4 = ip_with_mask.split('/')[0] + subnet = ip_with_mask.split('/')[1] if '/' in ip_with_mask else "" - # Split IP/mask - if '/' in ip_with_mask: - ip = ip_with_mask.split('/')[0] - subnet = ip_with_mask.split('/')[1] - else: - ip = ip_with_mask - subnet = "" - - print(f"{name:<{col_widths['interface']}} {ip:<{col_widths['ip']}} {subnet:<{col_widths['subnet']}} {state:<{col_widths['state']}} {mac:<{col_widths['mac']}}") + row = f"{interface:<{col_widths['interface']}} {ipv4:<{col_widths['ipv4']}}" + if show_ipv6: + row += f" {ipv6:<{col_widths['ipv6']}}" + row += f" {subnet:<{col_widths['subnet']}} {state:<{col_widths['state']}} {mac:<{col_widths['mac']}}" + print(row) except Exception as e: print(f"Error fetching details for {interface}: {e}")