多线程mtr-代码

Posted on 2015-09-06 13:16  NooBkey  阅读(345)  评论(0编辑  收藏  举报
#!/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2015-09-06 11:30:48
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$

import threading
import commands
import time



def Py_Mtr(ip):
        ISOTIMEFORMAT='%Y-%m-%d %X'
        time_now=time.strftime( ISOTIMEFORMAT,time.localtime())
        filename='/tmp/'+ip+'.txt'
        f=open(filename,'w')
        f.write(time_now)
        f.write('\n')
        mtr_out=commands.getstatusoutput("mtr -r -c 20 -n %s"%ip)[1]
        time_now=time.strftime( ISOTIMEFORMAT,time.localtime())
        f.write(mtr_out)
        f.write('\n')
        f.write(time_now)
        f.write('\n')
        f.close()



if __name__=='__main__':
        ip_target=['1.1.1.1','2.2.2.2','3.3.3.3']
        threads = []   #多线程
        print "Begin......"
        for i in ip_target:

                a=threading.Thread(target=Py_Mtr,args=(i,))
                a.start()
                threads.append(a)
        # 等待所有线程完成
        #       for t in threads:
        #               t.join()
                print "Exiting Main Thread"

 

Copyright © 2024 NooBkey
Powered by .NET 9.0 on Kubernetes