Register
It is currently Tue Jul 29, 2014 4:53 pm

[SOLVED]-basic IIS server check script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Thu Dec 29, 2011 4:08 pm   
User avatar

Joined: Fri Jul 22, 2011 5:39 pm
Posts: 46
If this is off-topic please feel free to move it.

My goal was to determine if the server is up using curl to ask for the Webserver Software version.
It should be and is "Microsoft-IIS"
Using "set -x" in my script showed me it may be "Microsoft-IIS"

Code:
#!/bin/bash
# set -x
MAIL_ME="jj"
MAIL_SUBJECT="Norveld_Server_needs_attention"
SERVER_DATE=$(TZ=PST8PDT date +%c)
LOCAL_DATE=$(date +%c)

# store this in a variable
temp=$(curl -Is http://xxx.xxx.xxx.xxx | \grep -E '^Server' | cut -c9-21)

echo $(TZ=PST8PDT date) - Web Server Check = $temp >> /home/jj/Documents/cirrhus9/Accounts/Norveld/3WeekTest/norveld.test

if [ $temp = "Microsoft-IIS/7.5\r" ] ; then
echo "Norveld_Server_Check - FAILED" on $LOCAL_DATE | mail $MAIL_ME -s $MAIL_SUBJECT
exit 0
fi



The source of my current amusement is what appears to be the backwards logic in my "if" statement.
But trust me, in this case, it does opposite what it should?

Pseudo-code:
Ask server for version
If the string IS Microsoft-IIS/7.5\r ; then
write log file
exit 0
else
If the string is NOT Microsoft-IIS ; then
write log file
mail me

but if I change the IP to an Apache server, I don't get an email...
Code:
$ /home/jj/Documents/cirrhus9/Accounts/Norveld/3WeekTest/norveldchk.sh && mail
Thu Dec 29 13:57:23 PST 2011 - Web Server Check = Microsoft-IIS/7.5
No mail for jj

$ /home/jj/Documents/cirrhus9/Accounts/Norveld/3WeekTest/norveldchk.sh && mail
Thu Dec 29 13:57:33 PST 2011 - Web Server Check = Apache
No mail for jj


Any light to be shed on this would greatly be appreciated!
Or some real keyboard KungFu...
Ping is NOT an option for this script since the server will ping,
but the machine is in fact, unresponsive through normal channels.

Thanks and Happy New Year!


Last edited by Habitual on Sat Mar 17, 2012 5:47 pm, edited 4 times in total.

Top
 Profile WWW  
 PostPosted: Thu Dec 29, 2011 4:49 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 533
hi,

first don't forget to quote variables, it may break test if variable contains an IFS char.

there are many ways to test content\r
try this:
Code:
if [ "$temp" = "Microsoft-IIS/7.5"$'\r' ]

Quote:
Using "set -x" in my script showed me it may be "Microsoft-IIS/7.5\r"
do you mean \r is not there all the time?


Top
 Profile  
 PostPosted: Thu Dec 29, 2011 5:27 pm   
User avatar

Joined: Fri Jul 22, 2011 5:39 pm
Posts: 46
it was curious to me when I saw it...

curl -Is http://xxx.xxx.xxx.xxx| \grep -E '^Server' | cut -c9- spits out
"Microsoft-IIS/7.5"

but with "set -x" in the script, it shows it as "Microsoft-IIS/7.5\r"

variable $temp now quoted in if statement.

Thank you for your help.


Top
 Profile WWW  
 PostPosted: Thu Dec 29, 2011 8:14 pm   
User avatar

Joined: Fri Jul 22, 2011 5:39 pm
Posts: 46
another method, maybe?

Code:
#!/bin/bash
# http://bashscripts.org/forum/viewtopic.php?f=16&t=1522
# set -x
MAIL_ME="jj"
MAIL_SUBJECT="Norveld_Server_needs_attention"
SERVER_DATE=$(TZ=PST8PDT date +%c)
LOCAL_DATE=$(date +%c)

# store this in a variable
IIS=$(curl -Is http://xxx.xxx.xxx.xxx | \grep -E '^Server' | cut -c9-21)
echo $(TZ=PST8PDT date) - Web Server Check = "$IIS" # >> /home/jj/Documents/cirrhus9/Accounts/Norveld/3WeekTest/norveld.test

if [ "$IIS" = "Microsoft-IIS" ] ; then
exit 0
else
echo "Norveld_Server_Check - FAILED" on "$LOCAL_DATE" | mail "$MAIL_ME" -s "$MAIL_SUBJECT"
exit 1
fi


the rubber-hits-the-road at
Code:
curl -Is http://xxx.xxx.xxx.xxx | \grep -E '^Server' | cut -c9-21
which leaves off the "/7.5" of the string "Microsoft-IIS/7.5"
NOW I can compare "$IIS" to "Microsoft-IIS"...?

fingers are crossed.

Thanks.


Top
 Profile WWW  
 PostPosted: Fri Dec 30, 2011 10:24 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 533
Code:
website="microsoft.com"
serverType=$(curl -Is "$website" |sed -n 's/^Server: \([^/]*\)\/.*/\1/p')

if [ "$serverType" = "Microsoft-IIS" ]
then
     echo yes
else
     echo no
fi
if you don't need serverType further, you can test directly
Code:
if [ "$(curl -Is "$website" |sed -n 's/^Server: \([^/]*\)\/.*/\1/p')" = "Microsoft-IIS" ]


Top
 Profile  
 PostPosted: Fri Dec 30, 2011 11:45 am   
User avatar

Joined: Fri Jul 22, 2011 5:39 pm
Posts: 46
Watael:

Thanks for the options. I love 'em.
for now, all seems to be well using
"curl -Is "$website" | \grep -E '^Server' | cut -c9-21"

I tested my script manually 103 times yesterday and finally settled on the current script.

HNY - 1d


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP