Created
July 28, 2016 06:50
-
-
Save s4w3d0ff/47796c129ebf677a677527c450da8fee to your computer and use it in GitHub Desktop.
Downloads the current iana.org port assignment csv and returns a list of 'Unassigned' ports in the 'User' range (>1024)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import requests | |
import logging | |
def getAvailUserPorts(url='http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.csv', minport=1024): | |
""" Downloads the current iana.org port assignment csv and returns a list of 'Unassigned' ports in the 'User' range (>1024)""" | |
logging.info("Downloading: '%s'" % url) | |
logging.info("This could take awhile...") | |
raw = requests.get(url) | |
reader = csv.reader(raw.iter_lines(), delimiter=',') | |
portList = [] | |
logging.info("Generating portList...") | |
for row in reader: | |
if 'Unassigned' in row: | |
if '-' in row[1]: # if port range | |
if int(row[1].split('-')[0]) > minport: # if range is in 'user' port range | |
for i in range(int(row[1].split('-')[0]), int(row[1].split('-')[1])): | |
portList.append(i) | |
elif int(row[1]) > minport: # if range is in private port range | |
portList.append(row[1]) | |
logging.info("Found %s 'Unassigned' ports > %s" % ( str(len(portList)), str(minport)) ) | |
return portList | |
if __name__ == "__main__": | |
logging.basicConfig(format='[%(asctime)s] %(message)s', datefmt="%Y-%m-%d %H:%M:%S", level=logging.DEBUG) | |
logging.info(getAvailUserPorts()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment