├── README.md └── nmap-parser.py /README.md: -------------------------------------------------------------------------------- 1 | # nmap-parser 2 | 3 | Parses Nmap XML files and prints the IP, hostname, port, service and service banner 4 | 5 | ```./nmap-parser.py -x NmapXMLFile.xml``` 6 | -------------------------------------------------------------------------------- /nmap-parser.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import argparse 4 | from libnmap.process import NmapProcess 5 | from libnmap.parser import NmapParser, NmapParserException 6 | 7 | def parse_args(): 8 | ''' Create the arguments ''' 9 | parser = argparse.ArgumentParser() 10 | parser.add_argument("-x", "--nmapxml", help="Nmap XML file to parse") 11 | parser.add_argument("-l", "--hostlist", help="Host list file") 12 | return parser.parse_args() 13 | 14 | def report_parser(report): 15 | ''' Parse the Nmap XML report ''' 16 | for host in report.hosts: 17 | ip = host.address 18 | 19 | if host.is_up(): 20 | hostname = 'N/A' 21 | # Get the first hostname (sometimes there can be multi) 22 | if len(host.hostnames) != 0: 23 | hostname = host.hostnames[0] 24 | 25 | print '[*] {0} - {1}'.format(ip, hostname) 26 | 27 | # Get the port and service 28 | # objects in host.services are NmapService objects 29 | for s in host.services: 30 | 31 | # Check if port is open 32 | if s.open(): 33 | serv = s.service 34 | port = s.port 35 | ban = s.banner 36 | 37 | # Perform some action on the data 38 | print_data(ip, port, serv, ban) 39 | 40 | def print_data(ip, port, serv, ban): 41 | ''' Do something with the nmap data ''' 42 | if ban != '': 43 | ban = ' -- {0}'.format(ban) 44 | 45 | print ' {0}: {1}{2}'.format(port, serv, ban) 46 | 47 | def main(): 48 | args = parse_args() 49 | report = NmapParser.parse_fromfile(args.nmapxml) 50 | report_parser(report) 51 | 52 | main() 53 | --------------------------------------------------------------------------------