I've been digging through stackexchange for the past few days and I've found bits and pieces of what I'm trying to accomplish, but I'm unsure how to put it all together...
I'm trying to create a script that has curl calls to an API. This returns a whole bunch of xml which I want to then parse down to just certain values only. Overall, I want this script to make the call, parse the values / set them as a variable, and return(display) them.
I may have found a working type solution, but is this practical?
#!/bin/bash
test=$(curl -k --silent "https://username:[email protected]?.full=true&name=devicename")
test2=$(curl -k --silent "https://username:[email protected]?.devicestatus&name=devicename")
variable1=$grep -oPm1 "(?<=<name>)[^<]+" <<< "$test:)
variable2=$grep -oPm1 "(?<=<status>)[^<]+" <<< "$test:)
echo "$variable"
echo "$variable2"
[admin]>./script
SwitchName
UP
Here's the XML I'm trying to dig through:
<?xml version="1.0" ?>
<queryResponse type="AccessPointDetails" rootUrl="https://website/webacs/api/v1/data" requestUrl="https://website/webacs/api/v1/data/AccessPointDetails?.full=true&name=devicename" responseType="listEntityInstances" count="1" first="0" last="0">
<entity url="https://website/webacs/api/v1/data/AccessPointDetails/14008947223" type="AccessPointDetails" dtoType="accessPointDetailsDTO">
<accessPointDetailsDTO id="14008947223" displayName="14008947223">
<clientCount>6</clientCount>
<clientCount_2_4GHz>0</clientCount_2_4GHz>
<clientCount_5GHz>6</clientCount_5GHz>
<ipAddress>172.16.83.5</ipAddress>
<name>devicename</name>
<unifiedApInfo>
......
</unifiedApInfo>
<upTime>609857</upTime>
</accessPointDetailsDTO>
</entity>
</queryResponse>
<?xml version="1.0" ?>
<queryResponse type="AccessPointDetails" rootUrl="https://website/webacs/api/v1/data" requestUrl="https://website/webacs/api/v1/data/AccessPointDetails?.full=true&name=devicename" responseType="listEntityInstances" count="1" first="0" last="0">
<entity url="https://website/webacs/api/v1/data/AccessPointDetails/14008947223" type="AccessPointDetails" dtoType="accessPointDetailsDTO">
<accessPointDetailsDTO id="14008947223" displayName="14008947223">
<name>devicename</name>
<status>UP</status>
<unifiedApInfo>
......
</unifiedApInfo>
</accessPointDetailsDTO>
</entity>
</queryResponse>
variable=$(curl -k "https://.....")
– Ryan Apr 20 at 16:31grep
is an ugly hack (which may be OK, but don't expect it to work in all cases) – Dmitry Grigoryev Apr 20 at 16:50