Created
December 20, 2019 20:48
-
-
Save MatthaeusHarris/2ffd3282352febb970247a1487f2f08b to your computer and use it in GitHub Desktop.
Python3 script to stay logged in to a captive portal wifi network with a very short timeout
This file contains 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
#!/usr/bin/env python3 | |
# pip3 install selenium | |
from selenium import webdriver | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.chrome.options import Options | |
import time | |
import os | |
# Test url must not be https so that the captive portal redirect can catch it | |
test_url="http://google.com" | |
# If the above url redirects normally, this is what it redirects to. | |
redirect_check_url="https://www.google.com" | |
# Captive portal requires a username | |
username="example@example.com" | |
# Timing variables, self-explanatory | |
sleep_time = 15 | |
retry_time = 3 | |
num_retries = 3 | |
# On the captive portal page, what are the elements named? | |
element_for_username="email" | |
element_for_submit="Submit" | |
chrome_options = Options() | |
chrome_options.add_argument("--headless") | |
while (True): | |
print("Checking for captive portal") | |
browser = webdriver.Chrome(chrome_options=chrome_options) | |
browser.get(test_url) | |
current_url = browser.current_url | |
print(f"Current url is {current_url}") | |
if (not current_url.startswith(redirect_check_url)): | |
retries = num_retries | |
while (retries > 0): | |
try: | |
username_element = browser.find_element_by_name(element_for_username) | |
username_element.send_keys(username) | |
signInButton = browser.find_element_by_name(element_for_submit) | |
signInButton.click() | |
retries = 0 | |
print(f"Captive portal login succeeded, we think.") | |
except Exception as e: | |
#### This exception occurs if the elements are not found in the webpage. | |
print ("Some error occured :(") | |
print(e) | |
if (retries > 0): | |
print(f"{retries} left, sleeping a few") | |
retries = retries - 1 | |
time.sleep(retry_time) | |
else: | |
print(f"Captive portal not found.") | |
browser.quit() | |
print(f"Sleeping for {sleep_time} seconds before checking again.") | |
time.sleep(sleep_time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment