黑松山资源网 Design By www.paidiu.com
本文实例讲述了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程序设计有所帮助。
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年10月09日
2024年10月09日
- 李家明.1992-不老的情人【新艺宝】【WAV+CUE】
- 以莉·高露.2015-美好時刻【风潮】【FLAC分轨】
- 谢金晶.2017-放心【豪记】【WAV+CUE】
- 炉石传说最强死亡骑士卡组是什么 死亡骑士最强天梯卡组推荐一览
- 炉石传说最强德鲁伊卡组是哪个 德鲁伊最强天梯卡组推荐一览
- 炉石传说最强战士卡组是哪个 战士最强天梯卡组推荐一览
- LexVandyke《HistoriadeunAmor情陷地中海》[WAV+CUE]
- TheBestPopClassics《LIGHTRHYTHMSOUND2》[WAV+CUE]
- TheLatinSoundofLexVandyke-ConciertodeAranjuez拉丁情人2012[SACD-ISO]
- 《怪物猎人世界》史低¥44,《耻辱终极版》仅需¥12
- 杉果秋促最后一天!海量3A大作击穿Steam史低
- 【果娘聊天室】国庆假期就这么水灵灵地结束啦!
- 《死或生》恋爱游戏新角色:来自大雷温柔公主的喂食
- 《寂静岭2》原制作人盛赞《寂静岭2:重制版》:新玩家能玩 我超高兴
- 艾什莉体模家今日份的COS:《鸣潮》守岸人 雪白香肩清纯而诱惑