Get the preferred interface for network scanning (Ethernet or WiFi).
()
| 11408 | try: |
| 11409 | result = subprocess.run( |
| 11410 | ['ip', '-o', '-4', 'addr', 'show'], |
| 11411 | capture_output=True, text=True, timeout=5) |
| 11412 | if result.returncode != 0: |
| 11413 | return None |
| 11414 | except Exception: |
| 11415 | return None |
| 11416 | import ipaddress as _ipaddress |
| 11417 | for line in result.stdout.strip().splitlines(): |
| 11418 | parts = line.split() |
| 11419 | if len(parts) < 4: |
| 11420 | continue |
| 11421 | if candidates and parts[1] not in candidates: |
| 11422 | continue |
| 11423 | for i, p in enumerate(parts): |
| 11424 | if p == 'inet' and i + 1 < len(parts): |
| 11425 | cidr = parts[i + 1] |
| 11426 | if cidr.startswith('127.'): |
| 11427 | continue |
| 11428 | try: |
| 11429 | return str(_ipaddress.IPv4Network(cidr, strict=False)) |
| 11430 | except (ValueError, _ipaddress.NetmaskValueError): |
nothing calls this directly
no test coverage detected