ping判断网络连通性(检测IP是否可达)

#shell
for x in `seq 1 255`
do
#echo $x
ip=10.10.1.$x
ping -c 2 $ip > /dev/null
if [ $? == 0 ];then
echo "$ip is alive"
else
echo "$ip is unavialable"
fi
done

#python3
import subprocess
import threading
import IPy
import queue

ip = IPy.IP('10.10.1.0/24')
num_worker_threads = 100

def do_work(item):
retcode = subprocess.call('ping -c 2 %s > /dev/null' % item, shell=True)
if retcode == 0:
#print("{0} is alive".format(item))
with open('alive.txt','a+') as f:
print(item, end='\n', file=f)
else:
#print("{0} is unreachable".format(item))
with open('unreachable.txt', 'a+') as f:
print(item, end='\n', file=f)

def worker():
while True:
item = q.get()
if item is None:
break
do_work(item)
q.task_done()

q = queue.Queue()
threads = []
for i in range(num_worker_threads):
t = threading.Thread(target=worker)
t.start()
threads.append(t)

for item in ip:
q.put(item)

# block until all tasks are done
q.join()

# stop workers
for i in range(num_worker_threads):
q.put(None)
for t in threads:
t.join()



posted @ 2018-03-20 16:11  helloworld899  阅读(1041)  评论(0编辑  收藏  举报