判断IP是否可达,threading,queue,subprocess

本文介绍了一种使用Python实现的并行IP可达性检测方法。通过读取包含IP地址的文件,利用多线程并发执行ping命令,快速判断大量IP地址是否可达。此方法提高了网络监控效率,适用于大规模网络环境。

#encoding:utf-8
import subprocess
import threading
from queue import Queue
from queue import Empty
def call_ping(ip):
if subprocess.call([“ping”,’-c’,‘1’,ip]):
print("{0} is alive".format(ip))
else:
print("{0}is unreacheable".format(ip))
def is_reacheable(q):
try:
while True:
ip=q.get_nowait()
call_ping(ip)
except Empty:
pass

def main():
q=Queue()
with open(‘ips.txt’) as f:
for line in f:
q.put(line)
threads=[]
for i in range(10):
thr=threading.Thread(target=is_reacheable,args=(q,))
thr.start()
threads.append(thr)
for thr in threads:
thr.join()
if name == ‘main’:
main()

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值