Skip to content

Commit 9eff520

Browse files
committed
Update nmap_xml_parser.py
1 parent e93d4d9 commit 9eff520

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

Parser/nmap_xml_parser.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88
def parse_nmap_xml_file(input_file):
99
result_dict={} #To store the port_number:service_name
1010
host_status="N/A"
11-
if os.path.isfile(input_file): #Check if the file exist
12-
with open(input_file) as fd:
13-
col_ordered_dict = xmltodict.parse(fd.read()) #Convert the xml file to dictonary to process it
14-
host_status=col_ordered_dict['nmaprun']['runstats']['hosts']['@up'] #Get the Host status Valye 1 or 0
15-
if host_status=="1": #if Host status is up then process further
16-
for items in col_ordered_dict['nmaprun']['host']['ports']['port']: #Loop Through all the open ports and get the list
17-
if items['state']['@state']=="open":
18-
result_dict[items['@portid']] = items['service']['@name'] #Save Port Number and Service Name into Dict
11+
try:
12+
if os.path.isfile(input_file): #Check if the file exist
13+
with open(input_file) as fd:
14+
col_ordered_dict = xmltodict.parse(fd.read()) #Convert the xml file to dictonary to process it
15+
host_status=col_ordered_dict['nmaprun']['runstats']['hosts']['@up'] #Get the Host status Valye 1 or 0
16+
if host_status=="1": #if Host status is up then process further
17+
for items in col_ordered_dict['nmaprun']['host']['ports']['port']: #Loop Through all the open ports and get the list
18+
if items['state']['@state']=="open":
19+
result_dict[items['@portid']] = items['service']['@name'] #Save Port Number and Service Name into Dict
20+
except Exception as e:
21+
#print(e)
22+
host_status="ERROR"
1923
return host_status,result_dict
2024

2125

0 commit comments

Comments
 (0)