├── .gitignore ├── readme.md ├── srun_login_py2.py ├── srun_login_py3.py └── srun_login_req.py /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # BIT 校园网用户认证 2 | 3 | 上网不涉密,涉密不上网 4 | 5 | login: 6 | ```python 7 | srun_login(username, password) 8 | ``` 9 | 10 | logout: 11 | ```python 12 | srun_login(username, action="logout") 13 | ``` 14 | 15 | 16 | # 注意 17 | 这里包含3个.py文件, 三个版本功能一致 18 | 19 | 其中"srun_login_req.py", 使用了requests, py2/py3环境下均可使用 20 | 21 | 对于某些无法使用requests的环境(例如路由器), 替代方案为使用urllib的"srun_login_py3.py"或"srun_login_py2.py" -------------------------------------------------------------------------------- /srun_login_py2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | # -*-coding:utf-8-*- 3 | 4 | import base64 5 | import hashlib 6 | import hmac 7 | import json 8 | import os 9 | import time 10 | import re 11 | import urllib, urllib2 12 | 13 | class hrequests: 14 | def __init__(self,content='',url=''): 15 | self.content=content 16 | self.url=url 17 | def get(self,url,params=None): 18 | if type(params)==dict: 19 | params=urllib.urlencode(params) 20 | request = urllib2.Request(url=url, data=params) 21 | response =urllib2.urlopen(request) 22 | #encoding = response.headers['content-type'].split('charset=')[-1] 23 | return hrequests(response.read(),response.geturl()) 24 | 25 | requests=hrequests() 26 | 27 | def char_code_at(stri, index): 28 | return 0 if index >= len(stri) else ord(stri[index]) 29 | 30 | 31 | def xencode(msg, key): 32 | ''' 33 | xEncode 34 | ''' 35 | def s(a, b): 36 | c = len(a) 37 | v = [] 38 | for i in range(0, c, 4): 39 | v.append(char_code_at(a, i) | (char_code_at(a, i+1) << 8) | 40 | (char_code_at(a, i+2) << 16) | (char_code_at(a, i+3) << 24)) 41 | if b: 42 | v.append(c) 43 | return v 44 | 45 | def l(a, b): 46 | d = len(a) 47 | c = (d-1) << 2 48 | if b: 49 | m = a[d-1] 50 | if (m < c-3) or (m > c): 51 | return None 52 | c = m 53 | for i in range(0, d): 54 | a[i] = ''.join([chr(a[i] & 0xff), chr((a[i] >> 8) & 0xff), chr( 55 | (a[i] >> 16) & 0xff), chr((a[i] >> 24) & 0xff)]) 56 | if b: 57 | return (''.join(a))[0:c] 58 | else: 59 | return ''.join(a) 60 | 61 | if msg == "": 62 | return "" 63 | v = s(msg, True) 64 | k = s(key, False) 65 | # print(v) 66 | # print(k) 67 | n = len(v) - 1 68 | z = v[n] 69 | y = v[0] 70 | c = 0x86014019 | 0x183639A0 71 | m = 0 72 | e = 0 73 | p = 0 74 | q = 6 + 52 // (n + 1) 75 | d = 0 76 | while 0 < q: 77 | q -= 1 78 | d = d + c & (0x8CE0D9BF | 0x731F2640) 79 | e = d >> 2 & 3 80 | for p in range(0, n): 81 | y = v[p+1] 82 | m = z >> 5 ^ y << 2 83 | m += (y >> 3 ^ z << 4) ^ (d ^ y) 84 | m += k[(p & 3) ^ e] ^ z 85 | z = v[p] = v[p] + m & (0xEFB8D130 | 0x10472ECF) 86 | y = v[0] 87 | m = z >> 5 ^ y << 2 88 | m += (y >> 3 ^ z << 4) ^ (d ^ y) 89 | m += k[(n & 3) ^ e] ^ z 90 | z = v[n] = v[n] + m & (0xBB390742 | 0x44C6F8BD) 91 | # print(v) 92 | return l(v, False) 93 | 94 | 95 | def get_json(url, data): 96 | '''Http GET, return json 97 | ''' 98 | callback = "jsonp%s" % int(time.time()*1000) 99 | data["callback"] = callback 100 | 101 | response = requests.get(url, data) 102 | response_content = response.content.decode('utf-8')[len(callback)+1:-1] 103 | response_json = json.loads(response_content) 104 | 105 | return response_json 106 | 107 | 108 | def srun_login(username, password=None, action='login'): 109 | '''srun login and logout 110 | 111 | Args: 112 | username: username 113 | passwprd: password 114 | action: 'login' or 'logout' 115 | 116 | Returns: 117 | a json object. 118 | ''' 119 | def data_info(get_data, token): 120 | if get_data['action'] == 'login': 121 | x_encode_json = { 122 | "username": get_data['username'], 123 | "password": get_data['password'], 124 | "ip": get_data['ip'], 125 | "acid": get_data['ac_id'], 126 | "enc_ver": enc 127 | } 128 | else: 129 | x_encode_json = { 130 | "username": get_data['username'], 131 | "ip": get_data['ip'], 132 | "acid": get_data['ac_id'], 133 | "enc_ver": enc 134 | } 135 | 136 | x_encode_str = json.dumps(x_encode_json, separators=(',', ':')) 137 | x_encode_key = token 138 | x_encode_res = xencode(x_encode_str, x_encode_key) 139 | #print "x_encode('%s', '%s')" % (x_encode_str, x_encode_key) 140 | #print 'x_encode_res(len: %s): %s' % (len(x_encode_res), x_encode_res) 141 | #print "x_encode_res unicode:", [ord(s) for s in x_encode_res] 142 | 143 | # base64_encode 144 | mapping = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", 145 | "LVoJPiCN2R8G90yg+hmFHuacZ1OWMnrsSTXkYpUq/3dlbfKwv6xztjI7DeBE45QA=")) 146 | b64_res = base64.b64encode(x_encode_res) 147 | base64_encode_res = ''.join([mapping[b] for b in b64_res]) 148 | # print('base64 encode res(len: %s): %s' % (len(base64_encode_res), base64_encode_res)) 149 | 150 | return "{SRBX1}" + base64_encode_res 151 | 152 | def pwd_hmd5(password, token): 153 | hmac_key = token.encode('utf-8') 154 | hmac_msg = password.encode('utf-8') 155 | hmd5 = hmac.new(hmac_key, hmac_msg, digestmod=hashlib.md5).hexdigest() 156 | # print(hmd5) 157 | return '{MD5}' + hmd5 158 | 159 | def checksum(get_data, token): 160 | if get_data['action'] == 'login': 161 | str_list = ['', get_data['username'], get_data['password'][5:], 162 | get_data['ac_id'], get_data['ip'], str(n), str(type_), get_data['info']] 163 | else: 164 | str_list = ['', get_data['username'], get_data['ac_id'], 165 | get_data['ip'], str(n), str(type_), get_data['info']] 166 | chksum_str = token.join(str_list) 167 | chksum = hashlib.sha1(chksum_str.encode('utf-8')).hexdigest() 168 | return chksum 169 | 170 | if action not in ['login', 'logout']: 171 | print('action must be "login" or "logout".') 172 | return 173 | enc = "srun_bx1" 174 | n = 200 175 | type_ = 1 176 | get_challenge_url = "http://10.0.0.55/cgi-bin/get_challenge" 177 | srun_portal_url = "http://10.0.0.55/cgi-bin/srun_portal" 178 | url = 'http://10.0.0.55' 179 | r = requests.get(url) 180 | ac_id=re.findall(r'index_(\d*).html',r.url)[0] 181 | if action == 'login': 182 | get_data = { 183 | "action": action, 184 | "username": username, 185 | "password": password, 186 | "ac_id": ac_id, 187 | "ip": '', 188 | "info": '', 189 | "chksum": '', 190 | "n": n, 191 | "type": type_ 192 | } 193 | else: 194 | get_data = { 195 | "action": action, 196 | "username": username, 197 | # "password": password, # logout, 198 | "ac_id": ac_id, 199 | "ip": '', 200 | "info": '', 201 | "chksum": '', 202 | "n": n, 203 | "type": type_ 204 | } 205 | # get token 206 | challenge_json = get_json( 207 | get_challenge_url, {"username": get_data['username']}) 208 | if challenge_json['res'] != "ok": 209 | print 'Error getting challenge. %s failed.' % action 210 | print 'Server response:\n%s' % json.dumps(challenge_json, indent=4) 211 | return 212 | token = challenge_json['challenge'] 213 | get_data['ip'] = challenge_json['client_ip'] 214 | get_data['info'] = data_info(get_data, token) 215 | if action == 'login': 216 | get_data['password'] = pwd_hmd5('', token) 217 | # get_data['password'] = pwd_hmd5(get_data['password'], token) # srun's bug 218 | 219 | get_data['chksum'] = checksum(get_data, token) 220 | 221 | # print('get data: %s' % json.dumps(get_data, indent=4)) 222 | res = get_json(srun_portal_url, get_data) 223 | # print("Server response: %s" % json.dumps(res, indent=4)) 224 | 225 | if res['error'] == 'ok': 226 | print '%s success.' % action 227 | else: 228 | print "%s failed.\n%s %s" % (action, res['error'], res['error_msg']) 229 | return res 230 | 231 | if __name__ == "__main__": 232 | username = "username" 233 | password = "password" 234 | srun_login(username, password) 235 | #srun_login(username, action="logout") 236 | 237 | 238 | -------------------------------------------------------------------------------- /srun_login_py3.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*-coding:utf-8-*- 3 | 4 | import base64 5 | import hashlib 6 | import hmac 7 | import json 8 | import os 9 | import time 10 | import re 11 | import urllib.request 12 | 13 | class hrequests: 14 | def __init__(self,content='',url=''): 15 | self.content=content 16 | self.url=url 17 | def get(self,url,params=None): 18 | if type(params)==dict: 19 | params=urllib.parse.urlencode(params).encode('ascii') 20 | request = urllib.request.Request(url=url, data=params) 21 | response = urllib.request.urlopen(request) 22 | #encoding = response.headers['content-type'].split('charset=')[-1] 23 | return hrequests(response.read(),response.geturl()) 24 | 25 | requests=hrequests() 26 | 27 | def char_code_at(stri, index): 28 | return 0 if index >= len(stri) else ord(stri[index]) 29 | 30 | 31 | def xencode(msg: str, key): 32 | ''' 33 | xEncode 34 | ''' 35 | def s(a: str, b: bool): 36 | c = len(a) 37 | v = [] 38 | for i in range(0, c, 4): 39 | v.append(char_code_at(a, i) | (char_code_at(a, i+1) << 8) | 40 | (char_code_at(a, i+2) << 16) | (char_code_at(a, i+3) << 24)) 41 | if b: 42 | v.append(c) 43 | return v 44 | 45 | def l(a, b): 46 | d = len(a) 47 | c = (d-1) << 2 48 | if b: 49 | m = a[d-1] 50 | if (m < c-3) or (m > c): 51 | return None 52 | c = m 53 | for i in range(0, d): 54 | a[i] = ''.join([chr(a[i] & 0xff), chr((a[i] >> 8) & 0xff), chr( 55 | (a[i] >> 16) & 0xff), chr((a[i] >> 24) & 0xff)]) 56 | if b: 57 | return (''.join(a))[0:c] 58 | else: 59 | return ''.join(a) 60 | 61 | if msg == "": 62 | return "" 63 | v = s(msg, True) 64 | k = s(key, False) 65 | # print(v) 66 | # print(k) 67 | n = len(v) - 1 68 | z = v[n] 69 | y = v[0] 70 | c = 0x86014019 | 0x183639A0 71 | m = 0 72 | e = 0 73 | p = 0 74 | q = 6 + 52 // (n + 1) 75 | d = 0 76 | while 0 < q: 77 | q -= 1 78 | d = d + c & (0x8CE0D9BF | 0x731F2640) 79 | e = d >> 2 & 3 80 | for p in range(0, n): 81 | y = v[p+1] 82 | m = z >> 5 ^ y << 2 83 | m += (y >> 3 ^ z << 4) ^ (d ^ y) 84 | m += k[(p & 3) ^ e] ^ z 85 | z = v[p] = v[p] + m & (0xEFB8D130 | 0x10472ECF) 86 | y = v[0] 87 | m = z >> 5 ^ y << 2 88 | m += (y >> 3 ^ z << 4) ^ (d ^ y) 89 | m += k[(n & 3) ^ e] ^ z 90 | z = v[n] = v[n] + m & (0xBB390742 | 0x44C6F8BD) 91 | # print(v) 92 | return l(v, False) 93 | 94 | 95 | def get_json(url, data): 96 | '''Http GET, return json 97 | ''' 98 | callback = "jsonp%s" % int(time.time()*1000) 99 | data["callback"] = callback 100 | 101 | response = requests.get(url, data) 102 | response_content = response.content.decode('utf-8')[len(callback)+1:-1] 103 | response_json = json.loads(response_content) 104 | 105 | return response_json 106 | 107 | 108 | def srun_login(username, password=None, action='login'): 109 | '''srun login and logout 110 | 111 | Args: 112 | username: username 113 | passwprd: password 114 | action: 'login' or 'logout' 115 | 116 | Returns: 117 | a json object. 118 | ''' 119 | def data_info(get_data, token): 120 | if get_data['action'] == 'login': 121 | x_encode_json = { 122 | "username": get_data['username'], 123 | "password": get_data['password'], 124 | "ip": get_data['ip'], 125 | "acid": get_data['ac_id'], 126 | "enc_ver": enc 127 | } 128 | else: 129 | x_encode_json = { 130 | "username": get_data['username'], 131 | "ip": get_data['ip'], 132 | "acid": get_data['ac_id'], 133 | "enc_ver": enc 134 | } 135 | 136 | x_encode_str = json.dumps(x_encode_json, separators=(',', ':')) 137 | x_encode_key = token 138 | x_encode_res = xencode(x_encode_str, x_encode_key) 139 | # print("x_encode('%s', '%s')" % (x_encode_str, x_encode_key)) 140 | # print('x_encode_res(len: %s): %s' % (len(x_encode_res), x_encode_res)) 141 | # print("x_encode_res unicode:", [ord(s) for s in x_encode_res]) 142 | 143 | # base64_encode 144 | mapping = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", 145 | "LVoJPiCN2R8G90yg+hmFHuacZ1OWMnrsSTXkYpUq/3dlbfKwv6xztjI7DeBE45QA=")) 146 | b64_res = base64.b64encode( 147 | bytes([ord(s) for s in x_encode_res])).decode() 148 | base64_encode_res = ''.join([mapping[b] for b in b64_res]) 149 | # print('base64 encode res(len: %s): %s' % (len(base64_encode_res), base64_encode_res)) 150 | 151 | return "{SRBX1}" + base64_encode_res 152 | 153 | def pwd_hmd5(password, token): 154 | hmac_key = token.encode('utf-8') 155 | hmac_msg = password.encode('utf-8') 156 | hmd5 = hmac.new(hmac_key, hmac_msg, digestmod='MD5').hexdigest() 157 | # print(hmd5) 158 | return '{MD5}' + hmd5 159 | 160 | def checksum(get_data, token): 161 | if get_data['action'] == 'login': 162 | str_list = ['', get_data['username'], get_data['password'][5:], 163 | get_data['ac_id'], get_data['ip'], str(n), str(type_), get_data['info']] 164 | else: 165 | str_list = ['', get_data['username'], get_data['ac_id'], 166 | get_data['ip'], str(n), str(type_), get_data['info']] 167 | chksum_str = token.join(str_list) 168 | chksum = hashlib.sha1(chksum_str.encode('utf-8')).hexdigest() 169 | return chksum 170 | 171 | if action not in ['login', 'logout']: 172 | print('action must be "login" or "logout".') 173 | return 174 | enc = "srun_bx1" 175 | n = 200 176 | type_ = 1 177 | get_challenge_url = "http://10.0.0.55/cgi-bin/get_challenge" 178 | srun_portal_url = "http://10.0.0.55/cgi-bin/srun_portal" 179 | url = 'http://10.0.0.55' 180 | r = requests.get(url) 181 | ac_id=re.findall(r'index_(\d*).html',r.url)[0] 182 | if action == 'login': 183 | get_data = { 184 | "action": action, 185 | "username": username, 186 | "password": password, 187 | "ac_id": ac_id, 188 | "ip": '', 189 | "info": '', 190 | "chksum": '', 191 | "n": n, 192 | "type": type_ 193 | } 194 | else: 195 | get_data = { 196 | "action": action, 197 | "username": username, 198 | # "password": password, # logout, 199 | "ac_id": ac_id, 200 | "ip": '', 201 | "info": '', 202 | "chksum": '', 203 | "n": n, 204 | "type": type_ 205 | } 206 | # get token 207 | challenge_json = get_json( 208 | get_challenge_url, {"username": get_data['username']}) 209 | if challenge_json['res'] != "ok": 210 | print('Error getting challenge. %s failed.' % action) 211 | print('Server response:\n%s' % json.dumps(challenge_json, indent=4)) 212 | return 213 | token = challenge_json['challenge'] 214 | get_data['ip'] = challenge_json['client_ip'] 215 | print(get_data) 216 | print(token) 217 | get_data['info'] = data_info(get_data, token) 218 | print(get_data['info']) 219 | if action == 'login': 220 | get_data['password'] = pwd_hmd5('', token) 221 | # get_data['password'] = pwd_hmd5(get_data['password'], token) # srun's bug 222 | 223 | get_data['chksum'] = checksum(get_data, token) 224 | 225 | # print('get data: %s' % json.dumps(get_data, indent=4)) 226 | res = get_json(srun_portal_url, get_data) 227 | # print("Server response: %s" % json.dumps(res, indent=4)) 228 | 229 | if res['error'] == 'ok': 230 | print('%s success.' % action) 231 | else: 232 | print("%s failed.\n%s %s" % (action, res['error'], res['error_msg'])) 233 | 234 | return res 235 | 236 | 237 | if __name__ == "__main__": 238 | username = "username" 239 | password = "password" 240 | 241 | srun_login(username, password) 242 | #srun_login(username, action="logout") 243 | 244 | 245 | -------------------------------------------------------------------------------- /srun_login_req.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*-coding:utf-8-*- 3 | 4 | import base64 5 | import hashlib 6 | import hmac 7 | import json 8 | import os 9 | import time 10 | import re 11 | import requests 12 | 13 | 14 | def char_code_at(stri, index): 15 | return 0 if index >= len(stri) else ord(stri[index]) 16 | 17 | 18 | def xencode(msg: str, key): 19 | ''' 20 | xEncode 21 | ''' 22 | def s(a: str, b: bool): 23 | c = len(a) 24 | v = [] 25 | for i in range(0, c, 4): 26 | v.append(char_code_at(a, i) | (char_code_at(a, i+1) << 8) | 27 | (char_code_at(a, i+2) << 16) | (char_code_at(a, i+3) << 24)) 28 | if b: 29 | v.append(c) 30 | return v 31 | 32 | def l(a, b): 33 | d = len(a) 34 | c = (d-1) << 2 35 | if b: 36 | m = a[d-1] 37 | if (m < c-3) or (m > c): 38 | return None 39 | c = m 40 | for i in range(0, d): 41 | a[i] = ''.join([chr(a[i] & 0xff), chr((a[i] >> 8) & 0xff), chr( 42 | (a[i] >> 16) & 0xff), chr((a[i] >> 24) & 0xff)]) 43 | if b: 44 | return (''.join(a))[0:c] 45 | else: 46 | return ''.join(a) 47 | 48 | if msg == "": 49 | return "" 50 | v = s(msg, True) 51 | k = s(key, False) 52 | # print(v) 53 | # print(k) 54 | n = len(v) - 1 55 | z = v[n] 56 | y = v[0] 57 | c = 0x86014019 | 0x183639A0 58 | m = 0 59 | e = 0 60 | p = 0 61 | q = 6 + 52 // (n + 1) 62 | d = 0 63 | while 0 < q: 64 | q -= 1 65 | d = d + c & (0x8CE0D9BF | 0x731F2640) 66 | e = d >> 2 & 3 67 | for p in range(0, n): 68 | y = v[p+1] 69 | m = z >> 5 ^ y << 2 70 | m += (y >> 3 ^ z << 4) ^ (d ^ y) 71 | m += k[(p & 3) ^ e] ^ z 72 | z = v[p] = v[p] + m & (0xEFB8D130 | 0x10472ECF) 73 | y = v[0] 74 | m = z >> 5 ^ y << 2 75 | m += (y >> 3 ^ z << 4) ^ (d ^ y) 76 | m += k[(n & 3) ^ e] ^ z 77 | z = v[n] = v[n] + m & (0xBB390742 | 0x44C6F8BD) 78 | # print(v) 79 | return l(v, False) 80 | 81 | 82 | def get_json(url, data): 83 | '''Http GET, return json 84 | ''' 85 | callback = "jsonp%s" % int(time.time()*1000) 86 | data["callback"] = callback 87 | 88 | response = requests.get(url, data) 89 | response_content = response.content.decode('utf-8')[len(callback)+1:-1] 90 | response_json = json.loads(response_content) 91 | 92 | return response_json 93 | 94 | 95 | def srun_login(username, password=None, action='login'): 96 | '''srun login and logout 97 | Args: 98 | username: username 99 | password: password 100 | action: 'login' or 'logout' 101 | Returns: 102 | a json object. 103 | ''' 104 | def data_info(get_data, token): 105 | if get_data['action'] == 'login': 106 | x_encode_json = { 107 | "username": get_data['username'], 108 | "password": get_data['password'], 109 | "ip": get_data['ip'], 110 | "acid": get_data['ac_id'], 111 | "enc_ver": enc 112 | } 113 | else: 114 | x_encode_json = { 115 | "username": get_data['username'], 116 | "ip": get_data['ip'], 117 | "acid": get_data['ac_id'], 118 | "enc_ver": enc 119 | } 120 | 121 | x_encode_str = json.dumps(x_encode_json, separators=(',', ':')) 122 | x_encode_key = token 123 | x_encode_res = xencode(x_encode_str, x_encode_key) 124 | # print("x_encode('%s', '%s')" % (x_encode_str, x_encode_key)) 125 | # print('x_encode_res(len: %s): %s' % (len(x_encode_res), x_encode_res)) 126 | # print("x_encode_res unicode:", [ord(s) for s in x_encode_res]) 127 | 128 | # base64_encode 129 | mapping = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", 130 | "LVoJPiCN2R8G90yg+hmFHuacZ1OWMnrsSTXkYpUq/3dlbfKwv6xztjI7DeBE45QA=")) 131 | b64_res = base64.b64encode( 132 | bytes([ord(s) for s in x_encode_res])).decode() 133 | base64_encode_res = ''.join([mapping[b] for b in b64_res]) 134 | # print('base64 encode res(len: %s): %s' % (len(base64_encode_res), base64_encode_res)) 135 | 136 | return "{SRBX1}" + base64_encode_res 137 | 138 | def pwd_hmd5(password, token): 139 | hmac_key = token.encode('utf-8') 140 | hmac_msg = password.encode('utf-8') 141 | hmd5 = hmac.new(hmac_key, hmac_msg, digestmod='MD5').hexdigest() 142 | # print(hmd5) 143 | return '{MD5}' + hmd5 144 | 145 | def checksum(get_data, token): 146 | if get_data['action'] == 'login': 147 | str_list = ['', get_data['username'], get_data['password'][5:], 148 | get_data['ac_id'], get_data['ip'], str(n), str(type_), get_data['info']] 149 | else: 150 | str_list = ['', get_data['username'], get_data['ac_id'], 151 | get_data['ip'], str(n), str(type_), get_data['info']] 152 | chksum_str = token.join(str_list) 153 | chksum = hashlib.sha1(chksum_str.encode('utf-8')).hexdigest() 154 | return chksum 155 | 156 | if action not in ['login', 'logout']: 157 | print('action must be "login" or "logout".') 158 | return 159 | enc = "srun_bx1" 160 | n = 200 161 | type_ = 1 162 | get_challenge_url = "http://10.0.0.55/cgi-bin/get_challenge" 163 | srun_portal_url = "http://10.0.0.55/cgi-bin/srun_portal" 164 | url = 'http://detectportal.firefox.com/success.txt' 165 | #Check if Redirect, when not, set to default 166 | try: 167 | r = requests.get(url, timeout=0.1) 168 | ac_id=re.findall(r'index_(\d*).html',r.url)[0] 169 | except requests.exceptions.Timeout: 170 | ac_id=1 171 | if not ac_id: 172 | ac_id=1 173 | ac_id=str(ac_id) 174 | if action == 'login': 175 | get_data = { 176 | "action": action, 177 | "username": username, 178 | "password": password, 179 | "ac_id": ac_id, 180 | "ip": '', 181 | "info": '', 182 | "chksum": '', 183 | "n": n, 184 | "type": type_ 185 | } 186 | else: 187 | get_data = { 188 | "action": action, 189 | "username": username, 190 | # "password": password, # logout, 191 | "ac_id": ac_id, 192 | "ip": '', 193 | "info": '', 194 | "chksum": '', 195 | "n": n, 196 | "type": type_ 197 | } 198 | # get token 199 | challenge_json = get_json( 200 | get_challenge_url, {"username": get_data['username']}) 201 | if challenge_json['res'] != "ok": 202 | print('Error getting challenge. %s failed.' % action) 203 | print('Server response:\n%s' % json.dumps(challenge_json, indent=4)) 204 | return 205 | token = challenge_json['challenge'] 206 | get_data['ip'] = challenge_json['client_ip'] 207 | get_data['info'] = data_info(get_data, token) 208 | 209 | if action == 'login': 210 | get_data['password'] = pwd_hmd5('', token) 211 | # get_data['password'] = pwd_hmd5(get_data['password'], token) # srun's bug 212 | 213 | get_data['chksum'] = checksum(get_data, token) 214 | 215 | # print('get data: %s' % json.dumps(get_data, indent=4)) 216 | res = get_json(srun_portal_url, get_data) 217 | # print("Server response: %s" % json.dumps(res, indent=4)) 218 | 219 | if res['error'] == 'ok': 220 | print('%s success.' % action) 221 | else: 222 | print("%s failed.\n%s %s" % (action, res['error'], res['error_msg'])) 223 | 224 | return res 225 | 226 | 227 | if __name__ == "__main__": 228 | username = "username" 229 | password = "password" 230 | 231 | srun_login(username, password) 232 | #srun_login(username, action="logout") 233 | --------------------------------------------------------------------------------