Python实现快速多线程ping的方法
2015-07-15 18:12
956 查看
本文实例讲述了Python实现快速多线程ping的方法。分享给大家供大家参考。具体如下:
#!/usr/bin/python #_*_coding:utf-8_*_ # ''' 名称:快速多线程ping程序 开发:gyhong gyh9711 日期:20:51 2011-04-25 ''' import pexpect import datetime from threading import Thread host=["192.168.1.1","192.168.1.123","192.168.2.1", "192.168.1.1","192.168.1.123","192.168.2.1", "192.168.1.1","192.168.1.123","192.168.2.1", "192.168.1.1","192.168.1.123","192.168.2.1", "192.168.1.1"] report_ok=[] report_error=[] class PING(Thread): def __init__(self,ip): Thread.__init__(self) self.ip=ip def run(self): Curtime = datetime.datetime.now() #Scrtime = Curtime + datetime.timedelta(0,minute,0) #print("[%s]主机[%s]" % (Curtime,self.ip)) ping=pexpect.spawn("ping -c1 %s" % (self.ip)) check=ping.expect([pexpect.TIMEOUT,"1 packets transmitted, 1 received, 0% packet loss"],2) if check == 0: print("[%s] 超时 %s" % (Curtime,self.ip)) elif check == 1: print ("[%s] %s 可达" % (Curtime,self.ip)) else: print("[%s] 主机%s 不可达" % (Curtime,self.ip)) #多线程同时执行 T_thread=[] for i in host: t=PING(i) T_thread.append(t) for i in range(len(T_thread)): T_thread[i].start() # #print ("\n=========问题主机情况如下==========\n") #output(report_error) #print ("\n=========正常主机情况如下==========\n") #output(report_ok)
执行结果:
administrator@nagios:/win/pexpect$ ./ping.py
[2011-04-25 21:30:22.126981] 192.168.1.1 可达
[2011-04-25 21:30:22.148376] 192.168.1.1 可达
[2011-04-25 21:30:22.179846] 192.168.1.1 可达
[2011-04-25 21:30:22.203691] 192.168.1.1 可达
[2011-04-25 21:30:22.227696] 192.168.2.1 可达
[2011-04-25 21:30:22.134049] 超时 192.168.1.123
[2011-04-25 21:30:22.145610] 超时 192.168.2.1
[2011-04-25 21:30:22.157558] 超时 192.168.1.123
[2011-04-25 21:30:22.167898] 超时 192.168.2.1
[2011-04-25 21:30:22.197572] 超时 192.168.1.123
[2011-04-25 21:30:22.202430] 超时 192.168.2.1
[2011-04-25 21:30:22.215561] 超时 192.168.1.123
[2011-04-25 21:30:22.229952] 超时 192.168.1.1
希望本文所述对大家的Python程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- 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在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例