forked from rocfy/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_ping.py
36 lines (34 loc) · 979 Bytes
/
check_ping.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
#!/usr/bin/python
import multiprocessing
import re
import sys
import commands
import datetime
def pinger(ip):
cmd='ping -c 2 %s' % (ip.strip())
ret = commands.getoutput(cmd)
loss_re=re.compile(r"received, (.*) packet loss")
packet_loss=loss_re.findall(ret)[0]
rtt_re=re.compile(r"rtt min/avg/max/mdev = (.*) ")
rtts=rtt_re.findall(ret)
#rtt.split(["/"])
rtt=rtts[0].split('/')
rtt_min=rtt[0]
rtt_avg=rtt[1]
rtt_max=rtt[2]
print "%s\t\t%s\t\t%s\t\t%s\t\t%s"%(ip,packet_loss,rtt_min,rtt_max,rtt_avg)
def test(ip):
print ip
if __name__ == "__main__":
now=datetime.datetime.now()
file=open('hosts.txt','r')
pool=multiprocessing.Pool(processes=4)
result=[]
print "########%s###########"%now
print "IPADDRSS\t\t\tLOSS\t\tMIN\t\tMAX\t\tAVG"
for i in file.readlines():
if len(i)==1 or i.startswith("#"):
continue
result.append(pool.apply_async(pinger,(i.strip(),)))
pool.close()
pool.join()