-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnmap-xml-parser.py
67 lines (59 loc) · 2.5 KB
/
nmap-xml-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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import argparse, sys
import xml.etree.ElementTree as ET
def all_ports():
for host in root.iter('ports'):
ports = host.findall('port')
used_ports = []
for port in ports:
if port.get('portid') not in used_ports:
used_ports.append(port.get('portid'))
for port in used_ports:
print(port)
def host_ports():
for host in root.iter('host'):
address = host.find('address')
print(address.get('addr'), end=' : ')
for ports in host.iter('ports'):
port_list = ports.findall('port')
for port in port_list:
print(port.get('portid'), end=',')
print()
def alive_hosts():
for host in root.iter('host'):
status = host.find('status')
address = host.find('address')
if status.get('state') == 'up':
print(address.get('addr'))
def all_service_names():
used_services = []
for ports in root.iter('ports'):
for port in ports.iter('port'):
services = port.find('service')
if services.get('product') and services.get('version'):
if services.get('product') + ' ' + services.get('version') not in used_services:
used_services.append(services.get('product') + ' ' + services.get('version'))
elif services.get('product') and not services.get('version'):
if services.get('product') not in used_services:
used_services.append(services.get('product'))
for service in used_services:
print(service)
parser = argparse.ArgumentParser(description='Parse nmap XML output.')
parser.add_argument('filepath', type = str, help = 'path to XML file')
parser.add_argument('--all_ports', action = 'store_true', help = 'display list of open ports irrespective of host')
parser.add_argument('--alive_hosts', action = 'store_true', help = 'display alive hosts')
parser.add_argument('--host_ports', action = 'store_true', help = 'display hosts & respective open ports')
parser.add_argument('--all_service_names', action = 'store_true', help = 'display list of service names + versions (if available) irrespective of host')
args = parser.parse_args()
tree = ET.parse(args.filepath)
root = tree.getroot()
if len(sys.argv) > 3:
print('Error: too many arguments.')
exit(-1)
elif args.all_ports == True:
all_ports()
elif args.alive_hosts == True:
alive_hosts()
elif args.host_ports == True:
host_ports()
elif args.all_service_names == True:
all_service_names()