python 多线程ping测试_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程序设计有所帮助。

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值