python 批量ping一个网段 返回结果 的程序
2015-09-28 19:28
633 查看
import subprocess import threading import time import re ip_num = 256 list_ping_result = [] class PingThread(threading.Thread): def __init__(self,str_ip,sleep_time,g_list_p_r): threading.Thread.__init__(self) self.sleep_time = sleep_time self.str_ip = str_ip self.list_p_r = g_list_p_r def run(self): time.sleep(self.sleep_time) ftp_sub = subprocess.Popen("ping %s -n 3" % self.str_ip, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True) ret = ftp_sub.stdout.read() str_ret = ret.decode("gbk") ret_s = re.search("TTL",str_ret) if ret_s: self.list_p_r.append(('ping ok',self.str_ip)) else: self.list_p_r.append(('ping error',self.str_ip)) def cmp_s(toupe_str): str_val = toupe_str[1] ret_group = re.match("\d*",str_val[::-1]) str_ret = ret_group.group() return int(str_ret[::-1]) thread_id =[] for i in range(ip_num): thread_id.append(0) thread_id[i] = PingThread("192.168.8.%d" % i, int(i/20),list_ping_result) thread_id[i].start() print(i,end='') while True: if len(list_ping_result) >= ip_num: list_ping_result.sort(key=cmp_s) for i in list_ping_result: print(i) break
相关文章推荐
- python 操作json
- python模拟浏览器登录人人网,并使用代理IP和发送表单数据
- LeetCode----Kth Largest Element in an Array
- python简单图片转字符
- Python: NLTK几个入门函数
- 【python】matplotlib在windows10 和win7下安装
- python抓取网站URL小工具
- python web开发框架django安装和测试
- @修饰符--python中的装饰器
- Python 脚本学习笔记(五)集中式病毒扫描,端口扫描以及分段数据库操作
- Python 脚本学习笔记(五)集中式病毒扫描,端口扫描以及分段数据库操作
- python 抓取美女图片
- 【Python】【解决】UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1: ordinal not in r
- python中格式化字符串
- Python-other-1
- Python 初识Pandas- Python Data Analysis Library
- python 用户登录, 锁定。多用户
- 利用Python的科学计算包scipy画Quantile-Quantile图
- Django+python+BeautifulSoup垂直搜索爬虫
- python-requests快速上手