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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
| from openpyxl import Workbook from openpyxl.styles import PatternFill import openpyxl from datetime import datetime
class SecPolicy2XLSX:
def __init__(self):
self.create_time = datetime.now().strftime("%Y%m%d%H%M%S")
self.export_cfg = '205550.log' self.f = open(self.export_cfg, 'r', encoding='utf8')
self.sheet1_th = [[ '名称', 'ID', '源安全域', '目的安全域', '类型', '描述', '源地址', \ '目的地址', '服务', '用户', '动作', '内容安全', '记录日志', '策略匹配统计', '启用' ]] self.sheet2_th = [['对象组名称和被引用', '对象']]
self.wb = Workbook() self.ws1 = self.wb.active self.ws1.title = '安全策略' self.ws2 = self.wb.create_sheet('地址对象组') self.ws3 = self.wb.create_sheet('服务对象组')
for i in self.sheet1_th: self.ws1.append(i) for i in self.sheet2_th: self.ws2.append(i) self.ws3.append(i)
def sp_comtent(self):
comtent_dict = {}
src_zone_list = [] dst_zone_list = [] src_ip_list = [] dst_ip_list = [] service_list = []
while True: cfg_line = self.f.readline() if 'rule' in cfg_line: name = cfg_line.strip().split(' ', 3)[3] id = cfg_line.split()[1] comtent_dict['name'] = name comtent_dict['id'] = id
if 'source-zone' in cfg_line: src_zone_list.append(cfg_line.strip().split()[1])
if 'profile' in cfg_line: profile_type = cfg_line.strip().split()[1].split('_')[1] comtent_dict['profile_type'] = profile_type
if 'description' in cfg_line: desc = cfg_line.strip()[12:] comtent_dict['desc'] = desc
if 'destination-zone' in cfg_line: dst_zone_list.append(cfg_line.strip().split()[1])
if 'source-ip' in cfg_line: src_ip_list.append(cfg_line.strip().split(' ', 1)[1])
if 'destination-ip' in cfg_line: dst_ip_list.append(cfg_line.strip().split(' ', 1)[1])
if 'action' in cfg_line: action = cfg_line.strip().split()[1] comtent_dict['action'] = action
if 'logging ' in cfg_line: logg = cfg_line.strip().split()[1] comtent_dict['logg'] = logg
if 'counting ' in cfg_line: count = cfg_line.strip().split()[1] comtent_dict['count'] = count
if 'disable' in cfg_line: comtent_dict['enable'] = 'disable'
if 'service' in cfg_line: service_list.append(cfg_line.strip().split(' ', 1)[1])
if cfg_line.strip() == '': break
src_zone = ','.join(src_zone_list) dst_zone = ','.join(dst_zone_list) src_ip = ','.join(src_ip_list) dst_ip = ','.join(dst_ip_list) service = ','.join(service_list)
comtent_dict['src_zone'] = src_zone comtent_dict['dst_zone'] = dst_zone comtent_dict['src_ip'] = src_ip comtent_dict['dst_ip'] = dst_ip comtent_dict['service'] = service
return comtent_dict
def ip_obj_comtent(self):
comtent_dict = {} networw_list = []
while True: cfg_line = self.f.readline() if 'Ip address object group' in cfg_line: name = cfg_line.strip().split(' ', 4)[4] comtent_dict['name'] = name
if 'network ' in cfg_line: networw_list.append(cfg_line.strip().split(' ', 2)[2])
if cfg_line.strip() == '': break
networw_list = ','.join(networw_list) comtent_dict['networw_list'] = networw_list
return comtent_dict
def service_obj_comtent(self):
comtent_dict = {} service_list = []
while True: cfg_line = self.f.readline() if 'Service object group' in cfg_line: name = cfg_line.strip().split(' ', 3)[3] comtent_dict['name'] = name
if 'service tcp' in cfg_line or 'service udp' in cfg_line: service_list.append(cfg_line.strip().split(' ', 2)[2]) if cfg_line.strip() == '': break
service_list = ','.join(service_list) comtent_dict['service_list'] = service_list
return comtent_dict
def more_sp_comtent(self):
eof = self.f.seek(0, 2) self.f.seek(0, 0) while True: res = self.sp_comtent() yield res
if self.f.tell() >= eof: break
def more_ip_comtent(self):
eof = self.f.seek(0, 2) self.f.seek(0, 0) while True: res = self.ip_obj_comtent() yield res
if self.f.tell() >= eof: break
def more_service_comtent(self):
eof = self.f.seek(0, 2) self.f.seek(0, 0) while True: res = self.service_obj_comtent() yield res
if self.f.tell() >= eof: break
def sp_fill_task(self):
res = self.more_sp_comtent() comtent_list = list(res)
index_list = [] for i in comtent_list:
key_list = list(i.keys()) if 'name' not in key_list: index_list.append(comtent_list.index(i))
index_list = list(set(index_list)) index_list.sort(reverse=True) for i in index_list: comtent_list.pop(i)
ret_comtent_list = [] for i in comtent_list: if i not in ret_comtent_list: ret_comtent_list.append(i) ret_comtent_list = ret_comtent_list[1:]
for i in ret_comtent_list: i['user'] = 'Any' i['ap_profile'] = 'default' i.setdefault('desc', '-') i.setdefault('enable', 'enable') i.setdefault('profile_type', 'IPv4') i.setdefault('count', 'disable') i.setdefault('logg', 'enable')
ret_comtent_list = [{k: v if v and k != 'desc' else 'Any' for k, v in i.items()} for i in ret_comtent_list] ret_comtent_list[-1]['desc'] = ''
self.ret_comtent_list = ret_comtent_list
return self.ret_comtent_list
def ip_fill_task(self):
res = self.more_ip_comtent() comtent_list = list(res)
ret_comtent_list = [] for i in comtent_list: if i not in ret_comtent_list: ret_comtent_list.append(i) ret_comtent_list = ret_comtent_list[1:]
self.ret_comtent_list = ret_comtent_list
return self.ret_comtent_list
def service_fill_task(self):
res = self.more_service_comtent() comtent_list = list(res)
ret_comtent_list = [] for i in comtent_list: if i not in ret_comtent_list: ret_comtent_list.append(i) ret_comtent_list = ret_comtent_list[1:]
self.ret_comtent_list = ret_comtent_list
self.f.close()
return self.ret_comtent_list
def write2ws1(self):
row = 2 big_list = self.sp_fill_task()
for i in big_list: self.ws1.cell(row=row, column=1, value=i['name']) self.ws1.cell(row=row, column=2, value=i['id']) self.ws1.cell(row=row, column=3, value=i['src_zone']) self.ws1.cell(row=row, column=4, value=i['dst_zone']) self.ws1.cell(row=row, column=5, value=i['profile_type']) self.ws1.cell(row=row, column=6, value=i['desc']) self.ws1.cell(row=row, column=7, value=i['src_ip']) self.ws1.cell(row=row, column=8, value=i['dst_ip']) self.ws1.cell(row=row, column=9, value=i['service']) self.ws1.cell(row=row, column=10, value=i['user']) self.ws1.cell(row=row, column=11, value=i['action']) self.ws1.cell(row=row, column=12, value=i['ap_profile']) self.ws1.cell(row=row, column=13, value=i['logg']) self.ws1.cell(row=row, column=14, value=i['count']) self.ws1.cell(row=row, column=15, value=i['enable']) row += 1
def write2ws2(self):
row = 2 big_list = self.ip_fill_task()
for i in big_list: self.ws2.cell(row=row, column=1, value=i['name']) self.ws2.cell(row=row, column=2, value=i['networw_list']) row += 1
def write2ws3(self):
row = 2 big_list = self.service_fill_task()
for i in big_list: self.ws3.cell(row=row, column=1, value=i['name']) self.ws3.cell(row=row, column=2, value=i['service_list']) row += 1
def save_to_file(self):
self.ws1.column_dimensions['A'].width = 50 self.ws1.column_dimensions['B'].width = 4 self.ws1.column_dimensions['C'].width = 46 self.ws1.column_dimensions['D'].width = 38 self.ws1.column_dimensions['F'].width = 50 self.ws1.column_dimensions['G'].width = 50 self.ws1.column_dimensions['H'].width = 50 self.ws1.column_dimensions['I'].width = 50 self.ws1.column_dimensions['N'].width = 12
self.ws2.column_dimensions['A'].width = 50 self.ws2.column_dimensions['B'].width = 255
self.ws3.column_dimensions['A'].width = 50 self.ws3.column_dimensions['B'].width = 255
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, 16): self.ws1.cell(row=1, column=col).fill = th_color_fill for col in range(1, 3): self.ws2.cell(row=1, column=col).fill = th_color_fill for col in range(1, 3): self.ws3.cell(row=1, column=col).fill = th_color_fill
for r in self.ws1: for c in r: c.alignment = openpyxl.styles.Alignment(vertical='center', wrapText=True) for r in self.ws2: for c in r: c.alignment = openpyxl.styles.Alignment(vertical='center', wrapText=True) for r in self.ws3: for c in r: c.alignment = openpyxl.styles.Alignment(vertical='center', wrapText=True)
self.wb.save(f'XXX防火墙安全策略统计{self.create_time}.xlsx')
if __name__ == '__main__':
x = SecPolicy2XLSX() x.write2ws1() x.write2ws2() x.write2ws3() x.save_to_file() print('搞快点-->>搞快点-->>')
|