MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / _scan_via_secondary_interface

Method _scan_via_secondary_interface

wifi_manager.py:1567–1656  ·  view source on GitHub ↗

Attempt a full nmcli scan using a secondary WiFi adapter (not the AP interface). Returns a list of network dicts or an empty list when no secondary adapter is available or the scan fails.

(self, system_profiles=None, known_ssids=None)

Source from the content-addressed store, hash-verified

1565 if not name or state not in ('DOWN', 'DISCONNECTED', 'UNAVAILABLE', 'UNMANAGED'):
1566 continue
1567 # Skip secondary interfaces (e.g. wlan1) — they may be intentionally
1568 # unmanaged for monitor mode and forcing them managed disrupts wlan0.
1569 if name != self.default_wifi_interface:
1570 self.logger.debug(f"Skipping non-default Wi-Fi interface {name} (state: {state})")
1571 continue
1572 self.logger.info(f"Bringing up Wi-Fi interface {name} (state: {state})")
1573
1574 link_result = subprocess.run(
1575 ['sudo', 'ip', 'link', 'set', name, 'up'],
1576 capture_output=True,
1577 text=True,
1578 timeout=5
1579 )
1580 if link_result.returncode != 0:
1581 self.logger.warning(
1582 f"Failed to set interface {name} up: "
1583 f"{(link_result.stderr or link_result.stdout).strip()}"
1584 )
1585
1586 managed_result = subprocess.run(
1587 ['sudo', 'nmcli', 'dev', 'set', name, 'managed', 'yes'],
1588 capture_output=True,
1589 text=True,
1590 timeout=5
1591 )
1592 if managed_result.returncode != 0:
1593 self.logger.warning(
1594 f"Failed to mark interface {name} managed: "
1595 f"{(managed_result.stderr or managed_result.stdout).strip()}"
1596 )
1597 except Exception as exc:
1598 self.logger.debug(f"Unable to re-enable Wi-Fi interfaces: {exc}")
1599
1600 def get_current_ssid(self):
1601 """Get the current connected SSID"""
1602 try:
1603 # Method 1: SSID from the active connection on the CLIENT radio —
1604 # the dongle when one is fitted, since the built-in may be hosting
1605 # the AP and would report the AP's own name instead.
1606 result = subprocess.run(['nmcli', '-t', '-f', 'GENERAL.CONNECTION', 'dev', 'show', self._client_wifi_interface()],
1607 capture_output=True, text=True, timeout=10)
1608 if result.returncode == 0 and result.stdout.strip():
1609 # Extract connection name (which is usually the SSID for WiFi)
1610 for line in result.stdout.strip().split('\n'):
1611 if line.startswith('GENERAL.CONNECTION:'):
1612 ssid = line.split(':', 1)[1].strip()
1613 if ssid and ssid != '--':
1614 return ssid
1615
1616 # Method 2: Try using iwgetid as fallback
1617 try:
1618 result = subprocess.run(['iwgetid', '-r'],
1619 capture_output=True, text=True, timeout=5)
1620 if result.returncode == 0 and result.stdout.strip():
1621 return result.stdout.strip()
1622 except FileNotFoundError:
1623 pass # iwgetid not available
1624

Callers 1

Calls 11

_get_known_ssidsMethod · 0.95
_run_iwlist_scanMethod · 0.95
infoMethod · 0.80
warningMethod · 0.80
appendMethod · 0.80
addMethod · 0.80
setFunction · 0.50
runMethod · 0.45
sleepMethod · 0.45

Tested by

no test coverage detected