FG Policies To CSV
FG Policies To CSV
/usr/bin/env python3
# -*- coding: utf-8 -*-
# OptionParser imports
from optparse import OptionParser
from optparse import OptionGroup
# Options definition
parser = OptionParser(usage="%prog [options]")
# Handful patterns
# -- Entering policy definition block
p_entering_policy_block = re.compile(r'^\s*config firewall policy$', re.IGNORECASE)
p_entering_subpolicy_block = re.compile(r'^\s*config .*$', re.IGNORECASE)
# -- Commiting the current policy definition and going to the next one
p_policy_next = re.compile(r'^next$', re.IGNORECASE)
# -- Policy number
p_policy_number = re.compile(r'^\s*edit\s+(?P<policy_number>\d+)', re.IGNORECASE)
# -- Policy setting
p_policy_set = re.compile(r'^\s*set\s+(?P<policy_key>\S+)\s+(?P<policy_value>.*)$',
re.IGNORECASE)
# Functions
def parse(options):
"""
Parse the data according to several regexes
in_policy_block = False
skip_ssl_vpn_policy_block = False
inspect_next_ssl_vpn_command = False
policy_list = []
policy_elem = {}
order_keys = []
# We match a setting
if p_policy_set.search(line) and not(skip_ssl_vpn_policy_block):
policy_key = p_policy_set.search(line).group('policy_key')
if not(policy_key in order_keys):
order_keys.append(policy_key)
policy_value =
p_policy_set.search(line).group('policy_value').strip()
policy_value = re.sub('["]', '', policy_value)
policy_elem[policy_key] = policy_value
if policy_key == 'action' and policy_value == 'ssl-vpn':
inspect_next_ssl_vpn_command = True
skip_ssl_vpn_policy_block = True
if not(options.skip_header):
spamwriter.writerow(keys)
spamwriter.writerow(output_line)
if options.newline:
spamwriter.writerow('')
fd_output.close()
return None
def main():
"""
Dat main
"""
global parser
if (options.input_file == None):
parser.error('Please specify a valid input file')
return None
if __name__ == "__main__" :
main()