python 多线程ping测试_python3实现多线程ping一个网段

该博客介绍了一个Python脚本,用于通过多线程技术ping一个网段内的所有地址,检测其网络连通性。代码创建了一个Pinger线程类,用于执行ping操作,并利用队列进行任务分配。在主函数中,创建并启动多个Pinger线程,每个线程负责ping一个特定的IP地址。这个工具对于网络管理员进行网络故障排查非常有用。
摘要由CSDN通过智能技术生成

####################################################

"""

实现多线程ping一个网段的地址,测试联通性

copyright  2016/9/30 lighter_py

"""

####################################################

import os

import queue

import threading

class Pinger(threading.Thread):

def __init__(self,queue,pingIp,pingCoint=1):

threading.Thread.__init__(self)

self.queue = queue

self.pingIp = pingIp

self.pingCount = 1

def run(self):

pingResult = os.popen(‘ping -n‘ + ‘ ‘ + str(self.pingCount) + ‘ ‘ +self.pingIp).read()

if ‘无法访问目标主机‘ not in pingResult:

print(self.pingIp,‘\t is online‘)

self.queue.get()

class creatpinger:

def __init__(self,queue,pingIpParagraph,allcount=255,pingCount=1):

self.queue = queue

self.pingIpParagraph = pingIpParagraph

self.allcount = allcount

self.pingCount = 1

self.create()

def create(self):

for i in range(1,self.allcount+1):

self.queue.put( i )

Pinger(self.queue,self.pingIpParagraph+str(i),self.pingCount).start()

if __name__ == ‘__main__‘:

creatpinger(queue.Queue(100),‘192.168.1.‘)

原文:http://11888798.blog.51cto.com/11878798/1858226

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值