打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python 批量ping一个网段 返回结果 的程序
http://blog.csdn.net/nomefat/article/details/48788413
2015

  1. import subprocess  
  2. import threading  
  3. import time  
  4. import re  
  5.   
  6. ip_num = 256  
  7. list_ping_result = []  
  8.   
  9. class PingThread(threading.Thread):  
  10.       
  11.     def __init__(self,str_ip,sleep_time,g_list_p_r):  
  12.         threading.Thread.__init__(self)  
  13.         self.sleep_time = sleep_time  
  14.         self.str_ip = str_ip  
  15.         self.list_p_r = g_list_p_r  
  16.     def run(self):  
  17.         time.sleep(self.sleep_time)  
  18.         ftp_sub = subprocess.Popen("ping %s -n 3" % self.str_ip,  
  19.                                   stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)  
  20.         ret = ftp_sub.stdout.read()  
  21.         str_ret = ret.decode("gbk")  
  22.         ret_s = re.search("TTL",str_ret)  
  23.         if ret_s:  
  24.             self.list_p_r.append(('ping    ok',self.str_ip))  
  25.         else:  
  26.             self.list_p_r.append(('ping error',self.str_ip))  
  27.               
  28. def cmp_s(toupe_str):  
  29.     str_val = toupe_str[1]  
  30.     ret_group = re.match("\d*",str_val[::-1])  
  31.     str_ret = ret_group.group()  
  32.     return int(str_ret[::-1])  
  33.   
  34. thread_id =[]  
  35. for i in range(ip_num):  
  36.     thread_id.append(0)  
  37.     thread_id[i] = PingThread("192.168.8.%d" % i, int(i/20),list_ping_result)  
  38.     thread_id[i].start()  
  39.     print(i,end='')  
  40.   
  41. while True:  
  42.     if len(list_ping_result) >= ip_num:  
  43.         list_ping_result.sort(key=cmp_s)  
  44.         for i in list_ping_result:  
  45.             print(i)  
  46.   
  47.         break  

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python多线程批量Ping主机IP的脚本
python实现ping
[蟒蛇菜谱] Python封装shell命令
Python入门教程 超详细1小时学会Python
Python获取CMD命令行输出结果
python 实践1 python连接库
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服