使用python检索所有华为防火墙配置文件的rule,并提取需要的rule到txt文本中
示例:
security-policy
rule name trust_test1
source-zone trust
destination-zone untrust
source-address x.x.x.x mask 255.255.255.0
destination-address address-set test1
service protocol tcp source-port 0 to 65535 destination-port 2222 3333
action permit
rule name trust_test2
source-zone trust
destination-zone untrust
source-address x.x.x.x mask 255.255.255.0
destination-address address-set test2
service protocol tcp source-port 0 to 65535 destination-port 2222 3333
action permit
防火墙配置文件中几万个,其中包含rule test1、test2
要求:提取所有rule中包含test2地址集的rule
在该程序的目录下,新建一个 “FW配置文件” 文件夹存放防火墙配置文件
执行程序后在当前目录下生成FW_rule.txt文件
python代码如下
#-*- coding:utf-8 -*-
# Author:Sheng Luhao
# ScriptName: configuration_get_all_sec_ruleV1.1.py
# Create Date: 2020-12-2 20:05
#***************************************************************#
import os
import re
import sys
if __name__ == '__main__':
find_info = input('输入需要搜索的内容:')
file_list_all = os.scandir('FW配置文件') # 遍历FW文件
file_list = []
for item in file_list_all