python 接收linux 命令返回数据分析统计
程序员文章站
2024-03-20 23:28:34
...
1、通过linux命令抓取log关键数据,使用python接收linux返回数据进行统计
import os,sys,subprocess
from collections import Counter
from pprint import pprint
cmd = 'cat *.log |grep "ack_status" |cut -d"," -f5,14|cut -d"=" -f2,3|tr -d "value="|sort'
inter_speed = subprocess.Popen(cmd,stdout=subprocess.PIPE,shell=True)
speed = inter_speed.communicate()[0]
c=str(speed,encoding="utf-8").replace('\n',',').replace('\r','').split(',')
ap_ack=c[:-1:2]
ap_values=c[1:-1:2]
new_list=[]
new_dict=[]
mid=map(list,zip(ap_ack,ap_values))
for item in mid:
new_dict=dict(zip(['ap_ack','ap_values'],item))
new_list.append(new_dict)
nl = []
tl = [ str(r) for r in new_list ]
for record in set(tl):
n = eval(record)
n.update({"counts":tl.count(record)})
nl.append(n)
ap_count = {}
ack_count={}
for i in nl:
apid = i['ap_ack']
apval = int(i['ap_values'])
apc = i['counts']
ap_count.setdefault(apid, {'count': 0})
ap_count[apid]['count'] += apc
ack_count.setdefault(apid,{}).setdefault(apval, 0)
ack_count[apid][apval] += apc
pprint(ack_count)
输入结果
上一篇: 华为网络设备基本配置
下一篇: 搭建Magento电子商务网站