Curl/http 503 error with bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Curl/http 503 error with bash script
# 1  
Old 12-04-2017
Curl/http 503 error with bash script

I am trying to use REST API and curl in a bash script to generate a http report. The curl command at the end of the script should generate a html file but instead I get an error "HTTP/1.1 503 Service Unavailable".

This is the script

Code:
#!/bin/bash

export HOME=/export/home/kfoley/scripts/REST_API
export TEMP_DIR=/export/home/kfoley/scripts/REST_API/Temp

echo "Enter the hostname:"
read HOSTNAME

curl -u username:Password  http://seieadpa01:9004/apollo-api/nodes/?query=name%3d$HOSTNAME > $TEMP_DIR/test_file.txt

HOST_ID=`cat $TEMP_DIR/test_file.txt | grep "<id>"`

cat $HOME/host_query_input_file.out | awk -v ID="$HOST_ID" '{if ($1~/\<id>/) $1=ID ;print}' > $TEMP_DIR/host_query_input_file.out

curl -u username:Password -X POST -H "Content-Type:application/vnd.emc.apollo-v1+xml" --data "@/export/home/kfoley/scripts/REST_API/Temp/host_query_input_file.out" http://seieadpa01:9004/dpa-api/report -o $TEMP_DIR/test_xml.out

LINK=`cat $TEMP_DIR/test_xml.out | grep link | sed 's/<link>//g;s/<\/link>//g'| awk '{sub(/^[ \t]+/, ""); print}'`

#curl -u username:Password $LINK -o /symdata/DPA-REST-API-REPORTS/tester.html

curl -u username:Password $LINK

yet when I run the same command outside the script from the command line I do not get any error.

Code:
LINK="http://seieadpa01:9004/dpa-api/report/result/f73356fb-c08c-4ab8-90ec-7caa0bd19d50"
curl -u username:Password $LINK


Anybody know what I am doing wrong here? I'm thinking I need to define a CURL variable within the script?
# 2  
Old 12-04-2017
Wouldn't it be helpful to show the LINK variable's contents within the script?
# 3  
Old 12-04-2017
Hi RudiC,

I'm not sure what you mean? The LINK variable is defined from this line in the script.

Code:
LINK=`cat $TEMP_DIR/test_xml.out | grep link | sed 's/<link>//g;s/<\/link>//g'| awk '{sub(/^[ \t]+/, ""); print}'`

Once the script finishes running (albeit with the 503 service available error) I am able to define the LINK variable from the command line and run the curl GET using $LINK or using the variables contents - either way it works when I run it outside of the script.

If I echo the $LINK variable within the script to get it's contents it is what I expect it to be.
# 4  
Old 12-04-2017
So, we can shorten the definition of LINK somewhat. Can you show us the output you get from
Code:
uname -a
ps -f
LINK=$(grep link $TEMP_DIR/test_xml.out)
echo "${LINK}"

Please follow this by the value you would want to convert it to in this case. We will then be able to clearly see what you are trying to get and be better placed to try to work out where it's going wrong. I've included the first two statements to show us your OS and shell so we know what we're able to work with.

Please post all the output in CODE tags.


Thanks, in advance,,
Robin
# 5  
Old 12-04-2017
Hi Robin,

this is the output from the commands you asked me to run.

Code:
[root@seiemccli01 REST_API]# uname -a
Linux seiemccli01 2.6.39-400.215.10.el5uek #1 SMP Tue Sep 9 22:51:46 PDT 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@seiemccli01 REST_API]# ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
root     14440 14435  0 10:05 pts/1    00:00:00 -bash
root     14590 14440  0 10:08 pts/1    00:00:00 ps -f
[root@seiemccli01 REST_API]# LINK=$(grep link $TEMP_DIR/test_xml.out)
[root@seiemccli01 REST_API]# echo "${LINK}"
   <link>http://seieadpa01:9004/dpa-api/report/result/57ba6675-1aa6-47cc-afdf-205b1d36d49c</link>
[root@seiemccli01 REST_API]#


In the script I strip out the leading and trailing <link>

Thanks.
# 6  
Old 12-04-2017
LinkedIn

So a few suggestions to get your script neater only making one external call:-
Code:
LINK=$(grep link $TEMP_DIR/test_xml.out)            # Read the line you want from the file
LINK="${LINK#*>}"                                   # Strip first part up to '>'
LINK="${LINK%<*}"                                   # Strip last part up from '<'


Can you run your code both from the command line and scheduled but adding set -x just before you try to curl and see a difference between the trace output.

Obviously, if you post output here, overwrite the credentials so they are not published to the world.




Regards,
Robin
# 7  
Old 12-04-2017
Try running your curl manually in command prompt and check if it works:-
Code:
curl -u Username:Password "$LINK"

If you still get a 503 - Service Unavailable, then check if you have a proxy server. If yes, try:-
Code:
export http_proxy="<proxy_host>:<proxy_port>"

curl -u Username:Password "$LINK"

or
Code:
curl -x <proxy_host>:<proxy_port> -u Username:Password "$LINK"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Curl , download file with user:pass in bash script

Hello, My question is about curl command. (ubuntu14.04) In terminal, I am able to download my mainfile with: curl -u user1:pass1 http://11.22.33.44/******* When I convert it into bash script like this: #!/bin/bash cd /root/scripts computer_ip=11.22.33.44 curl -u $1:$2... (8 Replies)
Discussion started by: baris35
8 Replies

2. Shell Programming and Scripting

Access a complete flow with CURL + bash shell

Hello Experts , I have an use case which needed your help . I have been using google for 2 days buy couldn`t succed , i believe i can get the help here. Here is my use case to run on bash shell 1. Access an URL -- in script , it will be mentioned as inputURL 2. Once i accessed the URL... (5 Replies)
Discussion started by: radha254
5 Replies

3. Shell Programming and Scripting

Using curl in bash script

Hello. I have pick up a script from internet to track errors from curl command. #!/bin/bash # URL_TO_TEST="http://www.xxxxxx.yyy" MY_VAR=curl_init("$URL_TO_TEST") ; curl_setopt($MY_VAR, CURLOPT_HEADER, 1); curl_setopt($MY_VAR, CURLOPT_RETURNTRANSFER, 1); curl_setopt($MY_VAR,... (2 Replies)
Discussion started by: jcdole
2 Replies

4. Shell Programming and Scripting

awk script to find time difference between HTTP PUT and HTTP DELETE requests in access.log

Hi, I'm trying to write a script to determine the time gap between HTTP PUT and HTTP DELETE requests in the HTTP Servers access log. Normally client will do HTTP PUT to push content e.g. file_1.txt and 21 seconds later it will do HTTP DELETE, but sometimes the time varies causing some issues... (3 Replies)
Discussion started by: Juha
3 Replies

5. Shell Programming and Scripting

Sending awk variables into curl in a bash script

Hello experts! I have a file1 with the following format (yr,day, month, hour,minute): 201201132435 201202141210 201304132030 201410100110 ... What i want to do is to assign variables and then use them in the curl command to download the text of each event from a web page. What I have... (6 Replies)
Discussion started by: phaethon
6 Replies

6. Linux

Apt-get update wont run and gives 503 service unavailable error

Hi, I am running on Ubuntu 12 (precise) 64 bit and came to know that i cannot install any packages using apt-get and aptitude. Linux test01 3.5.0-23-generic #35~precise1-Ubuntu SMP Fri Jan 25 17:13:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux But the same /etc/apt/sources.list file is... (20 Replies)
Discussion started by: prash358
20 Replies

7. UNIX for Advanced & Expert Users

Syntax issue in curl for getting a file from http site

Hi All I am using curl in my client environment. We need to pull a file from the libraries site. All the files are having perticular URL which can be used to get the files. Only issue is, when we fire that command , it will take to authentication page 1st. once authentication is succed, it will... (1 Reply)
Discussion started by: prabirkumardhar
1 Replies

8. Shell Programming and Scripting

bash curl escape & in url

I'm running a curl command in bash, but the & in the middle causes the second half of the line to run in the background, here's what I'm trying to do: lat="37.451" lon="-122.18" url="http://ws.geonames.org/findNearestAddress?lat=$lat&lng=$lon" curl -s "$url" I tried escaping the & with \&,... (4 Replies)
Discussion started by: unclecameron
4 Replies

9. UNIX for Advanced & Expert Users

Issue in Curl to send http POST request with attachment/multipart

Hi, I am using curl to hit a url using http in solaris 10 using commandline, I want to transfer an attachment(using multipart curl -F) also as a part of the request. If anyone has used kindly help me with the syntax. I am using below command: Code: /usr/local/bin/curl -v... (1 Reply)
Discussion started by: manishmaha
1 Replies

10. Shell Programming and Scripting

Bash script idea using cUrl -- possible?

I have an alias already in my .bash_profile to download files using cUrl's -o (output to file, user provides the file name) option. I find I'm using it quite a bit, so I wanted to write a script to run "curl -o", taking the necessary inputs - file name and URL from which to download - and then... (3 Replies)
Discussion started by: SilversleevesX
3 Replies
Login or Register to Ask a Question