-
Notifications
You must be signed in to change notification settings - Fork 1
/
sudawifi.py
100 lines (84 loc) · 2.83 KB
/
sudawifi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import requests
import base64
import logging
import win32wifi.Win32Wifi as wifi
import os
import json
PATH = "account.json"
userAccount = {"user": None, "pwd": None}
logging.basicConfig(format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s',
level=logging.INFO)
def login(userAccount):
"""
Performing logging by sending POST to http://a.suda.edu.cn
:param userAccount:
:return:Response from portal
"""
url = "http://a.suda.edu.cn/index.php/index/login"
form_dict = {"username": userAccount["user"], "password": base64.b64encode(userAccount["pwd"].encode('utf-8'))}
headers = {"Content-Type": "application/x-www-form-urlencoded", "Connection": "Keep-Alive"}
logging.debug("sending POST")
response = requests.post(url=url, data=form_dict, headers=headers)
response = response.json()
logging.info("From portal:" + response["info"])
return response["info"]
def logout():
"""
send a GET request to logout
:return: Whether the response is valid
"""
url = "http://a.suda.edu.cn/index.php/index/logout"
response = requests.get(url=url)
if response.status_code != 200:
logging.warning("Log out Failed!")
return False
return True
def pingTest():
"""
Test network connection
:return:Whether you are able to visit baidu.com
"""
try:
requests.get("http://www.baidu.com", timeout=2)
except:
return False
return True
def portalScan():
"""
get available ssid
:return:Whether sudawifi is in range
"""
res = wifi.getWirelessInterfaces() # get network interfaces, it's a generator
ssid = [] # prepare the bucket wo store ssids
for i in res: # extract next element from generator
tmp = wifi.getWirelessAvailableNetworkList(i) # get Network info from interface, get a list
for j in tmp: # for each access point store its ssid
ssid.append(j.ssid.decode('utf8')) # decode it because it's a byte like object
# print(ssid)
return "SUDA_WIFI" in ssid, "SUDA_WIFI_5G" in ssid
def ScanAndLogin():
"""
perform network scan then login
:return: success or not
"""
if not portalScan():
logging.warning("SUDA_WIFI not in range")
return "Cannot find SUDA_WIFI"
global userAccount
if os.path.exists("account.json"):
with open("account.json") as f:
userAccount = json.load(f)
if userAccount['user'] == None:
print("username: ", end='')
userAccount["user"] = input()
print("password: ", end='')
userAccount["pwd"] = input()
login(userAccount=userAccount)
if pingTest():
print("SUCCESS !!!!!!!!")
return "Login Success"
else:
print("NOOOOOOOOOO")
return "NOOOOOOOOOO"
if __name__ == '__main__':
ScanAndLogin()