[TAG] 2 cent tip on checking HTTP servers

Thomas Bonham thomasbonham at bonhamlinux.org
Sun Jun 15 07:20:42 MSD 2008


Hi All,

I will share the socket code that I wrote for checking to see if your 
http server is running. This will need to be run from a remote computer.

There will be something that you may need to change some things around 
for your needs and at this time it will not work with https it is only 
for http.

Here is the code I have put a few notes in where you may need to change 
for your needs.

#!/usr/bin/perl
use Socket;
$host = $ARGV[0];
$port = $ARGV[1];
$iaddr = inet_aton($host) || die "Unable to determine address for $host";
$paddr = sockaddr_in($port, $iaddr);
$proto = getprotobyname('tcp');
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) ||
    return "Unable to create a new socket: $!";
connect(SOCKET, $paddr) || die "Connection refused by $host: $!";
select(SOCKET);
$| = 1;
select(stdout);
print SOCKET "GET / HTTP/1.1\n";
print SOCKET "Host: $host\n";
print SOCKET "Connection: close\n";
print SOCKET "\n";
my $i = 0;
while (<SOCKET>) {
    if($i < 1) {
        # May need to change to match what header you would like back
        if($_ !~/^HTTP\/1.1 200 OK/) {
            # Do something here
        }
        break;
    }
    $i++;
}

Thomas




More information about the TAG mailing list