Created
April 3, 2015 12:45
-
-
Save veloutin/993d3e5acd70556d0cc4 to your computer and use it in GitHub Desktop.
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
#!/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