?
Solved

curl using https

Posted on 2014-10-20
16
Medium Priority
?
311 Views
Last Modified: 2014-10-20
I use the following command to get a result code from the web server. If 200, then all is good, if anything else, jump to something else in the script.

function primary_check()
{
        RES="$(curl -m 5 -o /dev/null --silent --head --write-out '%{http_code}\n' $IP1 -o /dev/null)"
        if [ ! $RES -eq 000 ];then
    # return a non empty value if all is good
                echo "GOOD"
        fi
    # don't return if there was an error
}

Open in new window


In the above,
curl -m 5 -o /dev/null --silent --head --write-out '%{http_code}\n' $IP1 -o /dev/null
returns simply 200 which is what I want.

Problem is... I need to start using https instead because I need to set up htpasswd on the server.

I am not having any luck returning only the result code so my question is looking for a solution.

curl -m 5 -k --key server.key --cert server.crt -o /dev/null --silent --head --write-out -u xxxx|xxxxxxx" '%{http_code}\n' server.com -o /dev/null

-u-uHTTP/1.1 200 OK
Date: Mon, 20 Oct 2014 23:21:08 GMT
Server: Apache
Last-Modified: Sat, 02 Aug 2014 01:12:25 GMT
ETag: "2dc00fb-f-4ff9b32d9f040"
Accept-Ranges: bytes
Content-Length: 15
Connection: close
Content-Type: text/html; charset=UTF-8
0
Comment
Question by:projects
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 6
16 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40393415
Double period

server..crt
0
 

Author Comment

by:projects
ID: 40393420
Just a typo. I've updated the question. Thanks.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40393429
This should just echo the response code

curl -m 5 -k --key server.key --cert server.crt -s -o /dev/null -u "xxxx|xxxxxxx"  -w '%{http_code}\n' https://www.google.com

Open in new window

0
On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

 

Author Comment

by:projects
ID: 40393432
Using the following;

curl -m 5 -k --key server.key --cert server.crt -s -o /dev/null -w -u "xxxx|xxxxxx" '%{http_code}\n' https://server.com

The response is the single line in the index.html plus what you see in front.

-u-uNothing here!
0
 

Author Comment

by:projects
ID: 40393435
Removing to -s to see what's going on... looks like an order of things?

# curl -m 5 -k --key server.key --cert server.crt -o /dev/null -w -u "xxxx|xxxxxx" '%{http_code}\n' https://server.com
curl: (6) Couldn't resolve host 'xxxx|xxxxxx'
curl: (6) Couldn't resolve host '%http_code\n'
-u-uNothing here!
0
 
LVL 58

Expert Comment

by:Gary
ID: 40393436
That's not the same as I posted (I may have edited it while you were copying it)
0
 

Author Comment

by:projects
ID: 40393437
Your does the same thing but remember, I also don't have any name/passwd to google so it'll fail anyhow.

Yours does return the code however but it's a 000
0
 
LVL 58

Expert Comment

by:Gary
ID: 40393441
Uh?
That's just an example, if you press enter on the challenge you'll get the response code (000 - connection cancelled) , but you should be entering your URL and user/pass

Edit
In cygwin you will get 000 in Linux you will get 302
0
 

Author Comment

by:projects
ID: 40393443
Yes, I have substituted my own info of course.
That's what I posted above.

Let me check, I bet I've got a typo.
0
 

Author Comment

by:projects
ID: 40393447
# curl -m 5 -k --key server.key --cert server.crt -s -o /dev/null -u "xxxx|xxxxxx"  -w '%{http_code}\n' https://server.com
Enter host password for user 'xxxx|xxxxxx':
200



So, the above is what I'm using, with my own certs, name/pass and server of course.
It keeps prompting me for the name/password but does give me a 200 after I hit return.
0
 

Author Comment

by:projects
ID: 40393448
This is on Linux.
0
 
LVL 58

Accepted Solution

by:
Gary earned 2000 total points
ID: 40393451
I shouldn't have relied on your code being right

curl -m 5 -k --key server.key --cert server.crt -u xxxx:xxxxxxx -s -o /dev/null  -w '%{http_code}\n' https://www.google.com

Open in new window

0
 

Author Comment

by:projects
ID: 40393455
What does that mean??
0
 
LVL 58

Expert Comment

by:Gary
ID: 40393456
This bit
-u xxxx:xxxxxxx

You had
-u xxxx|xxxxxxx
0
 

Author Comment

by:projects
ID: 40393464
Yes, I see that too now. I have no idea how that got in there because my code actually uses the semi colon.
Maybe another typo when I was editing the question, sorry.

Works as it should, thanks very much. I'll test it and award shortly.
0
 

Author Comment

by:projects
ID: 40393472
curl -m 5 -k --key server.key --cert server.crt -u xxxx:xxxxxxx -s -o /dev/null  -w '%{http_code}\n' https://www.google.com

So same thing, without the authentication
curl -m 5 -s -o /dev/null  -w '%{http_code}\n' https://www.google.com

Cool, that's what I was struggling with. Thanks very much.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question