1

I have multiple phones I am trying to obtain the up time for phones. I have enabled REST API for all phones and am looking to run CURL scripts to get the value of uptime. I have a script to get the value off of a Polycom phone. The below command works but I have over 3000 devices I would like to do this with. When I run a script of 100 commands I get the output but it is all jumbled together. I have a txt and excel file with all of the IP Adresses of the phones, the username and passwords are the same for all 3000 devices. I am looking for a way to run all commands and then get a text file with the IPADDRESS and return result of the request( IPADRESS:"Status": "2000"" or something similar). I mostly want an easy way to see the results of each curl line per IP address.

Command

curl -d "{\"data\": [\"UpTimeSinceLastReboot\"]}" -H "Content-Type: application/json" -k https://USERNAME:PASSWORD@IPADDRESS/api/v1/mgmt/config/get

Output

{“Status”: “2000”, “data”: {"UpTimeSinceLastReboot": "<DAYS_HOURS_MINUTES_SECONDS>"}

I was able to add >> /tmp/filename.txt to output a txt file with all of the responses but there was no way to accurately coorilate that to the phone's IP address..

mdbrown
  • 11
  • 1
  • 4
  • you haven't given much specific detail about what you are trying to do, so i can only give a very generic suggestion: you already know the ip address, so print it with echo or printf or something before running curl. or store the output of the curl command in a variable and print them together. or redirect all output to a file with the ip address as its name. or....thousands of other variations. – cas Aug 06 '19 at 04:57

1 Answers1

1

In all examples I assume the file is one IP per line.

1. curl and magic

You can achieve Your goal with curl(1) and some magic around

curl -d "{\"data\": [\"UpTimeSinceLastReboot\"]}" \
  -H "Content-Type: application/json" \
  -k \
  -u USERNAME:PASSWORD \
  -w ",%{remote_ip}\t" \
  <(sed 's#^#https://#;s#$#/api/v1/mgmt/config/get#' /path/to/file) \
  | tr -ds '\n\t' '\t\n'

I split the command into multiple lines for better understanding:

  • I moved credentials from URL to curl parameter
  • The key is input from subshell to prefix the ip with protocol https:// and append the path /api/v1/mgmt/config/get
  • -w option writes remote_ip after the response. The format is ,REMOTE_IP<TAB> used later in tr to create desired format
  • piping this through tr(1) removes possible newline at the end of response and creates newline at the end (this is not nice but it should work).

Note: In this example the result is

RESPONSE,IP
…

2. for loop

for ip in $(cat /path/to/file)
do
  echo -ne "$ip\t"
  curl -d "{\"data\": [\"UpTimeSinceLastReboot\"]}" -H "Content-Type: application/json" -k https://USERNAME:PASSWORD@$ip/api/v1/mgmt/config/get
done

result:

IP,RESPONSE
…

3. while loop

while loop

while read ip
do
  echo -ne "$ip\t"
  curl -d "{\"data\": [\"UpTimeSinceLastReboot\"]}" -H "Content-Type: application/json" -k https://USERNAME:PASSWORD@$ip/api/v1/mgmt/config/get
done < /path/to/file

result:

IP,RESPONSE
…
Jakub Jindra
  • 1,392
  • 1
  • 12
  • 25
  • thank you Jakub Jindra for your detailed reply. I am getting the following error: `curl: (3) malformed` I am going to do some research first to see if I can resolve it. – mdbrown Aug 06 '19 at 16:30
  • Yeah, that may be my fault. What of suggested solutions triggred the error? Can you provide output of curl with additional flag -v ? – Jakub Jindra Aug 06 '19 at 16:41
  • This was the output: `* malformed * Closing connection -1 curl: (3) malformed` – mdbrown Aug 06 '19 at 16:51
  • doesn't ring a bell to me. can you try `set -x` and calling it. so we can see what exact shell command is executed? – Jakub Jindra Aug 06 '19 at 16:54
  • For sure: `* Rebuilt URL to: set/ % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Could not resolve proxy: -d * Closing connection 0 curl: (5) Could not resolve proxy: -d curl: (3) [globbing] nested brace in column 10 * malformed * Closing connection -1 curl: (3) malformed` – mdbrown Aug 06 '19 at 17:01
  • no. I meant `set -x` and than `curl …` – Jakub Jindra Aug 06 '19 at 17:02
  • `Could not resolve proxy: -d curl: (3) [globbing] nested brace in column 10 ` but according to this it seems the problem is argument of -d. There's -d inside doublequotes. Try changing doublequotes to single quotes and remove escapes of the inside doublequotes like this `-d '{"data": ["UpTimeSinceLastReboot"]}'` – Jakub Jindra Aug 06 '19 at 17:06
  • Hi Jakub, I was finally able to get it to do what i wanted. Created a .sh file that looks for a txt file with IPs and return with "IPAddress"+"Status" '#!/bin/bash `while read line; do echo result=$(curl -d "{\"data\": [\"UpTimeSinceLastReboot\"]}" -H "Content-Type: application/json" -k https://Polycom:PASSWORD@$line/api/v1/mgmt/config/get); echo $line+$result=; done < IPsgetUptimeSinceLastReboot.txt ` – mdbrown Aug 11 '19 at 17:02