Check if an Ethernet interface has a physical connection (carrier).
(interface_name: str)
| 250 | |
| 251 | |
| 252 | def _check_ethernet_carrier(interface_name: str) -> bool: |
| 253 | """Check if an Ethernet interface has a physical connection (carrier).""" |
| 254 | try: |
| 255 | # Check carrier file in sysfs |
| 256 | result = subprocess.run( |
| 257 | ['cat', f'/sys/class/net/{interface_name}/carrier'], |
| 258 | capture_output=True, |
| 259 | text=True, |
| 260 | timeout=2 |
| 261 | ) |
| 262 | if result.returncode == 0: |
| 263 | carrier = result.stdout.strip() |
| 264 | return carrier == '1' |
| 265 | except Exception as exc: |
| 266 | logger.debug(f"Unable to check carrier for {interface_name}: {exc}") |
| 267 | |
| 268 | # Fallback: check if interface is UP and has an IP |
| 269 | try: |
| 270 | result = subprocess.run( |
| 271 | ['ip', 'link', 'show', interface_name], |
| 272 | capture_output=True, |
| 273 | text=True, |
| 274 | timeout=2 |
| 275 | ) |
| 276 | if result.returncode == 0: |
| 277 | return 'state UP' in result.stdout |
| 278 | except Exception: |
| 279 | pass |
| 280 | |
| 281 | return False |
| 282 | |
| 283 | |
| 284 | def gather_ethernet_interfaces(default_interface: str = 'eth0') -> List[Dict]: |
no test coverage detected