-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectivity_matrix2.py
More file actions
executable file
·89 lines (75 loc) · 2.09 KB
/
connectivity_matrix2.py
File metadata and controls
executable file
·89 lines (75 loc) · 2.09 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
#!/usr/bin/python
import pwd
import os
import re
import glob
import time
PROC_TCP = "/proc/net/tcp"
STATE = {
'01':'ESTABLISHED',
'02':'SYN_SENT',
'03':'SYN_RECV',
'04':'FIN_WAIT1',
'05':'FIN_WAIT2',
'06':'TIME_WAIT',
'07':'CLOSE',
'08':'CLOSE_WAIT',
'09':'LAST_ACK',
'0A':'LISTEN',
'0B':'CLOSING'
}
def _hex2dec(s):
return str(int(s,16))
def _load():
''' Read the table of tcp connections & remove header '''
with open(PROC_TCP,'r') as f:
content = f.readlines()
content.pop(0)
return content
def _convert_ip_port(array):
host,port = array.split(':')
return _ip(host),_hex2dec(port)
def _remove_empty(array):
return [x for x in array if x !='']
def _ip(s):
ip = [(_hex2dec(s[6:8])),(_hex2dec(s[4:6])),(_hex2dec(s[2:4])),(_hex2dec(s[0:2]))]
return '.'.join(ip)
def _net(host):
l = re.split(r'(\.|/)', host)
l = l[0:5]
p = ""
for i in l:
p += str(i)
return p
def netstat(result):
PERIOD_OF_TIME = 10
start = time.time()
remote_hp = [] # Remote hp - host and port
host = []
while True:
content=_load()
for line in content:
line_array = _remove_empty(line.split(' '))
l_host,l_port = _convert_ip_port(line_array[1]) # Convert ipaddress and port from hex to decimal.
r_host,r_port = _convert_ip_port(line_array[2])
if l_host == '0.0.0.0' or l_host == '127.0.0.1' or r_host == '0.0.0.0':
continue
else:
host = [l_host+ ' --- ' + _net(r_host)]
if host not in remote_hp:
remote_hp.append(host)
nline = [l_host+':'+l_port + ' --- ' +r_host+':'+r_port]
# print(remote_hp)
host = []
else:
continue
# time.sleep(2)
result.append(nline)
time.sleep(1)
if time.time() > start + PERIOD_OF_TIME : break
for i in range(len(result)):
print(result[i]) # Print result
return result
if __name__ == '__main__':
result = []
netstat(result)