Extracting policy status through API

adroitboy
New Contributor III

I'm trying to get the status of a policy using the API and a script. I can't seem to extract the text I am looking for. I'm using curl to read the XML

curl -v -k -u API_username:API_password https://jss.example.com:8443/JSSResource/policies/id/17

It comes in a long single-line string that I've placed some below. From that string I'm hoping to read what's between <enabled> </enabled>. I can't seem to figure it out.

<?xml version="1.0" encoding="UTF-8" standalone="no"?><policy><general><id>181</id><name>ZombieComputers</name><enabled>false</enabled><trigger>every15</trigger><trigger_other/><frequency>Disabled</frequency><offline>false</offline><category><id>20</id><name>NetRender</name></category><date_time_limitations><activation_date>2011-08-30 16:00:00</activation_date> ....and it goes on from here....

Anyone?

Thanks!!!

1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

I had to tackle this type of scenario once before. I honestly don't know the best way to read the long line of xml data that the API pumps out. Its not easy using the shell. It might be better with something like perl, but I don't know perl scripting.
Anyway, I used the following to get the exact string I was looking for. This is obviously inefficient, but it works.

curl -v -k -u API_username:API_password https://jss.example.com:8443/JSSResource/policies/id/17 | awk -F"<enabled>" '{ print $2 }' | awk -F"</enabled>" '{ print $1 }'

Basically, using awk, set the field separator to <enabled> and print everything after it, then again pass it through awk to set the field separator to </enabled> and print everything before it, which leaves you with the text between <enabled> and </enabled>

As I said, its ugly and I'm certain there is a better way, but the API documentation is a little sparse on exactly how to use the data you can extract from it, so I wasn't able to figure anything else out.

Edit: FWIW, I had tried using both awk's and sed's start/end pattern matching to pull the data between two tags but wasn't able to get it to work on the output the API spits out. It just doesn't seem to work on that string. Maybe someone else can figure out how to do it though.

View solution in original post

2 REPLIES 2

mm2270
Legendary Contributor III

I had to tackle this type of scenario once before. I honestly don't know the best way to read the long line of xml data that the API pumps out. Its not easy using the shell. It might be better with something like perl, but I don't know perl scripting.
Anyway, I used the following to get the exact string I was looking for. This is obviously inefficient, but it works.

curl -v -k -u API_username:API_password https://jss.example.com:8443/JSSResource/policies/id/17 | awk -F"<enabled>" '{ print $2 }' | awk -F"</enabled>" '{ print $1 }'

Basically, using awk, set the field separator to <enabled> and print everything after it, then again pass it through awk to set the field separator to </enabled> and print everything before it, which leaves you with the text between <enabled> and </enabled>

As I said, its ugly and I'm certain there is a better way, but the API documentation is a little sparse on exactly how to use the data you can extract from it, so I wasn't able to figure anything else out.

Edit: FWIW, I had tried using both awk's and sed's start/end pattern matching to pull the data between two tags but wasn't able to get it to work on the output the API spits out. It just doesn't seem to work on that string. Maybe someone else can figure out how to do it though.

adroitboy
New Contributor III

That seems to do it. I too had triend many forms of sed/awk/grep and coun't figure out anything. Your method seems to work and sometimes you just need it to work.

Thanks!