Skip to content

Instantly share code, notes, and snippets.

@whoo
Created June 5, 2020 17:49
Show Gist options
  • Save whoo/968ce1ae32070632243fb46a85e12ec4 to your computer and use it in GitHub Desktop.
Save whoo/968ce1ae32070632243fb46a85e12ec4 to your computer and use it in GitHub Desktop.
import json
config = {}
with open('fortigate.cfg') as fh:
for line in fh:
args = line.split()
action = args.pop(0)
# python3: action, *args = line.split()
if action == 'config':
header = ' '.join(args)
if header not in config:
config[header] = {}
if action == 'edit':
section = ' '.join(args).strip('"')
if section not in config[header]:
config[header][section] = {}
if action == 'set':
name = args.pop(0)
value = ' '.join(args).strip('"')
config[header][section][name] = value
print(json.dumps(config))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment