Skip to content

Instantly share code, notes, and snippets.

@amsz
Created January 7, 2016 08:59
Show Gist options
  • Save amsz/495cba046cd896b69091 to your computer and use it in GitHub Desktop.
Save amsz/495cba046cd896b69091 to your computer and use it in GitHub Desktop.
import os
times = 2
show_top = 10
flag = 'time='
domain = '.vpnhide.com'
protocols = ['p1', 'p2']
nodes = { # node: number
'jp': 4,
'us': 6,
'sg': 2,
'tw': 2,
'hk': 3,
'uk': 1
}
result = [] # server: [time1, time2...]
def ping(node):
print(node)
lines = os.popen('ping -c ' + str(times) + ' ' + node).readlines()
data = []
for line in lines:
idx1 = line.find(flag)
if idx1 != -1:
idx2 = line.find(' ', idx1)
idx1 += len(flag)
data.append(float(line[idx1:idx2]))
return data
for(k, v) in nodes.items():
for i in range(1, v + 1):
for p in protocols:
node = p + '.' + str(k) + str(i) + domain
data = ping(node)
result.append((node, sum(data) / len(data)))
sorted_result = sorted(result, key=lambda t: t[1])
sorted_result_len = len(sorted_result)
top = sorted_result_len if show_top > sorted_result_len else show_top
print('Top ' + str(top))
print('====================')
for i in range(top):
print(str(i + 1) + '. ' + sorted_result[i][0] + ' -> ' + str(sorted_result[i][1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment