### Automatic Wifi Connector Bot


import os
import sys

saved_profiles = os.popen('netsh wlan show profiles').read()           # To get the saved profiles
print(saved_profiles)
available_profiles = os.popen('netsh wlan show networks').read()       # To get the available profiles
print(available_profiles)

preferred_ssid=input('Enter the preferred Wifi for your connection : ')

response = os.popen("netsh wlan disconnect").read()# To disconnect the present connection and connect to the preferred one
print(response)

if preferred_ssid not in saved_profiles:
    print("Profile for "+preferred_ssid+" is not saved in system")
    print("Sorry but can't establish the connection")
    sys.exit()
else:
    print("Profile for "+preferred_ssid+" is saved in system")


while True:
    avail = os.popen('netsh wlan show networks').read()              # To get the available profiles
    #sleep(3)
    if preferred_ssid in avail:                                      # Checks if the preferred connection is available or not
        print('Found')
        break


print('--------Connecting----------')
resp = os.popen('netsh wlan connect name='+'"'+preferred_ssid+'"').read()
print(resp)
