-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path.post.py
147 lines (124 loc) · 4.97 KB
/
.post.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from urllib import urlencode
from urllib2 import urlopen
from os import system, devnull, path
import subprocess
import signal
from sys import argv, exit
from time import sleep
import httplib
from json import load
from check import check_status
# For periodic checking of login status
SLEEP_TIME = 120
BASE_URL = "http://172.16.68.6:8090/login.xml"
BROWSERS_DICT = {"0": "", "1": "firefox", "2": "google-chrome"}
CREDENTIALS_FILE = path.dirname(path.realpath(__file__)) + '/.credentials.json'
try:
credentials = load(open(CREDENTIALS_FILE))
user_dict = credentials[0]
browser = BROWSERS_DICT[credentials[1]["web-browser"]]
print browser
except ValueError:
notify_alert("Some error occured!!")
print """Error decoding the JSON file.
[Note: Check that there is a trailing comma after every line in the
credentials.json file, except the last one].
"""
exit()
# Function to send login or logout request
def send_request(request_type, *arg):
if(request_type == 'login'):
print "Initialting login request for USERNAME: %s" % arg[0]
params = urlencode(
{'mode': 191, 'username': arg[0], 'password': arg[1]})
elif(request_type == 'logout'):
print "Initiating logout request.."
params = urlencode({'mode': 193, 'username': arg[0]})
response = urlopen(BASE_URL, params)
return response.read()
# Function to alert the user on successful login/logout
def notify_alert(string):
print string
system('notify-send ' + '"' + string + '"')
# Function to initiate a logout request and kill the ongoing login request
def logout():
for username in user_dict:
data = send_request('logout', username)
if 'off' in data:
notify_alert("Successfully logged off")
system(
"""ps aux | grep python | grep '.post.py login' |
awk '{print $2}' | xargs kill -9""")
break
if __name__ == "__main__":
login_check = False
try:
if "login" in argv:
for username in user_dict:
username = str(username)
user_dict[username] = str(user_dict[username])
data = send_request("login", username, user_dict[username])
if "Maximum" in data:
print "Maximum Login Limit Reached"
elif "exceeded" in data:
print "Data transfer exceeded"
elif "could not" in data:
print "Incorrect username or password, Removing this combination .."
f = open(CREDENTIALS_FILE, "r+")
d = f.readlines()
f.seek(0)
for i in d:
if username not in i:
f.write(i)
else:
print i
f.truncate()
f.close()
elif "into" in data:
notify_alert(
"Successfully logged in with {0}".format(username))
login_check = True
launch_browser = True
try:
ps = subprocess.Popen(
('ps', 'aux'), stdout=subprocess.PIPE)
output = subprocess.check_output(
('pgrep', browser.split("-")[1]), stdin=ps.stdout)
output = output.split("\n")
if (len(output) > 0):
launch_browser = False
except subprocess.CalledProcessError:
pass
except Exception as e:
print e
"""Uncomment this piece of code to
automatically launch your web browser after login:
[Note: web browser will be launched only if
it is not already open, and not otherwise]"""
if launch_browser:
print "Launching " + browser + ".."
with open(devnull, 'wb') as devnull:
subprocess.Popen(
browser, stdout=devnull,
stderr=subprocess.STDOUT)
print "Press Ctrl + C to logout"
break
else:
print "Request Failed, Please try again later"
login_check = False
while login_check:
sleep(SLEEP_TIME)
if not check_status(username):
print "Logging in again"
data = send_request("login", username, user_dict[username])
except KeyboardInterrupt:
logout()
sys.exit()
except httplib.BadStatusLine:
notify_alert(
"Bad Connection. Please check your connection and try again.")
sys.exit()
except Exception as e:
print e
if "logout" in argv:
logout()