#!/usr/bin/python3
################################################################################
# ram:
# 	used to return information about the ram configuration in a .json
#   format. This is a helper script for use with the
#   cockpit-hardware package (https://github.com/45Drives/cockpit-hardware)
#
# Copyright (C) 2020, Mark Hooper   <mhooper@45drives.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#   
################################################################################
import subprocess
import re
import json
import sys


def ipmi_lan():
	try:
		ipmi_lan_result = subprocess.Popen(
			["ipmitool","lan","print","1"],stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True).stdout
	except:
		print("Error running 'ipmitool lan print 1'")
		sys.exit(1)
	if ipmi_lan_result is None:
		sys.exit(1)

	ipmi_lan_dict = {
		"IP Address": "-",
		"Subnet Mask": "-",
		"MAC Address": "-",
		"Default Gateway IP": "-"
	}

	for line in ipmi_lan_result:
		for field in ipmi_lan_dict.keys():
			regex = re.search(r"^({fld})\s+:\s+(\S+)".format(fld=field),line)
			if regex != None:
				ipmi_lan_dict[regex.group(1)] = regex.group(2)
	return ipmi_lan_dict

def main():
	ipmi_lan_dict = ipmi_lan()
	result = {
		"ipAddress": ipmi_lan_dict["IP Address"],
		"subnetMask":ipmi_lan_dict["Subnet Mask"],
		"macAddress":ipmi_lan_dict["MAC Address"],
		"defaultGatewayIp":ipmi_lan_dict["Default Gateway IP"]
	}
	print(json.dumps(result,indent = 4))

if __name__ == "__main__":
    main()