forked from leonteale/pentestpackage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNmapParser.py
More file actions
86 lines (72 loc) · 2.02 KB
/
NmapParser.py
File metadata and controls
86 lines (72 loc) · 2.02 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
#!/usr/bin/env python
import csv
import sys
# Name of file
finput = sys.argv[1]
fout_suffix = "_nmap.csv"
cols = ['PORT', 'STATE', 'SERVICE', 'VERSION']
def getIP(line):
if not line: return None
if 'Nmap scan report for' in line:
return line[21:-1]
return ''
def findHead(line):
if not line: return False
for i in cols:
if i not in line:
return False
return True
def parseData(line):
data = []
for i in line.split():
if i.isspace(): continue
if len(data) == 0:
for j in i.strip().split('/'):
data.append(j)
else:
data.append(i.strip())
# Get the version type
i = 4
version = ''
while i < len(data):
version += ' ' + data[i]
data.remove(data[i])
if version:
data.append(version.strip())
return data
def checkLine(line):
for i in line:
if i.isdigit(): continue
if i == '/': return True
return False
# Open the file
with open(finput, 'r') as fin:
while True:
# Get IP address
while True:
ip = getIP(fin.readline())
if ip != '': break
if ip == None:
break
# Get first column from port scan
while not findHead(fin.readline()):
pass
# Get all data
data = []
while True:
line = fin.readline()
# All lines stating with '|' will be ignored
if line[0] == '|': continue
# Check if line is invalid
if not checkLine(line): break
# Record the line
data.append(parseData(line))
# Output into file
with open(ip + fout_suffix, 'wb') as csvfile:
# Get file to be written
rows = csv.writer(csvfile)
cols.insert(1, 'PROTOCOL')
rows.writerow(cols)
cols.remove('PROTOCOL')
# Print all the rows
rows.writerows(data)