堡垒机访问记录
''' 堡垒机访问记录自动化 ''' import pandas as pd import numpy as np # 显示全部列 pd.options.display.max_columns = 999 # 列对齐 pd.set_option('display.unicode.ambiguous_as_wide', True) pd.set_option('display.unicode.east_asian_width', True) # 常量 total = pd.read_excel('/Users/soymilk/微云同步助手(296672942)/OY/excel_auto/bastion/堡垒机访问记录.xlsx') ori_cent_date = pd.read_excel('/Users/soymilk/微云同步助手(296672942)/OY/excel_auto/bastion/核心数据库.xlsx') ip_list = ori_cent_date.IP.tolist() center_data = pd.DataFrame() # ---【2】会话大小--- # 转化单位为KB for x in range(0,len(total)): y = total.iloc[x,12] if 'MB' in y: y = y.replace('MB','') y = float(y) * 1024 y = int(y) y = str(y) + 'KB' total.iloc[x,12] = y elif 'GB' in y: y = y.replace('GB','') y = float(y) * 1024 * 1024 y = int(y) y = str(y) + 'KB' total.iloc[x,12] = y elif 'TB' in y: y = y.replace('TB','') y = float(y) * 1024 * 1024 * 1024 y = int(y) y = str(y) + 'TB' total.iloc[x,12] = y else: y = y total.iloc[x,12] = y # 会话总和 data_size = total['会话大小'].apply(lambda x:x[:-2]) data_size = data_size.astype('int64') data_size_sum = data_size.sum() if 4 <= len(str(data_size_sum)) < 7: data_size_sum = '%.2f' % (data_size_sum / 1024) data_size_sum = str(data_size_sum) + 'MB' elif 7 <= len(str(data_size_sum)) < 10: data_size_sum = '%.2f' % (data_size_sum / 1024 / 1024) data_size_sum = str(data_size_sum) + 'GB' elif 10 <= len(str(data_size_sum)) < 13: data_size_sum = '%.2f' % (data_size_sum / 1024 / 1024 / 1024) data_size_sum = str(data_size_sum) + 'TB' else: data_size_sum = str(data_size_sum) + 'KB' # ---【2】核心数据库--- # 开局定义空DataFrame,迭代相加 for i in ip_list: center_data = center_data.append(total.loc[total['主机IP']==i]) # ---【3】汇总--- total_sum_data = { '项目':['本周运维次数','核心数据库传输量'], '数值':[len(total.index),data_size_sum] } total_sum = pd.DataFrame(total_sum_data,index=['本周运维次数','核心数据库传输量'],columns=['项目','数值']) # ---【4】保存导出excel--- writer = pd.ExcelWriter('/Users/soymilk/微云同步助手(296672942)/OY/excel_auto/bastion/bastion_finish/堡垒机访问记录finish.xlsx') # center_date.set_index('类型') total_sum.to_excel(writer,sheet_name='汇总') total.to_excel(writer,sheet_name='会话大小') center_data.to_excel(writer,sheet_name='核心数据库') writer.save() print('auto_excle success')
知识点:
读取excel文件
total = pd.read_excel('/Users/soymilk/微云同步助手(296672942)/OY/excel_auto/bastion/堡垒机访问记录.xlsx') ori_cent_date = pd.read_excel('/Users/soymilk/微云同步助手(296672942)/OY/excel_auto/bastion/核心数据库.xlsx') print(total)
DataFrame转化为list
ip_list = ori_cent_date.IP.tolist()
循环实现迭代相加
ip_list = ori_cent_date.IP.tolist() # 转化为list好实现循环(可能有更好的方法) center_data = pd.DataFrame() # 定义空的DataFrame for i in ip_list: center_data = center_data.append(total.loc[total['主机IP']==i]) print(center_data)
# 数字迭代
# a = [11,22,33,44,55,66,77,88,99]
# sum_a = 0
# for i in a:
# sum_a += i
# print(sum_a)
if elif else的运用
for x in range(0,len(total)): y = total.iloc[x,12] # 列的id为12 if 'MB' in y: # 如果y包含字符串'MB' y = y.replace('MB','') # 删除'MB'(将'MB'替换为空字符串) y = float(y) * 1024 # 转化为浮点型乘以1024转化单位为'KB' y = int(y) # 转化为整数 y = str(y) + 'KB' # 转化为字符串 并加上字符串'KB',形成最终想要的效果 total.iloc[x,12] = y # 将最终的结果赋值给total.iloc[x,12] elif 'GB' in y: y = y.replace('GB','') y = float(y) * 1024 * 1024 y = int(y) y = str(y) + 'KB' total.iloc[x,12] = y elif 'TB' in y: y = y.replace('TB','') y = float(y) * 1024 * 1024 * 1024 y = int(y) y = str(y) + 'TB' total.iloc[x,12] = y else: y = y total.iloc[x,12] = y
会话总和
# 会话总和 data_size = total['会话大小'].apply(lambda x:x[:-2]) # 只从倒数第2位开始取值(不包含倒数第2位) data_size = data_size.astype('int64') # 转化为整形 data_size_sum = data_size.sum() if 4 <= len(str(data_size_sum)) < 7: data_size_sum = '%.2f' % (data_size_sum / 1024) # 只取小数点后2位 data_size_sum = str(data_size_sum) + 'MB' elif 7 <= len(str(data_size_sum)) < 10: data_size_sum = '%.2f' % (data_size_sum / 1024 / 1024) data_size_sum = str(data_size_sum) + 'GB' elif 10 <= len(str(data_size_sum)) < 13: data_size_sum = '%.2f' % (data_size_sum / 1024 / 1024 / 1024) data_size_sum = str(data_size_sum) + 'TB' else: data_size_sum = str(data_size_sum) + 'KB'