I have some code that takes an input.xml file from server commands and I get the output.xml
Bu I need to check two servers and have a loop at end for this and I check it but I get only the last result inside output.xml file.
I would need to merge the json.dumps(data) in one dictionary or whatever xmltodict produces and then parse into one xml.
I have tried some dictionary update but it did not work.
The code is here:
def get_output_dict():
if vendor == 'HP':
data = xml_to_dict(xml_doc='hp_input.xml')
elif vendor == 'Dell':
data = xml_to_dict(xml_doc='dell_input.xml')
for test in data['platform']['vendor']['tests']:
command = test.get('command') #continue if command is not present
output = remote(command)
str1 = ''.join(str(e) for e in output)
for key in test.keys():
if key == 'command':
test[key] = str1
#Change command key name with result using .pop
test['Result'] = test.pop('command')
return json.loads(json.dumps(data))
def get_output_xml(output_dict):
#dicttoxml.set_debug()
output_xml = dicttoxml.dicttoxml(output_dict,custom_root='output',attr_type=False,root=False)
if vendor == 'HP':
filename = 'hp_output-{}.xml'.format(host)
elif vendor == 'Dell':
filename = 'dell_output-{}.xml'.format(host)
tree = etree.fromstring(output_xml)
output_xml_string = etree.tostring(tree, pretty_print=True)
with open(filename, 'wb') as f:
f.write(output_xml_string)
print('Output for hostname server: {} written to:
{}'.format(host,filename))
return output_xml
for x in range(3, len(sys.argv)):
print("Checking Server: %s" % (sys.argv[x]))
remote = myssh(sys.argv[x], username, password)
data = get_output_dict()
xml = get_output_xml(data)
The result should be that I at get_output_xml(data) get the data merged from two iterations of two servers, and maybe later on 3 servers.