|
| 1 | +import subprocess |
| 2 | +import getpass |
| 3 | +import time |
| 4 | +from pywifi import PyWiFi |
| 5 | +from pywifi import const |
| 6 | +from pywifi import Profile |
| 7 | + |
| 8 | + |
| 9 | +def connect_wifi(ssid, password): |
| 10 | + # function to connect to Wi-Fi with Password |
| 11 | + |
| 12 | + wifi = PyWiFi() |
| 13 | + iface = wifi.interfaces()[0] |
| 14 | + iface.disconnect() # disconnects from the current Wi-Fi if any |
| 15 | + time.sleep(2) |
| 16 | + |
| 17 | + profile = Profile() # adding new Profile for given Wi-Fi |
| 18 | + profile.ssid = ssid |
| 19 | + profile.auth = const.AUTH_ALG_OPEN |
| 20 | + profile.akm.append(const.AKM_TYPE_WPA2PSK) |
| 21 | + profile.cipher = const.CIPHER_TYPE_CCMP |
| 22 | + profile.key = password |
| 23 | + |
| 24 | + print("\nConnecting to WiFi....") |
| 25 | + iface.connect(iface.add_network_profile(profile)) |
| 26 | + time.sleep(5) |
| 27 | + |
| 28 | + if iface.status() == const.IFACE_CONNECTED: |
| 29 | + print("\t>> Connected Successfully!") |
| 30 | + else: |
| 31 | + print("\t>> Failed to Connect!") |
| 32 | + |
| 33 | + |
| 34 | +def get_available_wifi(): |
| 35 | + # function to get 3 strongest Wi-Fi with SSIDs in Windows |
| 36 | + |
| 37 | + try: |
| 38 | + |
| 39 | + cmd = ['netsh', 'wlan', 'show', 'network', 'mode=Bssid'] |
| 40 | + wifidata = subprocess.check_output(cmd).decode( |
| 41 | + 'ascii').replace("\r", "").split("\n\n") |
| 42 | + |
| 43 | + except subprocess.CalledProcessError: |
| 44 | + print("\nWhoops! Your Wi-Fi seems to be powered off.") |
| 45 | + raise SystemExit |
| 46 | + |
| 47 | + else: |
| 48 | + networks = [] |
| 49 | + for i in range(1, len(wifidata)-1): # parsing SSIDs and signals |
| 50 | + temp = wifidata[i].split("\n") |
| 51 | + ssid = temp[0].rstrip().split(" : ")[-1] |
| 52 | + signal = int(temp[5].rstrip().split(" : ")[-1][:-1]) |
| 53 | + networks.append((ssid, signal)) |
| 54 | + |
| 55 | + networks = sorted(networks, reverse=True, key=lambda x: x[1]) |
| 56 | + return (networks) |
| 57 | + |
| 58 | + |
| 59 | +def main(): |
| 60 | + # main function to take 3 strongest Wi-Fis and connect |
| 61 | + |
| 62 | + networks = get_available_wifi()[:3] # get top 3 strongest networks |
| 63 | + |
| 64 | + print(f"\nYour {len(networks)} Strongest Networks are :") |
| 65 | + for key, network in enumerate(networks): |
| 66 | + print(f"\t[{key+1}] {network[0]}, Signal: {network[1]}%") |
| 67 | + |
| 68 | + choice = input("\nPlease enter your choice : \n\t>> ").strip() |
| 69 | + if not(choice.isdigit() and 1 <= int(choice) <= len(networks)): |
| 70 | + print("Wrong Choice Entered. Exiting...") |
| 71 | + return False |
| 72 | + |
| 73 | + ssid = networks[int(choice)-1][0] |
| 74 | + |
| 75 | + # check if the chosen SSID is already connected |
| 76 | + if ssid in subprocess.check_output("netsh wlan show interfaces").decode(): |
| 77 | + print(f"\nYou are already connected to {ssid} Network. \nExiting...") |
| 78 | + else: |
| 79 | + print(f"\nYou Selected: {ssid}") |
| 80 | + password = getpass.getpass() |
| 81 | + # connect to the chosen SSID with password |
| 82 | + connect_wifi(ssid, password) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + main() |
0 commit comments