Collect SSID/frequency information for an interface via iw/iwgetid.
(interface_name: str)
| 92 | |
| 93 | |
| 94 | def _get_interface_link_details(interface_name: str) -> Dict[str, Optional[str]]: |
| 95 | """Collect SSID/frequency information for an interface via iw/iwgetid.""" |
| 96 | details: Dict[str, Optional[str]] = { |
| 97 | 'ssid': None, |
| 98 | 'frequency_mhz': None, |
| 99 | 'band': None, |
| 100 | } |
| 101 | try: |
| 102 | result = subprocess.run( |
| 103 | ['iw', 'dev', interface_name, 'link'], |
| 104 | capture_output=True, |
| 105 | text=True, |
| 106 | timeout=3 |
| 107 | ) |
| 108 | if result.returncode == 0: |
| 109 | stdout = result.stdout.strip() |
| 110 | if stdout and 'Not connected' not in stdout: |
| 111 | ssid_match = re.search(r'SSID:\s*(.+)', stdout) |
| 112 | freq_match = re.search(r'freq:\s*(\d+)', stdout) |
| 113 | if ssid_match: |
| 114 | details['ssid'] = ssid_match.group(1).strip() |
| 115 | if freq_match: |
| 116 | freq_value = int(freq_match.group(1)) |
| 117 | details['frequency_mhz'] = freq_value |
| 118 | details['band'] = _infer_frequency_band(freq_value) |
| 119 | return details |
| 120 | except FileNotFoundError: |
| 121 | logger.debug("iw utility not available for interface introspection") |
| 122 | except subprocess.TimeoutExpired: |
| 123 | logger.debug(f"iw dev {interface_name} link timed out") |
| 124 | except Exception as exc: |
| 125 | logger.debug(f"iw dev {interface_name} link failed: {exc}") |
| 126 | |
| 127 | try: |
| 128 | result = subprocess.run( |
| 129 | ['iwgetid', '-i', interface_name, '-r'], |
| 130 | capture_output=True, |
| 131 | text=True, |
| 132 | timeout=3 |
| 133 | ) |
| 134 | if result.returncode == 0: |
| 135 | ssid = (result.stdout or '').strip() |
| 136 | if ssid: |
| 137 | details['ssid'] = ssid |
| 138 | except FileNotFoundError: |
| 139 | logger.debug("iwgetid utility not available") |
| 140 | except subprocess.TimeoutExpired: |
| 141 | logger.debug(f"iwgetid -i {interface_name} timed out") |
| 142 | except Exception as exc: |
| 143 | logger.debug(f"iwgetid -i {interface_name} failed: {exc}") |
| 144 | |
| 145 | return details |
| 146 | |
| 147 | |
| 148 | def gather_wifi_interfaces(default_interface: str = 'wlan0') -> List[Dict]: |
no test coverage detected