python 写的一个windows下ping+tracert 测试网络连通性和路由跟踪

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import re
import copy
import datetime
from multiprocessing.pool import ThreadPool
import time
#Filename: ip.py ping
now_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
pwd='ping-'+now_time+'.txt'
with open(r'ip.txt') as f1:
    cNames = f1.readlines()
def processping(i):
    ip=cNames[i]
    print('ping loading %s' %ip)
    ping = os.popen('ping %s' %ip).read() #执行1次ping命令
    cNames[i] = str(ping+"\n -------------------------------------------")

pool = ThreadPool(processes=20)
pool.map(processping, (i for i in range(0,len(cNames))))
pool.close()

#将处理过的cNames写入新的文件中
with open(r'%s' %pwd,'wb') as pingf:
    pingf.close
with open(r'%s' %pwd,'w') as pingf:
    pingf.writelines(cNames)



#tracert
pwdtracert='tracert-'+now_time+'.txt'
with open(r'ip.txt') as f1:
    cNames = f1.readlines()
def process(i):
    ip=cNames[i]
    print('tracert loading %s' %ip)
    tracert = os.popen('tracert %s' %ip).read() #执行1次ping命令
    cNames[i] = str(tracert+"\n -------------------------------------------")

pool = ThreadPool(processes=20)
pool.map(process, (i for i in range(0,len(cNames))))
pool.close()

#将处理过的cNames写入新的文件中
with open(r'%s' %pwdtracert,'wb') as f2:
    f2.close
with open(r'%s' %pwdtracert,'w') as f2:
    f2.writelines(cNames)

不怎么会python 临时写的 

你可能感兴趣的:(python)