Skip to content

Instantly share code, notes, and snippets.

@3urobeat
Created November 27, 2021 15:41
Show Gist options
  • Save 3urobeat/da3fdb011cd7cebb219c068f62db8630 to your computer and use it in GitHub Desktop.
Save 3urobeat/da3fdb011cd7cebb219c068f62db8630 to your computer and use it in GitHub Desktop.
A script to automatically login my headless machine into the house internet captive portal when it gets logged out for some reason
#!/usr/bin/python -u
# captivelogin.py by 3urobeat
# A script to automatically login my headless machine into the house internet captive portal when it gets logged out for some reason
import datetime
import requests
import time
session = requests.Session() # init a session
timenow = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # make getting current time easier
interval = 30
password = "" # my password to login with
url = "https://hotspot.maxxarena.de/logon/cgi/index.cgi" # url of the captive portal
# checks if we can reach the internet and returns a boolean
def checkConnection(retry):
try:
requests.get("https://3urobeat.com", timeout=10)
return True
except Exception as err:
print(f"[{timenow}] Ping Error: " + str(err)) # show error
if retry == True:
return False
else:
# Let's retry after waiting 10 seconds to (hopefully) avoid a unnecessary login requests
print(f"[{timenow}] Retrying in 10 seconds before returning False...")
time.sleep(10)
checkConnection(True)
# gets current cookies from the logon page so that our post requests will be accepted
def getCookie():
response = session.get(url) # send request to url
return session.cookies.get_dict() # return cookie
# Send a post request to the captive portal
def login():
print(f"[{timenow}] Trying to log in...")
ta_id = getCookie()["ta_id"]
session.post(url, data = "ta_id=" + ta_id + "&pwd_only= " + password + "&device_infos=1048%3A1920%3A1080%3A1920&pwd_only_logon_btn=Anmelden++") # idk what the last two parameters do but lets keep them
if checkConnection(False):
print(f"[{timenow}] Logged in!")
else:
print(f"[{timenow}] Failed to log in...\nRetrying in " + interval + " seconds")
# Entry point
print(f"[{timenow}] Checking your connection every {interval} seconds...")
# Keep script alive and check for internet connection every interval seconds
while (True):
timenow = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # refresh time
if not checkConnection(False):
print(f"[{timenow}] No connection!")
login()
# else:
# print(f"[{timenow}] Still connected")
time.sleep(interval)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment