Skip to content

Instantly share code, notes, and snippets.

@iwconfig
Created August 3, 2024 19:53
Show Gist options
  • Save iwconfig/03c4ba70ee03d6c9410ce04e0465c738 to your computer and use it in GitHub Desktop.
Save iwconfig/03c4ba70ee03d6c9410ce04e0465c738 to your computer and use it in GitHub Desktop.
Parser for Proxmox .conf files
#!/usr/bin/env python3
import configparser
import json
from pprint import pp
def parse_proxmox_config(file_path):
parser = configparser.ConfigParser(allow_no_value=True, delimiters=':')
with open(file_path, 'r') as file:
content = file.read()
# Prepend a dummy section header
modified_content = '[DEFAULT]\n' + content
parser.read_string(modified_content)
config = {}
for section in parser:
if section != 'DEFAULT':
config['snapshots'] = {section:{}}
for key,value in parser[section].items():
conf = config if section == 'DEFAULT' else config['snapshots'][section]
value, lst, dct = value.strip(), [], {}
# Check if the value contains list items and/or key-value pairs
for val in value.split(','):
if '=' in val:
dct |= dict([val.split('=')])
else:
lst.append(val)
if len(dct)>0:
lst.append(dct)
conf[key] = lst if len(lst)>1 else dct or value
return config
### Example usage ###
config_file_path = '/etc/pve/nodes/pve/qemu-server/9998.conf'
config = parse_proxmox_config(config_file_path)
#pp(config)
print(json.dumps(config,indent=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment