2013年9月18日 星期三

[python] ping6 to Ipv6


[python] ping6 to Ipv6
#coding:utf8
import os
import threading
import Queue
queue = Queue.Queue()
address = ['2001:730:1f:462::2'] # 2001:730:1f:462::2 = CNR
_thread = 1
for ip in address:
queue.put(ip) #将IP放入队列中。函数中使用q.get(ip)获取

def check(i,q):
while True:
ip=q.get() #获取Queue队列传过来的ip,队列使用队列实例queue.put(ip)传入ip,通过q.get() 获得
print "Thread %s:Pinging %s" %(i,ip)
data = os.system("ping6 -c 1 %s > /dev/null 2>&1" % ip)#使用os.system返回值判断是否正常
if data==0:
print "%s:正常运行" % ip
else:
print "%s:停止工作" % ip
q.task_done() #表示queue.join()已完成队列中提取元组数据

for i in range(_thread):#线程开始工作
run=threading.Thread(target=check,args=(i,queue)) #创建一个threading.Thread()的实例,给它一个函数和函数的参数
run.setDaemon(True)#这个True是为worker.start设置的,如果没有设置的话会挂起的,因为check是使用循环实现的
run.start() #开始线程的工作
queue.join()#线程队列执行关闭
print "ping 工作已完成"

Reference :


Related Posts:

0 意見:

張貼留言