Python實現(xiàn)快速多線程ping的方法
本文實例講述了Python實現(xiàn)快速多線程ping的方法。分享給大家供大家參考。具體如下:
#!/usr/bin/python #_*_coding:utf-8_*_ # ''' 名稱:快速多線程ping程序 開發(fā):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)) #多線程同時執(zhí)行 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)
執(zhí)行結(jié)果:
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程序設(shè)計有所幫助。
相關(guān)文章
python selenium 彈出框處理的實現(xiàn)
這篇文章主要介紹了python selenium 彈出框處理的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02