-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecnu_net.py
75 lines (60 loc) · 1.99 KB
/
ecnu_net.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
#!/usr/bin/env python3
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from argparse import ArgumentParser
# create the parser
parser = ArgumentParser(description='Script description')
## add arguments
parser.add_argument('--user', '-u', help='Username')
parser.add_argument('--password', '-p', help='Password')
parser.add_argument('--mode', '-m', help='Login or logout', default='login')
# parse the arguments
args = parser.parse_args()
## set values to variables
assert args.user is not None and args.password is not None, "Username and password are required"
username = args.user
password = args.password
mode = args.mode.lower()
LoginPageUrl = "https://login.ecnu.edu.cn/srun_portal_pc?ac_id=1&theme=pro"
# Ping test: return 0 if success
pingtest = lambda: os.system("ping -c 2 -W 3 baidu.com")
# Login
def Login(delay=5):
if pingtest() == 0: # the network is connected
print("Already connected to the Internet")
exit()
# start the device
driver = webdriver.PhantomJS(executable_path="./phantomjs/bin/phantomjs")
driver.get(LoginPageUrl)
time.sleep(delay)
# set username
driver.find_element(By.XPATH, '//*[@id="username"]').send_keys(username)
# set password
driver.find_element(By.XPATH, '//*[@id="password"]').send_keys(password)
# click to login
driver.find_element(By.XPATH, '//*[@id="login-account"]').click()
# wait for the response
time.sleep(2)
def Logout():
# start the device
driver = webdriver.PhantomJS(executable_path="./phantomjs/bin/phantomjs")
driver.get(LoginPageUrl)
time.sleep(3)
# click to logout
driver.find_element(By.XPATH, '//*[@id="logout"]').click()
# wait for the response
time.sleep(2)
def main():
if args.mode == 'login':
Login()
exit()
elif args.mode == 'logout':
Logout()
exit()
else:
print("Unknown mode: " + args.mode)
exit()
if __name__ == '__main__' :
main()