-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnmap_xml_parser.py
33 lines (27 loc) · 1.24 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
#Read ME - NMAP Output file is converted to dictonary and processed
#Input - NMAP Output XML File
#Output - Dictonary (Contains the opened port number and service name)
#pip install xmltodict
import xmltodict
import os
#Function to parse the xml file
def parse_nmap_xml_file(input_file):
result_dict={} #To store the port_number:service_name
host_status="N/A"
try:
if os.path.isfile(input_file): #Check if the file exist
with open(input_file) as fd:
col_ordered_dict = xmltodict.parse(fd.read()) #Convert the xml file to dictonary to process it
host_status=col_ordered_dict['nmaprun']['runstats']['hosts']['@up'] #Get the Host status Valye 1 or 0
if host_status=="1": #if Host status is up then process further
for items in col_ordered_dict['nmaprun']['host']['ports']['port']: #Loop Through all the open ports and get the list
if items['state']['@state']=="open":
result_dict[items['@portid']] = items['service']['@name'] #Save Port Number and Service Name into Dict
except Exception as e:
#print(e)
host_status="ERROR"
return host_status,result_dict
input_file='output.xml'
host_status,result_dict=parse_nmap_xml_file(input_file) #Call the function with the filename as parameter
print(host_status)
print(result_dict)