- #!/usr/bin/python
- #coding:utf-8
- import multiprocessing
- import re
- import sys,os
- import commands
- import datetime
- def pinger(ip):
- cmd='ping -c 2 %s' % (ip.strip()) #Return a copy of the string S with leading and trailing whitespace removed.
- ret = commands.getoutput(cmd) #Return output (stdout or stderr) of executing cmd in a shell.
- loss_re=re.compile(r"received, (.*) packet loss") #Compile a regular expression pattern, returning a pattern object.
- packet_loss=loss_re.findall(ret)[0] #Return a list of all non-overlapping matches in the string
- rtt_re=re.compile(r"rtt min/avg/max/mdev = (.*) ")
- rtts=rtt_re.findall(ret)
- #rtt.split(["/"])
- rtt=rtts[0].split('/') #Return a list of the words in the string
- 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)
- if __name__ == "__main__":
- if not os.path.exists("hosts.txt") :
- print "\\033[31mhosts.txt文件不存在,请重试\\033[0m" #"\\033[31m字符串\\033[0m"设置颜色字体
- sys.exit(1)
- now=datetime.datetime.now()
- file=open('hosts.txt','r')
- pool=multiprocessing.Pool(processes=4) #Returns a process pool object
- 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(),))) #A variant of the apply() method which returns a result object.
- pool.close()
- pool.join()
- #该片段来自于http://www.codesnippet.cn/detail/2308201614967.html
来源: http://www.codesnippet.cn/detail/2308201614967.html