Skip to content

Instantly share code, notes, and snippets.

@louchenyao
Last active November 7, 2018 15:42
Show Gist options
  • Save louchenyao/53590e9023dc233bd5ee725e73675de3 to your computer and use it in GitHub Desktop.
Save louchenyao/53590e9023dc233bd5ee725e73675de3 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python3
import requests
import subprocess
import time
USER = "USERNAME"
MD5 = "YOUR_PASSOWRD_MD5" # echo -n "password" | md5
AUTH4 = "https://auth4.tsinghua.edu.cn/do_login.php?action=login&username=%s&password={MD5_HEX}%s&ac_id=1" % (USER, MD5)
NET = "https://net.tsinghua.edu.cn/do_login.php?action=login&username=%s&password={MD5_HEX}%s&ac_id=1" % (USER, MD5)
def is_wired():
p = subprocess.Popen("ifconfig",stdout=subprocess.PIPE)
out = p.stdout.read().decode()
return "59.66" in out
def post(url, silent=False):
if not silent: print(url)
r = requests.post(url)
if not silent: print(r.text)
return r.text
def get(url, silent=False, timeout=None):
if not silent: print(url)
r = requests.get(url, timeout=timeout)
if not silent: print(r.text)
return r.text
def myip():
txt = get("https://myip.ipip.net", timeout=5, silent=True)
ip = txt.split()[1][3:]
return ip
def is_online():
try:
txt = get("https://net.tsinghua.edu.cn/rad_user_info.php", timeout=5, silent=True)
return USER in txt
except Exception:
return False
def login():
if is_wired():
post(AUTH4)
post(NET)
else:
post(NET)
print("IP:", myip())
def keep_login():
while True:
if not is_online():
login()
time.sleep(10)
if __name__ == '__main__':
login()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment