Skip to main content
Solved

Extracting policy status through API


Forum|alt.badge.img+8

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!!!

Best answer by mm2270

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 original
Did this topic help you find an answer to your question?

2 replies

mm2270
Forum|alt.badge.img+16
  • Legendary Contributor
  • 7880 replies
  • Answer
  • June 25, 2012

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.


Forum|alt.badge.img+8
  • Author
  • Contributor
  • 38 replies
  • June 25, 2012

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!


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings