Ping检查服务器网络
2016-03-08 16:36
756 查看
#!/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()
相关文章推荐
- android wifi 无线调试
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- 5 个用于在 Linux 终端中查找域名 IP 地址的命令
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例