-
Notifications
You must be signed in to change notification settings - Fork 0
/
port_diff.py
executable file
·102 lines (88 loc) · 2.61 KB
/
port_diff.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
#!/usr/bin/env python2.7
# ~*~ coding: utf8 ~*~
# Port diff for linux
# Author: ry
# version: 2.0.1
#
#
import sys
import shelve
import os.path
import subprocess
# Config
class Config:
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
TEMP_DIR = os.path.join(BASE_DIR, 'cache')
DB_FILE = os.path.join(TEMP_DIR, 'listen.db')
DB_INDEX = 'ftMMJEuuHJZcd'
BIN_SS = os.path.join(BASE_DIR, 'scan_listen_port')
BIN_EGREP = os.path.join(BASE_DIR, 'find_string')
# Get command
try:
command = sys.argv[1]
except IndexError:
sys.stderr.write('Usage: %s port_diff|tcp_syn_check|tcp_estab_check\n' % __file__)
exit(1)
# Get link all
shell = """ \
%s -atun | \
%s '^udp|^tcp' """ % \
(Config.BIN_SS, Config.BIN_EGREP)
result = subprocess.check_output(shell, shell=True)
def link(method):
link_choall = []
if result:
for x in result.strip().split('\n'):
row = x.split()
protocal = row[0]
state = row[1]
local_port = row[4].split(':')[-1]
if isinstance(method, (list, tuple)):
for _m in method:
if _m == state:
link_choall.append((local_port, protocal))
else:
if method == state:
link_choall.append((local_port, protocal))
else:
return []
return link_choall
if command == 'port_diff':
# tcp state: LISTEN, udp state: UNCONN
listen_port = link(('LISTEN', 'UNCONN'))
if listen_port:
listen_port = set(listen_port)
else:
sys.stdout.write('Not find tcp or udp listen port.')
exit(1)
try:
db = shelve.open(Config.DB_FILE)
except:
sys.stdout.write('Listen DB error[%s].' % Config.DB_FILE)
exit(1)
old_list = db.get(Config.DB_INDEX)
if old_list:
result = ''
miss = list(old_list - listen_port)
add = list(listen_port - old_list)
if miss:
result += 'Port to reduce: %s. ' % (miss.__str__())
if add:
result += 'The port to increase: %s' % (add.__str__())
if result:
sys.stdout.write(result)
else:
sys.stdout.write('OK')
else:
sys.stdout.write('OK')
# Save this list
db[Config.DB_INDEX] = listen_port
# Close db
db.close()
elif command == 'tcp_syn_check':
sys.stdout.write(link('SYN-RECV').__len__().__str__())
elif command == 'tcp_estab_check':
sys.stdout.write(link('ESTAB').__len__().__str__())
else:
sys.stderr.write('Usage: %s port_diff|tcp_syn_check|tcp_estab_check\n' % __file__)
exit(1)