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

被折叠的 条评论
为什么被折叠?



