Дополнение логики подключения к WiFi, если профиль оказывается битый
This commit is contained in:
parent
e1b027b301
commit
ab38a0db50
2 changed files with 130 additions and 37 deletions
|
|
@ -56,17 +56,27 @@ class WifiConnectionService:
|
|||
Exception: Если произошла ошибка при выполнении команд nmcli.
|
||||
"""
|
||||
try:
|
||||
result = subprocess.run(["nmcli", "radio", "wifi"], capture_output=True, text=True)
|
||||
if "enabled" in result.stdout.lower():
|
||||
interfaces = subprocess.run(["nmcli", "device", "status"], capture_output=True, text=True).stdout
|
||||
for line in interfaces.splitlines():
|
||||
if "wifi" in line.lower() and "connected" in line.lower():
|
||||
iface = line.split()[0]
|
||||
logger.info(f"Using Wi-Fi interface: {iface}")
|
||||
return iface
|
||||
logger.warning("Wi-Fi interface not found or not connected")
|
||||
return "wlan0"
|
||||
logger.error("Wi-Fi radio is disabled")
|
||||
result = subprocess.run(
|
||||
["nmcli", "radio", "wifi"], capture_output=True, text=True
|
||||
)
|
||||
if "enabled" not in result.stdout.lower():
|
||||
logger.error("Wi-Fi radio is disabled")
|
||||
return ""
|
||||
|
||||
interfaces_output = subprocess.run(
|
||||
["nmcli", "device", "status"], capture_output=True, text=True
|
||||
).stdout
|
||||
|
||||
for line in interfaces_output.splitlines():
|
||||
parts = line.split()
|
||||
if len(parts) < 2:
|
||||
continue
|
||||
device_name, device_type = parts[0], parts[1]
|
||||
if device_type.lower() == "wifi":
|
||||
logger.info(f"Using Wi-Fi interface: {device_name}")
|
||||
return device_name
|
||||
|
||||
logger.warning("Wi-Fi interface not found in `nmcli device status` output")
|
||||
return ""
|
||||
except Exception as e:
|
||||
logger.error(f"Error detecting Wi-Fi interface: {str(e)}")
|
||||
|
|
@ -92,20 +102,64 @@ class WifiConnectionService:
|
|||
if not self.interface:
|
||||
return False, self.translations.gettext("Wi-Fi-интерфейс не доступен")
|
||||
|
||||
result = subprocess.run(["nmcli", "-f", "SSID", "device", "wifi", "list"], capture_output=True, text=True)
|
||||
result = subprocess.run(
|
||||
["nmcli", "-f", "SSID", "device", "wifi", "list"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
if ssid not in result.stdout:
|
||||
return False, self.translations.gettext("Сеть с SSID {ssid} не найдена в списке доступных сетей").format(ssid=ssid)
|
||||
|
||||
cmd = ["nmcli", "device", "wifi", "connect", ssid, "password", password]
|
||||
process = subprocess.run(cmd, capture_output=True, text=True, check=False)
|
||||
def run_connect() -> subprocess.CompletedProcess[str]:
|
||||
cmd = [
|
||||
"nmcli",
|
||||
"device",
|
||||
"wifi",
|
||||
"connect",
|
||||
ssid,
|
||||
"ifname",
|
||||
self.interface,
|
||||
]
|
||||
if password:
|
||||
cmd.extend(["password", password])
|
||||
return subprocess.run(cmd, capture_output=True, text=True, check=False)
|
||||
|
||||
process = run_connect()
|
||||
if process.returncode != 0:
|
||||
error_msg = process.stderr.lower()
|
||||
if "secrets were required" in error_msg or "authentication failed" in error_msg or "802-11-wireless-security.psk: недействительное свойство" in process.stderr:
|
||||
return False, self.translations.gettext("Ошибка подключения к {ssid}: Неверный пароль").format(ssid=ssid)
|
||||
key_mgmt_error = (
|
||||
"secrets were required" in error_msg
|
||||
or "authentication failed" in error_msg
|
||||
or "802-11-wireless-security.psk" in error_msg
|
||||
or "key-mgmt" in error_msg
|
||||
)
|
||||
if key_mgmt_error:
|
||||
subprocess.run(
|
||||
["nmcli", "connection", "delete", "id", ssid],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
retry_process = run_connect()
|
||||
if retry_process.returncode == 0:
|
||||
logger.info(f"Successfully connected to network {ssid} after profile reset")
|
||||
return True, None
|
||||
error_msg = retry_process.stderr.lower()
|
||||
if (
|
||||
"secrets were required" in error_msg
|
||||
or "authentication failed" in error_msg
|
||||
or "802-11-wireless-security.psk" in error_msg
|
||||
or "key-mgmt" in error_msg
|
||||
):
|
||||
return False, self.translations.gettext("Ошибка подключения к {ssid}: Неверный пароль или тип безопасности").format(ssid=ssid)
|
||||
elif "no network with ssid" in error_msg:
|
||||
return False, self.translations.gettext("Ошибка подключения к {ssid}: Сеть с SSID {ssid} недоступна или неверно указана").format(ssid=ssid)
|
||||
else:
|
||||
return False, self.translations.gettext("Ошибка подключения к {ssid}: {error}").format(ssid=ssid, error=retry_process.stderr)
|
||||
elif "no network with ssid" in error_msg:
|
||||
return False, self.translations.gettext("Ошибка подключения к {ssid}: Сеть с SSID {ssid} недоступна или неверно указана").format(ssid=ssid)
|
||||
else:
|
||||
return False, self.translations.gettext("Ошибка подключения к {ssid}: {error}").format(ssid=ssid, error=process.stderr)
|
||||
|
||||
logger.info(f"Successfully connected to network {ssid}")
|
||||
return True, None
|
||||
except subprocess.CalledProcessError as e:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue