-
Notifications
You must be signed in to change notification settings - Fork 1
/
wannaknow.py
285 lines (215 loc) · 9.36 KB
/
wannaknow.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import curses
import time, sys
from math import floor
from curses import wrapper
from stats import getStats
from renders import proc_row, conn_row, usr_row, pck_row
from renders import usr_head, proc_head, pck_head, net_head, usage
def main(stdscr):
# Clear screen
stdscr.clear()
# Curses Settings // DO NOT CHANGE
curses.curs_set(False)
stdscr.nodelay(True)
black = curses.COLOR_BLACK
curses.init_pair(1, curses.COLOR_WHITE, black)
curses.init_pair(2, curses.COLOR_RED, black)
curses.init_pair(3, curses.COLOR_GREEN, black)
curses.init_pair(4, curses.COLOR_BLUE, black)
curses.init_pair(5, curses.COLOR_YELLOW, black)
curses.init_pair(6, curses.COLOR_CYAN, black)
curses.init_pair(7, curses.COLOR_MAGENTA, black)
curses.init_pair(8, curses.COLOR_BLUE, curses.COLOR_WHITE)
curses.init_pair(9, black, curses.COLOR_BLUE)
while True:
try:
stats = getStats()
try:
users = stats['users']
interfaces = stats['network']['interfaces']
connections = stats['network']['connections']
procs = stats['processes']
except TypeError:
pass
stdscr.clear() # Dont Change!
# Define section specific variables
num_connects = len(connections.items())
num_closed = 0
for i in range(0, num_connects):
if connections[i]['status'] == 'CLOSE_WAIT':
num_closed += 1
num_procs = len(procs.items())
# Manipulate row numbers according to user input
try:
row_conn
except NameError:
row_conn = 0
try:
row_proc
except NameError:
row_proc = 0
k = stdscr.getch()
if k == ord('a'):
if num_connects > connect_height:
row_conn = connect_height
curses.flushinp()
elif k == ord('s'):
if num_connects > connect_height * 2:
row_conn = connect_height * 2
curses.flushinp()
elif k == ord('d'):
if num_connects > connect_height * 3:
row_conn = connect_height * 3
curses.flushinp()
elif k == ord('w'):
if num_connects > connect_height * 4:
row_conn = connect_height * 4
curses.flushinp()
elif k == ord('z'):
if num_connects > connect_height * 5:
row_conn = connect_height * 5
curses.flushinp()
elif k == ord('x'):
row_conn = 0
curses.flushinp()
elif k == ord('j'):
if num_procs >= term_size[0] * 2 - 2:
row_proc = term_size[0] * 2 - 4
curses.flushinp()
elif k == ord('k'):
if num_procs >= term_size[0] * 4 - 2:
row_proc = term_size[0] * 4 - 8
curses.flushinp()
elif k == ord('l'):
if num_procs >= term_size[0] * 6 - 2:
row_proc = term_size[0] * 6 - 12
curses.flushinp()
elif k == ord('i'):
if num_procs >= term_size[0] * 8 - 2:
row_proc = term_size[0] * 8 - 16
curses.flushinp()
elif k == ord('m'):
if num_procs >= term_size[0] * 10 - 2:
row_proc = term_size[0] * 10 - 20
curses.flushinp()
elif k == ord('b'):
row_proc = 0
curses.flushinp()
# Define max width and height
try:
term_size
except NameError:
term_size = stdscr.getmaxyx()
resized = curses.is_term_resized(term_size[0], term_size[1])
# Recalculate if terminal has been resized
if resized == True:
term_size = stdscr.getmaxyx()
# Create sub-windows
win_width = term_size[1] / 3
term_1 = stdscr.subwin(term_size[0], floor(win_width) - 2, 0, 0)
term_2 = stdscr.subwin(term_size[0], floor(win_width), 0, floor(win_width) - 3)
term_3 = stdscr.subwin(term_size[0], floor(win_width), 0, floor(win_width * 2) - 3)
# Left column of terminal screen (term_1)
curr = term_1.getyx()
col_2 = (curr[0], curr[1] + (floor(win_width/5)) - 5)
col_3 = (curr[0], curr[1] + (floor((win_width/5)*2)) - 4)
col_4 = (curr[0], curr[1] + (floor((win_width/5)*3)) - 7)
col_5 = (curr[0], curr[1] + (floor((win_width/5)*4)) - 3)
usr_head(term_1, col_2, col_3, col_4, col_5, 4)
for c in range(0, len(users.items())):
user = users[c]
curr = term_1.getyx()
col_2 = (curr[0], curr[1] + (floor(win_width/5)) - 2)
col_3 = (curr[0], curr[1] + (floor((win_width/5)*2)) - 1)
col_4 = (curr[0], curr[1] + (floor((win_width/5)*3)) - 5)
col_5 = (curr[0], curr[1] + floor((win_width/5)*4))
usr_row(term_1, user, col_2, col_3, col_4, col_5, 3, 1)
# Network Info Display (Packets)
curr = term_1.getyx()
col_2 = (curr[0], curr[1] + floor(win_width/5))
# Save the current location as coordinates to print the usage later on.
col_instruct = (col_2[0] + 1, col_2[1] + floor((win_width/5))*2 - 3)
pck_head(term_1, col_2, 8, 4, win_width)
for i in range(0, len(interfaces.items())):
curr = term_1.getyx()
col_2 = (curr[0], curr[1] + floor(win_width/5))
pck_row(term_1, interfaces, col_2, i, 3, 2)
net_head(term_1, num_connects, num_closed)
curr = term_1.getyx()
connect_height = term_size[0] - curr[0] - 1
col_2 = (curr[0], curr[1] + (floor(win_width/5)) - 5)
if row_conn > 0:
row_c = row_conn
else:
row_c = 0
h1 = term_size[0] - 1
for c in range(curr[0], h1):
if row_c == num_connects:
break
cur = term_1.getyx()
col_2 = (cur[0], cur[1] + (floor(win_width/5)) - 4)
col_3 = (cur[0], cur[1] + (floor((win_width/5)*2)) + 3)
col_4 = (cur[0], cur[1] + (floor((win_width/5)*3)) - 1)
conn_row(term_1, connections, col_2, col_3, col_4, row_c, 3, 1, 4, 5, 9)
row_c += 1
# Print usage instructions
usage(term_1, col_instruct)
term_1.refresh()
# Middle column of terminal screen (term_2)
#Processes
curr = term_2.getyx()
col_2 = (curr[0], curr[1] + (floor(win_width/5)) - 5)
col_3 = (curr[0], curr[1] + (floor((win_width/5)*3)))
proc_head(term_2, col_2, col_3, 7, 8)
curr = term_2.getyx()
if row_proc > 0:
row_p = row_proc
else:
row_p = 0
h2 = term_size[0] - 1
for c in range(curr[0], h2):
if row_p == num_procs:
break
curr = term_2.getyx()
col_2 = (curr[0], curr[1] + (floor(win_width/5)) - 6)
col_3 = (curr[0], curr[1] + (floor((win_width/5)*3)))
col_4 = (curr[0], curr[1] + (floor((win_width/5)*4)) + 3)
if procs[row_p]['user'] == 'root':
proc_row(term_2, procs, col_2, col_3, col_4, row_p, 2)
elif procs[row_p]['user'] == users[0]['name']:
proc_row(term_2, procs, col_2, col_3, col_4, row_p, 6)
else:
proc_row(term_2, procs, col_2, col_3, col_4, row_p, 5)
row_p += 1
term_2.refresh()
# Right column of terminal screen (term_3)
curr = term_3.getyx()
col_2 = (curr[0], curr[1] + (floor(win_width/5)) - 5)
col_3 = (curr[0], curr[1] + (floor((win_width/5)*3)))
proc_head(term_3, col_2, col_3, 7, 8)
curr = term_3.getyx()
h3 = term_size[0] - 1
for a in range(curr[0], h3):
if row_p == num_procs:
break
curr = term_3.getyx()
col_2 = (curr[0], curr[1] + (floor(win_width/5)) - 6)
col_3 = (curr[0], curr[1] + (floor((win_width/5)*3)))
col_4 = (curr[0], curr[1] + (floor((win_width/5)*4)) + 3)
if procs[row_p]['user'] == 'root':
proc_row(term_3, procs, col_2, col_3, col_4, row_p, 2)
elif procs[row_p]['user'] == users[0]['name']:
proc_row(term_3, procs, col_2, col_3, col_4, row_p, 6)
else:
proc_row(term_3, procs, col_2, col_3, col_4, row_p, 5)
row_p += 1
term_3.refresh()
time.sleep(0.2)
except KeyboardInterrupt:
return sys.exit('Thank you! Goodbye!')
try:
wrapper(main)
except TypeError:
sys.exit('"Are you using python3?"')
except Exception:
sys.exit('"If you don\'t have root privileges, I can\'t help you!"')