1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
-
+
-
-
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
-
-
+
-
-
+
+
+
|
#!/usr/bin/python
import os,sys,re
import sys,socket,errno
f=os.popen("netstat -nlt4","r")
if len(sys.argv)>1:
start=int(sys.argv[1])
port=int(sys.argv[1])
else:
start=5900
s=set()
for line in f:
if not line.startswith("tcp"):
continue
port=5900
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
try:
s.bind(("",port))
except socket.error as e:
if e.errno== errno.EADDRINUSE:
port+=1
continue
m=re.search(":(\\d+)\\b",line)
if m:
s.add(int(m.group(1)))
i=start
while i in s:
else:
raise e
i += 1
print i
break
s.close()
print port
|