Skip to content

Instantly share code, notes, and snippets.

@zenfosec
Last active September 18, 2020 15:44
Show Gist options
  • Select an option

  • Save zenfosec/9e7285ec3bd6c842f7aa to your computer and use it in GitHub Desktop.

Select an option

Save zenfosec/9e7285ec3bd6c842f7aa to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#
# Counts the occurrences of each port found in a gnmap file
# Outputs a space delimited list of <port> <count>
#
# 9/20/11 - zenfosec
# Updated 5/15/18
import sys
import re
if len(sys.argv) != 2:
print "Usage: portcount.py <gnmap_file>"
sys.exit()
infile = open(sys.argv[1], "r")
port_dict = {}
#search for ports, add port and count to dict
for line in infile:
splitline=line.split('/')
host=splitline[0]
ports = re.findall(" \d*/open", line)
strippedports= []
for port in ports:
strippedports.append(port[1:-5])
for port in strippedports:
try:
if port_dict[port]:
port_dict[port] = port_dict[port] + 1
except:
port_dict[port] = 1
portlist = port_dict.keys()
portlist = map(int, portlist)
portlist.sort()
for port in portlist:
key = str(port)
print key, str(port_dict[key])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment