-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
myipnetstat.py
executable file
·72 lines (49 loc) · 1.67 KB
/
myipnetstat.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
#! /usr/bin/python3
import socket
import sys
class myipud:
def __init__(self, location):
self.buffer = ''
self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
self.s.connect(location)
except FileNotFoundError as e:
print(f'Socket cannot be opened ({e}), is MyIP running?')
sys.exit(0)
def request(self, req):
req_b = (req + '\n').encode('ascii')
self.s.send(req_b)
while True:
lf = self.buffer.find('\n')
if lf != -1:
rc = self.buffer[0:lf]
self.buffer = self.buffer[lf + 1:]
return rc
data = self.s.recv(4096)
if len(data) == 0:
rc = self.buffer
self.buffer = ''
return rc
self.buffer += data.decode('ascii')
def help():
print(' * sessions')
print(' * list-devices list the physical devices, to be used with start/stop-pcap')
print(' * start-pcap x start pcap recording on device x')
print(' * stop-pcap x stop pcap recording on device x')
print(' * list-arp x list MAC addresses on device x')
if len(sys.argv) == 1:
help()
sys.exit(1)
mi = myipud('/tmp/myipstats.sock')
if sys.argv[1] == 'sessions':
print(mi.request('sessions'))
elif sys.argv[1] == 'list-devices':
print(mi.request('list-devices'))
elif sys.argv[1] == 'start-pcap':
print(mi.request('start-pcap|%s' % sys.argv[2]))
elif sys.argv[1] == 'stop-pcap':
print(mi.request('stop-pcap|%s' % sys.argv[2]))
elif sys.argv[1] == 'list-arp':
print(mi.request('list-arp|%s' % sys.argv[2]))
else:
help();