0

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.

ultimo_frogman
  • 91
  • 3
  • 11
  • Hi. I did not merge the json.data otput but I made instead of with open(filename, 'wb') as f: I did write ab so it appended to the xml file. This is not a cool because it always adds to a file. Now I have to add some subelements in the input.xml so that i can check them later on and if the result and output element have some other values I need to put inside the false or pass – ultimo_frogman Jan 25 '19 at 11:09
  • Hi. I have e new subquestion to this problem. I wanted to add in a attribute the IF logic of a question that an stdout output should give out. When the output gives another output I should add a value as passed or false to another attribute. dmidecode --type bios | grep -e Version -e "Release Date""BIOS Information"| grep Version | awk '{print $2}' dmidecode --type bios | grep -e Version -e "Release Date" – ultimo_frogman Jan 28 '19 at 07:28
  • Now when we see the text above if the version of BIOS will be less as the number 89 I have to have pass="f" in the xml file. And for other test the same logic with another questions. – ultimo_frogman Jan 28 '19 at 07:29

0 Answers0