Bras login, logout, status

 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
import sys
import requests

info1 = dict(username="username", password="password")
onlinelist_url = "http://p.nju.edu.cn/proxy/onlinelist.php"
disconnect_url = "http://p.nju.edu.cn/proxy/disconnect.php"
login_url = "http://p.nju.edu.cn/portal/portal_io.do"
info = dict(action="login")
info.update(info1)


def Usage():
    print "Accepted args: on | off | stas | help | -h | --help."
    print "Usage: python bras.py on"
    print "       python bras.py off"
    print "       python bras.py stat"


def check_args():
    if len(sys.argv) > 2:
        Usage()
        sys.exit(1)


def get_code_and_mac():
    response = requests.post(onlinelist_url, data=info1)
    reply_code = response.json().get("reply_code")
    if reply_code == 501:
        mac = response.json().get("online")[0].get("mac")
        return reply_code, mac
    if reply_code == 502:
        mac = "None"
        return reply_code, mac


def get_acctsessionid():
    response = requests.post(onlinelist_url, data=info1)
    acctsessionid = response.json().get("online")[0].get("acctsessionid")

    return acctsessionid


def disconnect():
    acctsessionid = get_acctsessionid()
    info2 = dict(acctsessionid=acctsessionid)
    info2.update(info1)
    requests.post(disconnect_url, data=info2)


def login_get_reply():
    response = requests.post(login_url, data=info)
    reply_code = response.json().get("reply_code")

    return reply_code

check_args()
arg = sys.argv[1]

if arg == "--help" or arg == "-h" or arg == "help":
    Usage()

# to go online
if arg == 'on':
    login_reply_code = login_get_reply()
    if login_reply_code == 103:
        print "Kicking off the other login..."
        disconnect()
        login()
        print "You are now online!"
    elif login_reply_code == 101:
        print "You are now online!"

# to go offline
if arg == "off":
    reply_code, mac = get_code_and_mac()
    if reply_code == 501:
        disconnect()
        print "Now offline!"
    elif reply_code == 502:
        print "Now offline!"

# check status
if arg == "stat":
    online_reply_code, mac = get_code_and_mac()

    if online_reply_code == 501:
        mac = str(mac)
        macs = {"c8:1f:66:0f:8c:fc": "Dell",
                "76:4d:15:9d:68:3f": "pcDuino",
                "f0:db:d8:1a:2b:aa": "iPad Air"}
        print "Now online!"
        print "Login device is %s. Mac: %s" % (macs.get(mac), mac)
    if online_reply_code == 502:
        print "Offline!"
2014-06-08 14:1947
comments powered by Disqus