Skip to content

Instantly share code, notes, and snippets.

@tatiC
Created July 10, 2012 04:22
Show Gist options
  • Save tatiC/3080993 to your computer and use it in GitHub Desktop.
Save tatiC/3080993 to your computer and use it in GitHub Desktop.
Free ports
# aliases
# alias scripts="cd /Users/tatic/Dropbox/my_projects/myscripts; ls -la"
# alias freeports="scripts;ports >> ports.txt;ruby freeports.rb;rm ports.txt"
# by: TatiCarvalho 2012
# In the command-line: freeports
# v1.1
# example:
# all_ports = ["3000", "3001"]
# busy_ports = ["3000", "3010"]
# free_port: ["3000", "3001"] - ["3000", "3010"]
busy_ports = []
f = File.open("ports.txt")
f.each do |line|
busy_ports << /TCP *....../.match(line).to_s.gsub("TCP *:", "")
end
all_ports = ["3000", "3001", "3002", "3003", "3004", "3005", "3006", "3007", "3008", "3009", "3010"]
free_port = (all_ports - busy_ports).first
puts "Free port: #{free_port}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment