1

When querying my internal API I get a bunch of responses. Those responses will tell me if a specific service is up/down.

Curl command:

curl https://myinternalapi.example.com/Ping/1.1

Output:

<?xml version="1.0" encoding="UTF-8"?><ping><service name="FBcheckOnline" status="down"/><service name="APICheckOn" status="up"/></ping>

I would like to parse the output data using Python/Bash. With the data parsed, I can then create if statements to check if a specific service is up or down.

The desired output would be something like:

FBcheckOnline down
APICheckOn up

How can I do that using Python (2.7) or Bash?

EDIT 1: I managed to get it working with Python 2.7

from xml.dom import minidom
xmldoc = minidom.parse('test.xml')
items = xmldoc.getElementsByTagName('service')

for item in items:
    print item.attributes['name'].value, 
    print item.attributes['status'].value

However, I am trying to make it parse the data from a curl command instead of a file... or make the curl command to send the output to a file and then read it.

0

2 Answers 2

1

I managed to get what I needed with the following:

#!/usr/bin/python
#
#

from xml.dom import minidom
import subprocess

p = subprocess.Popen("curl -s https://api.example.com/Ping/1.1 > curl_api_output_3.xml", stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()

xmldoc = minidom.parse('curl_api_output_3.xml')
items = xmldoc.getElementsByTagName('service')

for item in items:
    print item.attributes['name'].value,
    print item.attributes['status'].value

Feel free to comment any tips you may have, as I am new in Python.

Sign up to request clarification or add additional context in comments.

Comments

1

Not exactly parsing XML, but if output has consistent format, you can try something like,

curl -Ssk https://myinternalapi.example.com/Ping/1.1 | tr '<|/' '\n' | grep ^service | tr '+|=|"' ' ' | awk '{print $3, $NF}'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.