Created
September 10, 2024 12:19
-
-
Save AlgorithmAlchemy/697ffe98955af66e42be1140aac216f1 to your computer and use it in GitHub Desktop.
Подключение к wifi python pywifi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pywifi | |
from pywifi import const, Profile | |
import time | |
def get_wifi_connections(): | |
wifi = pywifi.PyWiFi() | |
interfaces = wifi.interfaces() | |
print("Состояние подключения Wi-Fi адаптеров:") | |
for interface in interfaces: | |
print(f"Адаптер: {interface.name()}") | |
if interface.status() == const.IFACE_CONNECTED: | |
profiles = interface.network_profiles() | |
connected_ssid = profiles[0].ssid if profiles else 'Неизвестно' | |
print(f"Подключен к сети: {connected_ssid}") | |
else: | |
print("Не подключен к сети") | |
def connect_to_network(interface, ssid, password=None): | |
print(f"Попытка подключения к сети: {ssid}") | |
wifi_profile = Profile() | |
wifi_profile.ssid = ssid | |
wifi_profile.auth = const.AUTH_ALG_OPEN | |
wifi_profile.akm.append(const.AKM_TYPE_WPA2PSK) | |
wifi_profile.cipher = const.CIPHER_TYPE_CCMP | |
wifi_profile.key = password if password else '' | |
interface.remove_all_network_profiles() | |
profile = interface.add_network_profile(wifi_profile) | |
interface.connect(profile) | |
time.sleep(10) # Подождём, чтобы подключение успело установиться | |
if interface.status() == const.IFACE_CONNECTED: | |
print(f"Подключение к сети {ssid} успешно.") | |
else: | |
print(f"Не удалось подключиться к сети {ssid}.") | |
if __name__ == "__main__": | |
get_wifi_connections() | |
wifi = pywifi.PyWiFi() | |
interfaces = wifi.interfaces() | |
ssid_to_connect = "Название сети" # Укажите название сети | |
password = None # Укажи пароль, если он есть | |
for interface in interfaces: | |
if interface.status() != const.IFACE_CONNECTED or interface.network_profiles()[0].ssid != ssid_to_connect: | |
print(f"Попытка подключения к сети {ssid_to_connect} с адаптером {interface.name()}.") | |
connect_to_network(interface, ssid_to_connect, password) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment