Network login script on python

Introduction

I am a university student that lives on campus, so naturally i use the university wifi a lot. The only problem is that we have to login manually every time we try to enter a domain that is outside the university. I was kinda getting annoyed at having to do this so i made a script to do this for me. So, lets go.

Libraries

import subprocess
import time
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
import yaml

main function

  • Before i go to the main function i will be storing my login in formation in a .yml file which has to be imported into the code.

  • I will also be waiting 15 seconds before executing the code as I will be setting up for it to execute on boot.

  • After i wait 15 seconds and load in the yml file it is time to call the main function.

if __name__=="__main__":
    time.sleep(15)
    with open('C:\\Users\\manu6\\OneDrive\\Documents\\scripts\\make_life_easier\\details.yml','r') as details:
        conf=yaml.load(details,Loader=yaml.FullLoader)
    wifilogin(conf)
  • I will be sending the yml file as conf to wifilogin function which will be our main function.

  • I wanted to configure it in a way that it would automatically connect to my hotspot, which if not available would connect to my hostel wifi.

  • So i took the name of both network from the yaml file

hotspot=conf['Wifi']['hotspot']
wifi=conf['Wifi']['hostel']
  • Now i will pass a command to check all available networks and check if the two networks are available i did this using a user-defined function getNetworkData().

  • I gave the command, wifi name, hotspot name and the term i want to split the single string into each network.

  • This return a list containing wifi or hotspot or both names if available.

  • I later concatenate just the two names into a single string.

Networks=getNetworkdata(['netsh', 'wlan', 'show', 'networks'],wifi,hotspot,'SSID')
    AvailableNetworks=" "
    for i in Networks:
        AvailableNetworks+=i
  • Now if wifi was in the network list and my hotspot was not i connected to my wifi.

  • If my hotspot was available i connect to it.

  • If neither are available i exit the function.

 if (hotspot not in AvailableNetworks) and (wifi in AvailableNetworks):
        connect(wifi)
        login(conf,"username","password","loginbutton")
        print("connected to student")
    elif hotspot in AvailableNetworks: 
        connect(hotspot)
        print("connected to hotspot")
    else:
        print("nothing happened")
        exit()

getNetworkdata()

  • getNetworkdata(cmd,filter1,filter2,part) command first takes the output of the command given and stores it in the variable wifi which is done using the subprocess library.

  • We then decode the data recieved using decode('utf-8').

  • The data so far is a long string containing all the networks.

  • This string has to be split into each network. I did this by splitting the string whenever the term 'SSID' was met, which is at the beginning of each network.

  • Out of all the network i only want the two that i am concerned about i.e. my wifi and hotspot.

  • I do this by using a loop and appending the two toand empty list which is then returned.

def getNetworkdata(cmd,filter1,filter2,part):
    wifi=subprocess.check_output(cmd)
    data=wifi.decode('utf-8')
    info=data.split(part)
    req_info=[]
    for i in info:
        if (filter1 in i) or (filter2 in i):
            req_info.append(i)
    return req_info

connect()

  • The connect function runs the command to connect to the network given as an arguement.
def connect(connection):
    os.system(f'netsh wlan connect name="{connection}"')

login()

  • I case of my college wifi i would have to login after connecting to be able to browse relatively freely.

  • I did this by taking my login details and the url to be used to login from the yaml file.

  • Then i open firefox (my prefered web browser ) as a headless browser so that it doesn't visibly open the browser everytime i log in.

  • I then open the url enter the details and click login using selenium.

  • I then close the browser.

def login(conf,userID,passID,button):
    url=conf['Student_user']['url']
    user=conf['Student_user']['id']
    pas=conf['Student_user']['password']
    options=webdriver.FirefoxOptions()
    options.add_argument('--headless')
    driver=webdriver.Firefox(options=options)
    driver.get(url)
    driver.find_element(by=By.ID,value=userID).send_keys(user)
    driver.find_element(by=By.ID,value=passID).send_keys(pas)
    driver.find_element(by=By.ID,value=button).click()
    driver.quit()

Conclusion

  • So in the end i was able to get a satisfactory result, there are still a few bugs namely:-

    • The browser still opens the url visibly but does not login through the opened browser. (It still logs me in though).

    • even though i named the file with the .pyw extention it still flashes the cmd when i log in.

  • Any help to fix these issues would be greatly appreciated Thank you.