-
Notifications
You must be signed in to change notification settings - Fork 19
/
beautify-portscan
executable file
·73 lines (59 loc) · 1.93 KB
/
beautify-portscan
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/env python
import sys
import re
import pdb
import scrap
def isreg(rx):
return isinstance(rx, type(re.compile('')))
def reg(pat, flags=0):
if isreg(pat):
return pat
return re.compile(pat, re.VERBOSE | flags)
pat = r'Host: [\s]+ (?P<host> [0-9.]+)'
pat += r'[\s]+ [(].*?[)]'
pat += r'[\s]+ (?P<key> [A-Za-z\s]+)[:]'
pat += r'[\s]+ (?P<val> .+?)'
pat += r'$'
rx = reg(pat, re.VERBOSE | re.MULTILINE | re.DOTALL)
output = []
def prval(key, val, seps=['/']):
#print key, val
for sep in seps:
val = val.replace(sep, '\0')
global output
output += ['%s\0%s' % (key, val)]
def main():
indata=sys.stdin.read()
for line in indata.splitlines():
# print ''
# print line
# print ''
m = rx.search(line)
if not m:
#print line
pass
else:
host = m.expand(r'\g<host>')
key = m.expand(r'\g<key>')
val = m.expand(r'\g<val>')
if key == 'Status' and val == 'Up':
continue
if key != 'Ports':
print host, key, val
else:
ports = [x.strip() for x in val.split(',')]
ports = [x for x in ports if len(x) > 0]
ignored_state = ''
if len(ports) > 0:
if ports[-1].find('Ignored State:') >= 0:
m = reg(r'(.*?) [\s]+ Ignored[ ]State: [\s]+ (.*)').search(ports[-1])
assert(m)
ports[-1] = m.expand(r'\1')
ignored_state = m.expand(r'\2')
ignored_state = ignored_state.strip()
for port in ports:
prval(host, port, seps=['/'])
if len(ignored_state) > 0:
prval(host, ignored_state, seps=[' '])
print scrap.shell(r"cols -d '\0'", stdin='\n'.join(output))
main()