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

1
  • 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. Commented Aug 6, 2019 at 4:57

1 Answer 1

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
…
8
  • thank you Jakub Jindra for your detailed reply. I am getting the following error: curl: (3) <url> malformed I am going to do some research first to see if I can resolve it. Commented Aug 6, 2019 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 ? Commented Aug 6, 2019 at 16:41
  • This was the output: * <url> malformed * Closing connection -1 curl: (3) <url> malformed Commented Aug 6, 2019 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? Commented Aug 6, 2019 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 * <url> malformed * Closing connection -1 curl: (3) <url> malformed Commented Aug 6, 2019 at 17:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.