gpt4 book ai didi

python - 使用 Python 读取 fortigate 配置文件

转载 作者:行者123 更新时间:2023-12-01 06:06:59 32 4
gpt4 key购买 nike

对这个冗长的问题表示歉意。

我正在尝试读入配置文件并获取规则列表。我尝试使用 ConfigParser 来执行此操作,但它不是标准配置文件。该文件不包含节标题和 token 。

config section a
set something to something else
config subsection a
set this to that
next
end

config firewall policy
edit 76
set srcintf "There"
set dstintf "Here"
set srcaddr "all"
set dstaddr "all"
set action accept
set schedule "always"
set service "TCP_5600"
next
edit 77
set srcintf "here"
set dstintf "there"
set srcaddr "all"
set dstaddr "all"
set action accept
set schedule "always"
set service "PING"
next
end

由于我无法弄清楚如何让 ConfigParser 工作,我想我应该尝试迭代该文件,不幸的是我没有太多的编程技能,所以我陷入了困境。我真的认为我让事情变得比应有的更复杂了。这是我编写的代码;

class Parser(object):

def __init__(self):
self.config_section = ""
self.config_header = ""
self.section_list = []
self.header_list = []

def parse_config(self, fields): # Create a new section
new_list = []
self.config_section = " ".join(fields)
new_list.append(self.config_section)

if self.section_list: # Create a sub section
self.section_list[-1].append(new_list)
else: self.section_list.append(new_list)

def parse_edit(self, line): # Create a new header
self.config_header = line[0]
self.header_list.append(self.config_header)

self.section_list[-1].append(self.header_list)

def parse_set(self, line): # Key and values
key_value = {}

key = line[0]
values = line[1:]
key_value[key] = values

if self.header_list:
self.header_list.append(key_value)
else: self.section_list[-1].append(key_value)

def parse_next(self, line): # Close the header
self.config_header = []

def parse_end(self, line): # Close the section
self.config_section = []

def parse_file(self, path):
with open(path) as f:
for line in f:

# Clean up the fields and remove unused lines.
fields = line.replace('"', '').strip().split(" ")

if fields[0] == "set":
pass
elif fields[0] == "end":
pass
elif fields[0] == "edit":
pass
elif fields[0] == "config":
pass
elif fields[0] == "next":
pass
else: continue

# fetch and call method.
method = fields[0]
parse_method = "parse_" + method

getattr(Parser, parse_method)(self, fields[1:])
return self.section_list

config = Parser().parse_file('test_config.txt')

print config

我正在寻找的输出类似于以下内容;

[['section a', {'something': 'to something else'}, ['subsection a', {'this': 'to that'}]],['firewall policy',['76',{'srcintf':'There'}, {'dstintf':'Here'}{etc.}{etc.}]]]

这就是我得到的

[['section a']]

编辑

我更改了上述内容以反射(reflect)我目前的情况。我在获得预期的输出时仍然遇到问题。我似乎无法正确列出该列表。

最佳答案

 class Parser(object):

def __init__(self):
self.my_section = 0
self.flag_section = False
# ...

def parse_config(self, fields):
self.my_section += 1
# go on with fields
# ...
self.flag_section = True

def parse_edit(self, line):
...

def parse_set(self, line):
...

def parse_end(self, line):
...

def parse_file(self, path):
with open(path) as f:
for line in f:
fields = f.strip().split(" ")

method = fields[0]
# fetch and call method
getattr(Parser, "parse_" + method)(self, fields[1:])

关于python - 使用 Python 读取 fortigate 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7552364/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com