forked from mozillazg/justping
-
Notifications
You must be signed in to change notification settings - Fork 2
/
justping.py
executable file
·84 lines (71 loc) · 2.28 KB
/
justping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
""" find the fastest host from file by ping
"""
import re
import os
import sys
import shell_ping
def get_hosts(filename):
""" get IP/hostname from file
return IP/hostname list, default is null
"""
hosts = []
with open(filename) as f:
for line in f:
line = line.strip().strip('.,/')
if line:
hosts.append(line)
return hosts
if __name__ == '__main__':
argvs = sys.argv
hosts = []
filename = 'hosts.txt'
append = False
result_time = {}
if len(argvs) >= 2:
target = argvs[1]
if os.path.isfile(target):
filename = target
if leng > 2:
if argvs[2] == '+':
append = True
target = argvs[3:]
else:
target = argvs[2:]
elif target == '+':
append = True
target = argvs[2:]
else:
target = argvs[1:]
if target:
for s in target:
name = s.strip('.,/')
name = re.sub(r'https?://', '', name)
hosts.append(name)
if not hosts:
# read from file and remove duplicates
hosts = list(set(get_hosts(filename)))
else:
# remove duplicates
hosts = list(set(hosts))
# append additional host from command line
if append:
hosts = list(set(get_hosts(filename) + hosts))
if not hosts:
sys.exit('No host to ping.')
print 'host(ip)'.rjust(34), ' min_time'.rjust(8), 'avg_time'.rjust(8), 'max_time'.rjust(8), 'lost'.rjust(8)
for x in hosts:
ip, time_min, time_avg, time_max, lost = shell_ping.ping(x, times=5)
result_time.update({x: [time_min, time_avg, time_max]})
print ('%s(%s): ' % (x, ip)).rjust(35),\
('% 3sms' % (time_min)).rjust(8),\
('% 3sms' % (time_avg)).rjust(8),\
('% 3sms' % (time_max)).rjust(8),\
('% 2s%%' % (lost)).rjust(8)
times = sorted(result_time.itervalues())
# remove no reaching results
times = [i[0] for i in times[:] if i[0] > 0]
if times:
for k, v in result_time.iteritems():
if v[0] == times[0]:
print '%s has min ping time: %s ms' % (k, v[0])