玩蛇网提供最新Python编程技术信息以及Python资源下载!

Python pexpect包应用之快速多线程ping测试

python 培训
#!/usr/bin/python
#_*_coding:utf-8_*_


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))

#www.iplaypython.com
#多线程同时执行
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

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/scripts-shell/ss2476.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-03-31 08:34 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部