Skip to content

Instantly share code, notes, and snippets.

@veloutin
Created April 3, 2015 12:45
Show Gist options
  • Save veloutin/993d3e5acd70556d0cc4 to your computer and use it in GitHub Desktop.
Save veloutin/993d3e5acd70556d0cc4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
ports = set()
for s in ("tcp","udp","tcp6","udp6"):
with open("/proc/net/%s" % s) as f:
for l in f.readlines()[1:]:
ports.add(int(l.split(":")[2][:4],16))
ports = sorted(ports)
from_port = int((sys.argv[1:2]+[1,])[0])
max_port = int((sys.argv[2:3]+[2**16-1,])[0])
free_port = from_port
while free_port in ports and from_port <= max_port:
free_port += 1
if free_port < max_port:
print free_port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment