diff options
author | Saumit Dinesan <justsaumit@protonmail.com> | 2022-12-24 04:34:28 +0530 |
---|---|---|
committer | Saumit Dinesan <justsaumit@protonmail.com> | 2022-12-24 04:34:28 +0530 |
commit | cc49e60041b8958fedf5c0a519a92f646d9bae74 (patch) | |
tree | de1a4ba34bf89d0b4d4e232af223db8a3615c0e4 |
Initial commit
-rw-r--r-- | README.md | 65 | ||||
-rw-r--r-- | auto-login | 6 | ||||
-rw-r--r-- | auto-login.py | 24 | ||||
-rw-r--r-- | requirements.txt | 1 |
4 files changed, 96 insertions, 0 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..3414a5f --- /dev/null +++ b/README.md @@ -0,0 +1,65 @@ +# Auto-Captive-Portal-Login +A small python script which allows you to automatically login to a captive portal by Selenium using XPath values. + + +## Pre-Requisites: +[Selenium Webdriver](https://www.selenium.dev/documentation/webdriver/) for Python (install using `pip install selenium` or `pip install -r requirements.txt`) +[GNU pass](https://www.passwordstore.org/) for storing wifi-credentials (One can store their credentials in the script as plaintext aswell) +geckodriver for Firefox in PATH (optional if not already included in selenium webdriver) +([Download link](https://github.com/mozilla/geckodriver/releases)) + + +## Usage: +``` + $ git clone https://github.com/justsaumit/auto-captive-portal-login + $ cd auto-captive-portal-login/ +``` +If not using pass, edit autologin.py. +Replace the values of **w_user** and **w_pass** +When done Save and exit. +``` + $ (text editor/IDE of choice) autologin.py +``` + +Replace "/path/to/" to the path of the python script(auto-login.py) +When done Save and exit. +``` +$ (text editor/IDE of choice) autologin +``` + +Make the 'auto-login' shell script executable: +``` + $ chmod +x auto-login +``` +Now whenever you wish to login, use `./auto-login`. + +If you want to add the script to your PATH: +For system-wide +``` + $ sudo cp auto-login /usr/local/bin +``` +or +For individual user +``` + $ cp auto-login ~/.local/bin +``` +And execute it from anywhere as: +``` + $ auto-login +``` + +## Intentions: +I originally wished to create a bashscript to log into my College wifi-portal just using the terminal. +I later set up [this makeshift script](https://github.com/justsaumit/.dotfiles/blob/main/.scripts/wifi-captive-login) +which just finds out the gateway IP and uses st's -e flag that allows st to open the captive-portal in a webbrowser on a new temporary terminal window. +Issue was I still had to type in my wifi credentials _everytime_ :/ +With this it is the same except I get to automate it using [gnu pass](https://www.passwordstore.org/) +and that using XPath(XML Path) to find elements is really convenient. + +## Future additions: +Update the script upon reaching college as the Xpath values would differ. +Make the python-script platform independent +or just Make a separate pure bashscript to log in via CLI using curl. + + + diff --git a/auto-login b/auto-login new file mode 100644 index 0000000..1643f5d --- /dev/null +++ b/auto-login @@ -0,0 +1,6 @@ +#!/bin/sh + +##Add this shell script to your PATH to access it from any location +##Replace /path/to/ to the location of the python script + +python /path/to/auto-login.py diff --git a/auto-login.py b/auto-login.py new file mode 100644 index 0000000..506d202 --- /dev/null +++ b/auto-login.py @@ -0,0 +1,24 @@ +import time +import subprocess + +w_user=subprocess.run(["pass","wifi-user"], capture_output=True).stdout.decode().strip() +w_pass=subprocess.run(["pass","wifi-pass"], capture_output=True).stdout.decode().strip() +cmd = "netstat -nr | sed -n '3p' | awk '{print $2}'" +pipedps=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT) +gateway=pipedps.communicate()[0].decode().strip() + +from selenium import webdriver +from selenium.webdriver.common.by import By + +driver = webdriver.Firefox() +driver.get("http://"+gateway) + +driver.find_element(by=By.XPATH,value="//*[@id='tf1_userName']").send_keys(w_user) +driver.find_element(by=By.XPATH,value="//*[@id='tf1_password']").send_keys(w_pass) +driver.find_element(by=By.XPATH,value="/html/body/div[1]/div/div/div[2]/form/div/div[5]/button").click() +time.sleep(5) +if(driver.find_element(by=By.XPATH,value="//*[@id='lblLoggedinUser']").is_displayed()): + print("Logged in successfully!") +else: + print("Login failed") +driver.close() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5b0c982 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +selenium==4.7.2 |