File size: 4,409 Bytes
700dd75 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | #!/usr/bin/env python3
"""
Simple network interface utilities
"""
import platform
import re
import subprocess
def get_network_interfaces():
"""Get network interfaces with their IP addresses"""
try:
result = subprocess.run(
["/sbin/ip", "addr", "show"], capture_output=True, text=True, check=True
)
return _parse_ip_output(result.stdout)
except (subprocess.CalledProcessError, FileNotFoundError):
try:
result = subprocess.run(["ifconfig"], capture_output=True, text=True, check=True)
return _parse_ifconfig_output(result.stdout)
except (subprocess.CalledProcessError, FileNotFoundError):
return {}
def _parse_ip_output(output):
"""Parse 'ip addr' command output"""
interfaces = {}
current_interface = None
for line in output.split("\n"):
interface_match = re.match(r"^\d+:\s+(\w+):", line)
if interface_match:
current_interface = interface_match.group(1)
interfaces[current_interface] = []
ip_match = re.search(r"inet\s+(\d+\.\d+\.\d+\.\d+)", line)
if ip_match and current_interface:
interfaces[current_interface].append(ip_match.group(1))
return interfaces
def _parse_ifconfig_output(output):
"""Parse 'ifconfig' command output"""
interfaces = {}
current_interface = None
for line in output.split("\n"):
interface_match = re.match(r"^(\w+):", line)
if interface_match:
current_interface = interface_match.group(1)
interfaces[current_interface] = []
ip_match = re.search(r"inet\s+(\d+\.\d+\.\d+\.\d+)", line)
if ip_match and current_interface:
interfaces[current_interface].append(ip_match.group(1))
return interfaces
def find_interface_by_ip(target_ip):
"""Find interface name for given IP address"""
interfaces = get_network_interfaces()
for interface, ip_list in interfaces.items():
if target_ip in ip_list:
return interface
return None
def resolve_interface(interface: str) -> tuple[str, str]:
"""
Resolve interface parameter to actual network interface name and environment type
Args:
interface: "sim", "real", or direct interface name or IP address
Returns:
tuple: (interface_name, env_type) where env_type is "sim" or "real"
"""
# Check if interface is an IP address
if re.match(r"^\d+\.\d+\.\d+\.\d+$", interface):
if interface == "127.0.0.1":
return interface, "sim"
else:
return interface, "real"
if interface == "sim":
lo_interface = find_interface_by_ip("127.0.0.1")
if lo_interface:
# macOS uses lo0 instead of lo
if platform.system() == "Darwin" and lo_interface == "lo":
return "lo0", "sim"
return lo_interface, "sim"
return ("lo0" if platform.system() == "Darwin" else "lo"), "sim"
elif interface == "real":
interfaces = get_network_interfaces()
for iface, ip_list in interfaces.items():
for ip in ip_list:
if ip.startswith("192.168.123."):
return iface, "real"
return interface, "real" # fallback
else:
# Direct interface name - check if it has 127.0.0.1 to determine env_type
interfaces = get_network_interfaces()
if interface in interfaces:
for ip in interfaces[interface]:
if ip == "127.0.0.1":
return interface, "sim"
# macOS lo interface handling
if platform.system() == "Darwin" and interface == "lo":
return "lo0", "sim"
# Default to real for unknown interfaces
return interface, "real"
if __name__ == "__main__":
interfaces = get_network_interfaces()
if not interfaces:
print("No network interfaces found")
exit(1)
# Show all interfaces
print("Network interfaces:")
for interface, ip_list in interfaces.items():
print(f" {interface}: {', '.join(ip_list)}")
# Test resolve_interface function
print("\nTesting resolve_interface:")
for test_interface in ["sim", "real", "lo", "127.0.0.1"]:
interface_name, env_type = resolve_interface(test_interface)
print(f" {test_interface} -> {interface_name} ({env_type})")
|