Scan for networks while in AP mode using smart caching and fallback strategies
(self, interface=None)
| 1656 | if target_iface: |
| 1657 | rescan_cmd.extend(['ifname', target_iface]) |
| 1658 | subprocess.run(rescan_cmd, capture_output=True, timeout=15) |
| 1659 | |
| 1660 | # Give the radio time to complete the scan |
| 1661 | time.sleep(3) |
| 1662 | |
| 1663 | # Get scan results (--rescan no since we already triggered one) |
| 1664 | list_cmd = ['nmcli', '-t', '-f', 'SSID,SIGNAL,SECURITY', 'dev', 'wifi', 'list', '--rescan', 'no'] |
| 1665 | if target_iface: |
| 1666 | list_cmd.extend(['ifname', target_iface]) |
| 1667 | result = subprocess.run(list_cmd, capture_output=True, text=True, timeout=15) |
| 1668 | if result.returncode != 0: |
| 1669 | self.logger.warning(f"nmcli scan command failed on {target_iface} with code {result.returncode}: {result.stderr}") |
| 1670 | |
| 1671 | networks = [] |
| 1672 | if result.returncode == 0: |
| 1673 | for line in result.stdout.strip().split('\n'): |
| 1674 | if not line: |
| 1675 | continue |
| 1676 | parts = self._parse_nmcli_terse_line(line) |
| 1677 | # Need at least SSID and SIGNAL (security may be empty for open networks) |
| 1678 | if len(parts) >= 2 and parts[0]: |
| 1679 | ssid = parts[0] |
| 1680 | security = parts[2] if len(parts) > 2 and parts[2] else 'Open' |
| 1681 | # Mark as known if EITHER in Ragnar's list OR has system profile |
| 1682 | is_known = ssid in ragnar_known or ssid in system_profiles |
| 1683 | networks.append({ |
| 1684 | 'ssid': ssid, |
| 1685 | 'signal': int(parts[1]) if parts[1].isdigit() else 0, |
| 1686 | 'security': security, |
| 1687 | 'known': is_known, |
| 1688 | 'has_system_profile': ssid in system_profiles |
| 1689 | }) |
| 1690 | |
| 1691 | # Remove duplicates and sort by signal strength |
| 1692 | seen_ssids = set() |
| 1693 | unique_networks = [] |
| 1694 | for network in sorted(networks, key=lambda x: x['signal'], reverse=True): |
| 1695 | if network['ssid'] not in seen_ssids: |
| 1696 | seen_ssids.add(network['ssid']) |
| 1697 | unique_networks.append(network) |
| 1698 | |
| 1699 | if not unique_networks: |
| 1700 | self.logger.warning( |
| 1701 | f"nmcli reported zero networks on {target_iface}; attempting iwlist fallback" |
| 1702 | ) |
| 1703 | fallback_networks = self._run_iwlist_scan( |
| 1704 | target_iface, |
| 1705 | system_profiles=system_profiles, |
| 1706 | known_ssids=ragnar_known |
| 1707 | ) |
| 1708 | if fallback_networks: |
| 1709 | unique_networks = fallback_networks |
| 1710 | else: |
| 1711 | self.logger.info(f"Fallback iwlist scan also returned zero networks on {target_iface}") |
| 1712 | |
| 1713 | self._cache_interface_networks(target_iface, unique_networks) |
| 1714 | self.logger.info(f"Found {len(unique_networks)} unique networks on {target_iface}") |
| 1715 |
no test coverage detected