Nmap Open Port Stats Generator

This script will take a .nmap file from an nmap scan and give you a csv file containing a count of how many times each different port number was found open. For example:

  • port, count
  • 22, 100
  • 53, 6
  • 80, 88

It only looks at TCP ports but to change to UDP is fairly easy, to get both just add a second hits hash and put on in one and one in the new one.

#!/usr/bin/env ruby
 
if !File.exist? ARGV[0]
        puts "Input file not found"
        puts
        exit
end
 
hits = {}
File.open(ARGV[0], 'r').each { |line|
        line.chomp!
 
        if /^([0-9]*)\/tcp\s*open/.match line
                port = $1.to_i
                if hits.has_key?(port)
                        hits[port] += 1
                else
                        hits[port] = 1
                end
        end
}
 
puts "port, count"
hits.each_pair { |port, count|
        puts port.to_s + "," + count.to_s
}

By Robin Wood