Last active
May 22, 2022 09:17
-
-
Save km4sh/7b97fedf3f5dc28ba12174d65a799702 to your computer and use it in GitHub Desktop.
Auto Login SHU Campus Network
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
import sys | |
import logging | |
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.common.keys import Keys | |
from selenium.webdriver.firefox.options import Options | |
from time import sleep | |
from datetime import date, datetime | |
from random import random | |
username = "<USERNAME>" | |
password = "<PASSWORD>" | |
def open_firefox(): | |
options = Options() | |
options.headless = True | |
driver = webdriver.Firefox(options=options) | |
return driver | |
def wait_until_find(driver, element_id, method, time_out=10): | |
if method == "id": | |
element = WebDriverWait(driver, time_out).until( | |
EC.presence_of_element_located((By.ID, element_id)) | |
) | |
return element | |
elif method == "text": | |
element = WebDriverWait(driver, time_out).until( | |
EC.presence_of_all_elements_located((By.PARTIAL_LINK_TEXT, element_id)) | |
) | |
return element | |
elif method == "button": | |
element = WebDriverWait(driver, time_out).until( | |
EC.presence_of_element_located((By.LINK_TEXT, element_id)) | |
) | |
return element | |
def login(): | |
url = "http://10.10.9.9:8080" | |
driver = open_firefox() | |
driver.get(url) | |
sleep(1) | |
driver.execute_script("arguments[0].click()", driver.find_element_by_id("logo")) | |
username = wait_until_find(driver, "username", "id") | |
sleep(1) | |
username.send_keys(username + Keys.ENTER + password) | |
sleep(1) | |
driver.execute_script("arguments[0].click()", driver.find_element_by_id("loginLink_div")) | |
driver.quit() | |
def check(): | |
url = "http://10.10.9.9:8080" | |
driver = open_firefox() | |
driver.get(url) | |
driver.implicitly_wait(10) | |
try: | |
driver.get(url) | |
online_text = wait_until_find(driver, "userMessage", "id").text | |
driver.quit() | |
except Exception as e: | |
return False | |
if "成功连接" in online_text: | |
return True | |
else: | |
return False | |
if __name__ == "__main__": | |
while True: | |
try: | |
status = check() | |
except Exception as e: | |
with open("./auto_campus_login.log", "a") as f: | |
f.write(str(datetime.now()) + ": " + str(e) + "\n") | |
f.write(str(datetime.now()) + ": " + "Failed to check network.\n") | |
status = False | |
if status: | |
with open("./auto_campus_login.log", "a") as f: | |
f.write(str(datetime.now()) + ": " + "Successfully logged in.\n") | |
sleep(600) | |
else: | |
try: | |
with open("./auto_campus_login.log", "a") as f: | |
f.write(str(datetime.now()) + ": " + "Offline now.\n") | |
f.write(str(datetime.now()) + ": " + "Trying to login.\n") | |
login() | |
except Exception as e: | |
with open("./auto_campus_login.log", "a") as f: | |
f.write(str(datetime.now()) + ": " + str(e) + "\n") | |
f.write(str(datetime.now()) + ": " + "Failed to login.\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment