• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

快速多线程ping

python 水墨上仙 3120次浏览

快速多线程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


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明快速多线程ping
喜欢 (0)
加载中……