How do I check using shell-script if a website is available / responding?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I check using shell-script if a website is available / responding?
# 1  
Old 07-22-2008
Question How do I check using shell-script if a website is available / responding?

Hi,

Could someone please help.

How do I verify using a shell script whether a website URL is available? It's roughly the URL equivalent of ping <servername> or tnsping <Oracle database name>?

I hope this is enough information - please let me know if it's not.

Many thanks,

Neil
# 2  
Old 07-22-2008
Use wget for example and check it's exit code which you can test while echoing $? (if it's !=0 there is probably some problem) or it's text output.
# 3  
Old 07-22-2008
Thank-you very much indeed zaxxon. Exactly what I was after.
# 4  
Old 07-22-2008
Here is what I use, will send you an email if server is down:

On Webserver:

mystatus.cgi:
Code:
#!/usr/bin/perl
# authorized address the ip of the server getting accessing the script
$authaddress = "192.168.0.10";

# end configuration

print "Content-type: text/html\n\n";

if ($ENV{'REMOTE_ADDR'} eq "$authaddress") {

        print "I AM WORKING!";

        $uptime = open( UPTIME, "/usr/bin/uptime |" );
        while (<UPTIME>){print;}
        close( UPTIME );

}
else {print "unauthorized access";}
exit;


on other server run from cron:

servermonitor.pl:

Code:
#!/usr/bin/perl

# setup array of servers/websites to check
@sitestocheck = ('mynetworks.com','mywebsite2.com');

# the relative url of the website response script in each site
$responseprogram = "/cgi-bin/mystatus.cgi";

# path to the log file with the response data
$statusdir = "/var/log"; # needs to be 0777 web writeable

# mail feature
$mailprog ='/usr/sbin/sendmail';
$adminmail = 'me@home.com';
$frommail = 'websitemonitor@home.com';

###############################################################
# End Configuration
###############################################################

# main program
use LWP::UserAgent;


# now check each url in your array
foreach $sitetocheck (@sitestocheck){

$ua = new LWP::UserAgent;
$req = new HTTP::Request 'GET',"http://$sitetocheck$responseprogram";
$res = $ua->request($req);

if ($res->is_success) {

if ($res->content =~ /I AM WORKING/i){$response = "SERVER OK:$sitetocheck:".$res->content;}
        else {$response = "WARNING! There appears to be a problem, FIX ME!<br>\n";}
        }
        else {

        $timestamp = localtime;
        $response = "WARNING! UNABLE TO CONNECT TO $sitetocheck at $timestamp";

        $traceroute = `/usr/sbin/traceroute $sitetocheck`;
        $traceroute = "";

        }



        # write server status to the main log file
        open(FILE,">>$statusdir/statuslog.txt");
        flock(FILE, 2);
        print FILE "$response\n$traceroute\n\n";
        flock(FILE, 8);
        chmod 0777, $statusfile; # keep the file writeable

        # write to a current status file for each server or website being monitored
        open(FILE,">$statusdir/$sitetocheck");
        flock(FILE, 2);
        print FILE $response;
        flock(FILE, 8);
        chmod 0777, $statusfile; # keep the file writeable

}

        # if there is an error mail the administrator
if ($response =~ /WARNING/i){

open( MAIL, "|$mailprog -t" );

print MAIL "Subject: Server or Website Not Responding\n";
print MAIL "From: $frommail\n";
print MAIL "To: $adminmail\n";
print MAIL "Reply-to: $frommail\n\n";

      print MAIL "$response\n$traceroute";
      print MAIL "\n\n";        
      close MAIL;

}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash shell script to check if script itself is running

hi guys we've had nagios spewing false alarm (for the umpteenth time) and finally the customer had enough so they're starting to question nagios. we had the check interval increased from 5 minutes to 2 minutes, but that's just temporary solution. I'm thinking of implementing a script on the... (8 Replies)
Discussion started by: hedkandi
8 Replies

2. Web Development

Request to check:PHP website design help

Hi I have a website name www.gentrepid.org I have all the setting scripts for this website in php now as a research part, I am new to this as I havent done that before. I have to make certain changes in the website Include some icons on the left like "Drugs" when user click on it... (0 Replies)
Discussion started by: manigrover
0 Replies

3. Infrastructure Monitoring

Nagios Check Website Command help

Hi all, me again.... I am trying to add a website to my nagios checking juggernaught I am using the script from nagios exchange site called check_website_response (google to find it i am not allowed to post links yet, sorry) It is in /usr/local/nagios/libexec with the rest of the default... (1 Reply)
Discussion started by: Yoshi17
1 Replies

4. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

5. Shell Programming and Scripting

Shell Script for Logging into the Website

Hi ALL, Is there any way, to login into a website using Shell/Perl command/script? I am struggling on this from quite sometime but with no luck. Can you guys help, please? My sole purpose is to login a website (Which requires Username and Password) and then extract some information from... (3 Replies)
Discussion started by: parshant_bvcoe
3 Replies

6. Shell Programming and Scripting

IP check with shell script

hi guys ..newbie here i would like to create a simple script tat will detect wether the machine has IP or not ... but it seems that my script doesnt really work it kept rebooting... i set this script during boot so that it will check else it will reboot it a shell script thou... ... (5 Replies)
Discussion started by: bladez
5 Replies

7. UNIX for Dummies Questions & Answers

Shell not responding

Hi all (Shell: ksh Unix: NCR Unix) Upon opening a shell, I can type a command (any command), but the shell does very little or nothing. If I cd to an unknown directory, the shell responds with "file not found", if I issue 'ls', I get no directory listing (files are present). If I issue... (2 Replies)
Discussion started by: jgrogan
2 Replies

8. Shell Programming and Scripting

HELP: check if website is on, if not email

if {ping -c 1 www || { "Destination Host Unreachable" ; }} then { echo "neveikia senas-pastas, web serveris" | mailx -s "Senas web serveris" mail } endif; this is my script but it does not work... :confused: this script will be used to check if website is online if not then sends an... (7 Replies)
Discussion started by: big_nobody
7 Replies

9. Shell Programming and Scripting

check my first shell script

I have written the below script to determine whether a string is palindrome or not ? But its not working, any help to debug it ? I am new to this forum but when I searched for my question, I found that many people refused to answer this question thinking that its Homework question, Therefore I... (2 Replies)
Discussion started by: gridview
2 Replies

10. Shell Programming and Scripting

check in unix shell script so that no one is able to run the script manually

I want to create an automated script which is called by another maually executed script. The condition is that the no one should be able to manually execute the automated script. The automated script can be on the same machine or it can be on a remote machine. Can any one suggest a check in the... (1 Reply)
Discussion started by: adi_bang76
1 Replies
Login or Register to Ask a Question