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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
| from textfsm import TextFSM import pandas as pd from openpyxl import Workbook from openpyxl.styles import PatternFill import openpyxl from datetime import datetime import wcwidth
class SP2EXCEL:
def __init__(self, echo_content): self.sp_textfsm_path = 'hp_comware_display_security-policy_ip_local.textfsm' self.obj_ip_textfsm_path = 'hp_comware_display_object-group_ip_local.textfsm' self.obj_service_textfsm_path = 'hp_comware_display_object-group_service_local.textfsm'
with open(echo_content, 'r', encoding='utf8') as f: self.contents = f.read()
def expand_list(self, list_dict_res): for d in list_dict_res: for k, v in d.copy().items(): if isinstance(v, list): obj_item_str = '\n'.join(v) d[k] = obj_item_str
return list_dict_res
def parse_sp(self):
with open(self.sp_textfsm_path, encoding='utf8') as f: template = TextFSM(f) res = template.ParseTextToDicts(self.contents)
for d in res: if d['ACTIVE_STATE'] == '': d['ACTIVE_STATE'] = 'Active' if d['VRF'] == '': d['VRF'] = 'public' if d['PROFILE'] == '': d['PROFILE'] = 'none' if d['LOGGING'] == '': d['LOGGING'] = 'disable' if not d['COUNTING']: d['COUNTING'] = 'disable' if d['TIME_RANGE'] == '': d['TIME_RANGE'] = 'none' if d['DESC'] == '': d['DESC'] = 'none' if not d['SESSION']: d['SESSION'] = 'none' if not d['SRC_ZONE']: d['SRC_ZONE'] = 'any' if not d['DEST_ZONE']: d['DEST_ZONE'] = 'any' if not d['SRC_IP']: d['SRC_IP'] = 'any' if not d['DEST_IP']: d['DEST_IP'] = 'any' if not d['SERVICE']: d['SERVICE'] = 'any' if not d['APPLICATION']: d['APPLICATION'] = 'any' if not d['USER']: d['USER'] = 'any'
res = self.expand_list(res)
df = pd.DataFrame(res)
df_th = {'RULE_ID': 'ID', 'RULE_NAME': '名称', 'ACTIVE_STATE': '时间段状态', 'ACTION': '动作', 'VRF': '公网', 'PROFILE': '内容安全', 'LOGGING': '日志', 'COUNTING': '统计', 'TIME_RANGE': '时间段', 'DESC': '描述', 'SESSION': '会话', 'SRC_ZONE': '源安全域', 'DEST_ZONE': '目的安全域', 'SRC_IP': '源地址', 'DEST_IP': '目的地址', 'SERVICE': '服务', 'APPLICATION': '应用', 'USER': '用户'}
self.sp_df = df.rename(columns=df_th)
self.parse_obj_ip()
def parse_obj_ip(self):
with open(self.obj_ip_textfsm_path, encoding='utf8') as f: template = TextFSM(f) res = template.ParseTextToDicts(self.contents)
res = self.expand_list(res)
df = pd.DataFrame(res)
df_th = {'OBJ_GROUP_NAME': '对象组名称', 'OBJ_ITEM': '对象', 'REFERENCED': '被引用', 'SEC_ZONE': '安全域', 'DESC': '描述'} self.obj_ip_df = df.rename(columns=df_th)
self.parse_obj_service()
def parse_obj_service(self):
with open(self.obj_service_textfsm_path, encoding='utf8') as f: template = TextFSM(f) res = template.ParseTextToDicts(self.contents)
res = self.expand_list(res)
df = pd.DataFrame(res)
df_th = {'OBJ_GROUP_NAME': '对象组名称', 'OBJ_ITEM': '对象', 'REFERENCED': '被引用', 'DESC': '描述'} self.obj_service_df = df.rename(columns=df_th)
def to_excel(self):
self.parse_sp()
create_time = datetime.now().strftime("%Y%m%d%H%M%S") self.output_path = f'核心防火墙安全策略统计{create_time}.xlsx'
with pd.ExcelWriter(self.output_path) as writer: self.sp_df.to_excel(writer, sheet_name='安全策略', index=False) self.obj_ip_df.to_excel(writer, sheet_name='地址对象组', index=False) self.obj_service_df.to_excel(writer, sheet_name='服务对象组', index=False)
self.wb = writer.book self.sheet_names = self.wb.sheetnames
self.define_excel_style()
def define_excel_style(self):
self.ws1 = self.wb[self.sheet_names[0]] self.ws2 = self.wb[self.sheet_names[1]] self.ws3 = self.wb[self.sheet_names[2]]
for sheet_name in self.sheet_names: worksheet = self.wb[sheet_name] for col in worksheet.columns: max_width = 0 column = col[0].column for cell in col: lines = str(cell.value).split("\n") max_line_width = 0 for line in lines: width = wcwidth.wcswidth(line) + 2 if width > max_line_width: max_line_width = width if max_line_width > max_width: max_width = max_line_width worksheet.column_dimensions[openpyxl.utils.get_column_letter(column)].width = max_width
self.ws1.freeze_panes = 'A2' self.ws2.freeze_panes = 'A2' self.ws3.freeze_panes = 'A2'
th_color_fill = PatternFill('solid', fgColor='D3D3D3') for col in range(1, 19): self.ws1.cell(row=1, column=col).fill = th_color_fill for col in range(1, 6): self.ws2.cell(row=1, column=col).fill = th_color_fill for col in range(1, 6): self.ws3.cell(row=1, column=col).fill = th_color_fill
for sheet_name in self.sheet_names: worksheet = self.wb[sheet_name] for r in worksheet: for c in r: c.alignment = openpyxl.styles.Alignment(vertical='center', wrapText=True)
self.wb.save(self.output_path)
if __name__ == '__main__':
echo_content_path = 'output.log' obj = SP2EXCEL(echo_content_path) obj.to_excel()
print('搞快点-->>搞快点-->>')
|