-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnetmgr.py
More file actions
161 lines (145 loc) Β· 5.27 KB
/
netmgr.py
File metadata and controls
161 lines (145 loc) Β· 5.27 KB
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
#!/usr/bin/env python3
"""
NullSec Network Manager v1.3
A curses-based network and process monitoring utility for NullSec Linux.
Provides real-time connection monitoring, process listing, and logging.
GitHub: https://github.com/bad-antics/nullsec
"""
import curses
import subprocess
import time
import os
from datetime import datetime
__version__ = "1.3"
__author__ = "bad-antics"
# Helpers
def run(cmd):
try:
return subprocess.check_output(cmd, shell=True, text=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
return e.output
def list_connections():
# ss is faster; fallback to netstat
out = run("ss -tunap || netstat -tunap")
return out.splitlines()
def list_processes():
out = run("ps -eo pid,comm,cmd --sort=comm")
return out.splitlines()
def save_log(lines, prefix):
ts = datetime.now().strftime('%Y%m%d-%H%M%S')
path = f"logs/{prefix}-{ts}.log"
os.makedirs('logs', exist_ok=True)
with open(path, 'w') as f:
f.write('\n'.join(lines) + '\n')
return path
# UI
MENU_ITEMS = [
"Connections",
"Processes",
"Dump connections log",
"Dump processes log",
"Quit",
]
def draw_menu(stdscr, selected):
h, w = stdscr.getmaxyx()
title = "Home Network Device Manager"
stdscr.clear()
stdscr.addstr(0, 2, title)
stdscr.addstr(1, 2, "Use β β to navigate, Enter to select. 'r' to refresh. 'q' to quit.")
for idx, item in enumerate(MENU_ITEMS):
x = 4
y = 3 + idx
if idx == selected:
stdscr.attron(curses.A_REVERSE)
stdscr.addstr(y, x, item)
if idx == selected:
stdscr.attroff(curses.A_REVERSE)
stdscr.refresh()
def draw_list(stdscr, header, lines, selected_idx):
h, w = stdscr.getmaxyx()
stdscr.clear()
stdscr.addstr(0, 2, header)
stdscr.addstr(1, 2, "Up/Down to select, 's' save log, 'b' back, 'q' quit")
max_items = h - 5
start = max(0, selected_idx - max_items // 2)
visible = lines[start:start+max_items]
for i, line in enumerate(visible):
y = 3 + i
if start + i == selected_idx:
stdscr.attron(curses.A_REVERSE)
stdscr.addstr(y, 2, line[:w-4])
if start + i == selected_idx:
stdscr.attroff(curses.A_REVERSE)
stdscr.refresh()
def curses_main(stdscr):
try:
curses.curs_set(0)
except curses.error:
pass
stdscr.nodelay(False)
selected = 0
connections = list_connections()
processes = list_processes()
while True:
draw_menu(stdscr, selected)
ch = stdscr.getch()
if ch in (curses.KEY_UP, ord('k')):
selected = (selected - 1) % len(MENU_ITEMS)
elif ch in (curses.KEY_DOWN, ord('j')):
selected = (selected + 1) % len(MENU_ITEMS)
elif ch == ord('q'):
break
elif ch == ord('r'):
connections = list_connections()
processes = list_processes()
elif ch in (curses.KEY_ENTER, 10, 13):
if MENU_ITEMS[selected] == "Connections":
idx = 0
while True:
draw_list(stdscr, "Connections", connections, idx)
ch2 = stdscr.getch()
if ch2 in (curses.KEY_UP, ord('k')):
idx = max(0, idx - 1)
elif ch2 in (curses.KEY_DOWN, ord('j')):
idx = min(len(connections)-1, idx + 1)
elif ch2 == ord('s'):
path = save_log(connections, 'connections')
stdscr.addstr(2, 2, f"Saved: {path} ")
stdscr.refresh()
time.sleep(0.8)
elif ch2 == ord('b'):
break
elif ch2 == ord('q'):
return
elif ch2 == ord('r'):
connections = list_connections()
idx = min(idx, len(connections)-1)
elif MENU_ITEMS[selected] == "Processes":
idx = 0
while True:
draw_list(stdscr, "Processes", processes, idx)
ch2 = stdscr.getch()
if ch2 in (curses.KEY_UP, ord('k')):
idx = max(0, idx - 1)
elif ch2 in (curses.KEY_DOWN, ord('j')):
idx = min(len(processes)-1, idx + 1)
elif ch2 == ord('s'):
path = save_log(processes, 'processes')
stdscr.addstr(2, 2, f"Saved: {path} ")
stdscr.refresh()
time.sleep(0.8)
elif ch2 == ord('b'):
break
elif ch2 == ord('q'):
return
elif ch2 == ord('r'):
processes = list_processes()
idx = min(idx, len(processes)-1)
elif MENU_ITEMS[selected] == "Dump connections log":
save_log(connections, 'connections')
elif MENU_ITEMS[selected] == "Dump processes log":
save_log(processes, 'processes')
elif MENU_ITEMS[selected] == "Quit":
break
if __name__ == '__main__':
curses.wrapper(curses_main)