ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to test the condition of an if statement against a curl response, however for some reason it will not work no matter how many ways I try, please can someone take a look and see what I could be doing wrong?
I am curious if there is likely to be the word 'OK' anywhere else in the header?
It's not likely but still possible. One of the other headers could contain the two letters next to each other. The "grep" solution could still look for the right HTTP response status
Code:
if curl -sS -I -L http://127.0.0.1 | grep -qE '^HTTP\/1.1 200 OK'
then
echo "WORKING"
else
echo "not working"
fi
The earlier bit about the hidden carriage return brings up an important point that any and all data downloaded (or otherwise inputted) from external sources, especially the net, should not be trusted until it is processed and either replaced or properly cleaned. perl, for example, has a taint mode to detect and tract the spread of such tainted data within a script. Sanitize and validate externally acquired data before using it.
###
#!/bin/bash
# Written by user@domain.tld
# Monitors an Apache server and emails the Admin if the
# server reports anything other than "Apache"
# http://bashscripts.org/forum/viewtopic.php?f=16&t=1522
# Last Edited on Thu Dec 29, 2011 - 10:11:40 PM EST
###
MAIL_ME="user@address.com"
MAIL_SUBJECT="Web_Server_needs_attention"
SERVER_DATE=$(TZ=PST8PDT date "+%a %b %d %I:%M:%S %p %Z")
LOCAL_DATE=$(date +%c)
webserver=$(curl -Is http://address.com | \grep -E '^Server' | cut -c9-21)
echo "$SERVER_DATE" - Web Server Check = "$webserver" >> ~/server.log
if [ "$webserver" = "Apache" ] ; then
exit 0
else
echo "My_Server_Server_Check - FAILED" on "$LOCAL_DATE" | mail "$MAIL_ME" -s "$MAIL_SUBJECT"
exit 1
fi
#EOF
Hope that helps. May even serve as a bad example. IDK.
Last edited by Habitual; 06-22-2016 at 07:17 AM.
Reason: s/"$WEB"/"$webserver"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.