feat: add option to display IPv6 addresses in network interface details
Some checks failed
Ansible Lint Check / check-ansible (push) Failing after 12s
Nix Format Check / check-format (push) Successful in 46s
Python Lint Check / check-python (push) Failing after 10s

This commit is contained in:
Menno van Leeuwen 2025-03-26 14:49:40 +01:00
parent 98690a5e70
commit 7eed38dc76
Signed by: vleeuwenmenno
SSH Key Fingerprint: SHA256:OJFmjANpakwD3F2Rsws4GLtbdz1TJ5tkQF0RZmF0TRE

View File

@ -147,7 +147,7 @@ def get_external_ips():
return ipv4, ipv6
def display_interface_details(show_physical=False, show_virtual=False, show_all=False, show_external_ip=False):
def display_interface_details(show_physical=False, show_virtual=False, show_all=False, show_external_ip=False, show_ipv6=False):
"""
Display details of network interfaces based on the specified flags.
@ -156,12 +156,10 @@ def display_interface_details(show_physical=False, show_virtual=False, show_all=
show_virtual (bool): Show virtual interfaces (UP by default unless combined with show_all).
show_all (bool): Include all interfaces (UP, DOWN, UNKNOWN).
show_external_ip (bool): Fetch and display the external IP address.
show_ipv6 (bool): Include IPv6 addresses in the output.
Notes:
- If no flags are specified, both UP physical and virtual interfaces are shown by default.
- If `-a` is specified, it includes all physical interfaces by default.
- If `-a` and `-v` are combined, it includes all physical and virtual interfaces.
- If `-e` is specified, the external IP address is displayed.
- By default, only IPv4 addresses are shown unless `-6` is specified.
"""
if show_external_ip:
ipv4, ipv6 = get_external_ips()
@ -203,22 +201,26 @@ def display_interface_details(show_physical=False, show_virtual=False, show_all=
capture_output=True, text=True, check=True)
state, mac = get_interface_state(interface)
if result.returncode == 0:
parts = result.stdout.strip().split()
if len(parts) >= 3:
name = parts[0]
ip_with_mask = parts[2]
lines = result.stdout.strip().splitlines()
for line in lines:
parts = line.split()
if len(parts) >= 3:
name = parts[0]
ip_with_mask = parts[2]
# 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 = ""
# Skip IPv6 addresses unless `-6` is specified
if ":" in ip_with_mask and not show_ipv6:
continue
print(f"{name:<{col_widths['interface']}} {ip:<{col_widths['ip']}} {subnet:<{col_widths['subnet']}} {state:<{col_widths['state']}} {mac:<{col_widths['mac']}}")
else:
print(f"{interface:<{col_widths['interface']}} {'No IP':<{col_widths['ip']}} {'':<{col_widths['subnet']}} {state:<{col_widths['state']}} {mac:<{col_widths['mac']}}")
# 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']}}")
except Exception as e:
print(f"Error fetching details for {interface}: {e}")
@ -228,10 +230,12 @@ if __name__ == "__main__":
parser.add_argument('-v', action='store_true', help='Show virtual interfaces (UP by default)')
parser.add_argument('-a', action='store_true', help='Include all interfaces (UP, DOWN, UNKNOWN)')
parser.add_argument('-e', action='store_true', help='Fetch and display the external IP address')
parser.add_argument('--ipv6', '-6', action='store_true', help='Include IPv6 addresses in the output')
args = parser.parse_args()
# Default to showing both UP physical and virtual interfaces if no flags are specified
display_interface_details(show_physical=args.p or not (args.p or args.v or args.a or args.e),
show_virtual=args.v or not (args.p or args.v or args.a or args.e),
show_all=args.a,
show_external_ip=args.e)
show_external_ip=args.e,
show_ipv6=args.ipv6)