-
Notifications
You must be signed in to change notification settings - Fork 10
/
nmap-parser.py
52 lines (40 loc) · 1.49 KB
/
nmap-parser.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
#!/usr/bin/env python
import argparse
from libnmap.process import NmapProcess
from libnmap.parser import NmapParser, NmapParserException
def parse_args():
''' Create the arguments '''
parser = argparse.ArgumentParser()
parser.add_argument("-x", "--nmapxml", help="Nmap XML file to parse")
parser.add_argument("-l", "--hostlist", help="Host list file")
return parser.parse_args()
def report_parser(report):
''' Parse the Nmap XML report '''
for host in report.hosts:
ip = host.address
if host.is_up():
hostname = 'N/A'
# Get the first hostname (sometimes there can be multi)
if len(host.hostnames) != 0:
hostname = host.hostnames[0]
print '[*] {0} - {1}'.format(ip, hostname)
# Get the port and service
# objects in host.services are NmapService objects
for s in host.services:
# Check if port is open
if s.open():
serv = s.service
port = s.port
ban = s.banner
# Perform some action on the data
print_data(ip, port, serv, ban)
def print_data(ip, port, serv, ban):
''' Do something with the nmap data '''
if ban != '':
ban = ' -- {0}'.format(ban)
print ' {0}: {1}{2}'.format(port, serv, ban)
def main():
args = parse_args()
report = NmapParser.parse_fromfile(args.nmapxml)
report_parser(report)
main()