国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現快速多線程ping的方法

2020-01-04 18:03:58
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了Python實現快速多線程ping的方法,實例分析了Python多線程及ICMP數據包的發送技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了Python實現快速多線程ping的方法。分享給大家供大家參考。具體如下:

 

 
  1. #!/usr/bin/python 
  2. #_*_coding:utf-8_*_ 
  3. # 
  4. ''
  5. 名稱:快速多線程ping程序 
  6. 開發:gyhong gyh9711 
  7. 日期:20:51 2011-04-25 
  8. ''
  9. import pexpect 
  10. import datetime 
  11. from threading import Thread 
  12. host=["192.168.1.1","192.168.1.123","192.168.2.1"
  13. "192.168.1.1","192.168.1.123","192.168.2.1"
  14. "192.168.1.1","192.168.1.123","192.168.2.1"
  15. "192.168.1.1","192.168.1.123","192.168.2.1"
  16. "192.168.1.1"
  17. report_ok=[] 
  18. report_error=[] 
  19. class PING(Thread): 
  20. def __init__(self,ip): 
  21. Thread.__init__(self) 
  22. self.ip=ip 
  23. def run(self): 
  24. Curtime = datetime.datetime.now()  
  25. #Scrtime = Curtime + datetime.timedelta(0,minute,0)  
  26. #print("[%s]主機[%s]" % (Curtime,self.ip)) 
  27. ping=pexpect.spawn("ping -c1 %s" % (self.ip)) 
  28. check=ping.expect([pexpect.TIMEOUT,"1 packets transmitted, 1 received, 0% packet loss"],2) 
  29. if check == 0: 
  30. print("[%s] 超時 %s" % (Curtime,self.ip)) 
  31. elif check == 1: 
  32. print ("[%s] %s 可達" % (Curtime,self.ip)) 
  33. else
  34. print("[%s] 主機%s 不可達" % (Curtime,self.ip)) 
  35. #多線程同時執行 
  36. T_thread=[] 
  37. for i in host: 
  38. t=PING(i) 
  39. T_thread.append(t) 
  40. for i in range(len(T_thread)): 
  41. T_thread[i].start() 
  42. # 
  43. #print ("/n=========問題主機情況如下==========/n") 
  44. #output(report_error) 
  45. #print ("/n=========正常主機情況如下==========/n") 
  46. #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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吕梁市| 夹江县| 敦化市| 蓬安县| 汝城县| 土默特左旗| 潼南县| 西昌市| 滨海县| 白朗县| 通榆县| 贡觉县| 正安县| 历史| 蛟河市| 临高县| 隆昌县| 周口市| 陆河县| 新巴尔虎右旗| 顺昌县| 莆田市| 清涧县| 松滋市| 兴隆县| 垫江县| 丽水市| 左贡县| 左云县| 凭祥市| 汽车| 万宁市| 黄大仙区| 万州区| 大城县| 温州市| 安福县| 绩溪县| 宁海县| 临邑县| 辉南县|