多线程mtr-代码

Posted on   NooBkey  阅读(346)  评论(0编辑  收藏  举报
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/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 © 2025 NooBkey
Powered by .NET 9.0 on Kubernetes

点击右上角即可分享
微信分享提示