import ipaddress import shutil import subprocess from typing import Optional def check_installed() -> bool: """Check if tcpdump is installed and available on the system path.""" return shutil.which('tcpdump') is not None def ensure_installed(): """Ensure that tcpdump is installed, raise an error if not.""" if not check_installed(): raise RuntimeError('tcpdump is not installed. Please install it to continue.') def list_interfaces() -> str: """List available network interfaces using tcpdump.""" ensure_installed() try: result = subprocess.run(['tcpdump', '--list-interfaces'], capture_output=True, text=True, check=True) print(result.stdout) except subprocess.CalledProcessError as e: print(f'Failed to list interfaces: {e}') return '' def is_valid_ipv4(ip: str) -> bool: try: ipaddress.IPv4Address(ip) return True except ValueError: return False def str_to_ipv4(ip: str) -> (bool, Optional[ipaddress]): try: address = ipaddress.IPv4Address(ip) return address == ipaddress.IPv4Address(ip), address except ipaddress.AddressValueError: return False, None