本文主要是介绍用Python 把 fping -f 的结果写进Excel,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
用Python 把 fping -f 的结果写进Excel,其中targets.txt中为所有目标IP地址:
import os
from openpyxl import Workbook
import socket
import re
import datetime
start_time = datetime.datetime.now()
cmd = 'fping -f targets.txt -c 60 -p 1000 -a 2>&1 | tee fping.log'
os.system(cmd)
end_time = datetime.datetime.now()
wb = Workbook()
IP = socket.gethostbyname(socket.gethostname())
ws = wb.create_sheet(title=IP, index=0)
ws['A1']='目标IP'
ws['B1']='平均延时'
ws['C1']='最大延时'
ws['D1']='最小延时'
ws['E1']='丢包数'
with open('fping.log','r') as f:lines = f.readlines()f.close()
n =2
for i in lines:ws.cell(row=n,column=1).value = i.split(':')[0].strip()ws.cell(row=n,column=2).value = i.split('/')[-2].strip() if len(i.split(','))==2 else ''ws.cell(row=n,column=3).value = i.split('/')[-1].strip() if len(i.split(','))==2 else ''ws.cell(row=n,column=4).value = i.split('=')[2].split('/')[0].strip() if len(i.split(','))==2 else ''ws.cell(row=n,column=5).value = i.split(',')[0].split('/')[-1]n+=1wb.save('targets.xlsx')
print(start_time)
print(end_time)
这篇关于用Python 把 fping -f 的结果写进Excel的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!