#!/usr/bin/perl 

# Script-name: hostcheck.pl
#
# By Simon Carr... Inspired by code in the Perl Cookbook (O'Reilly Publishing)
# 
# Used in the good old days when I'd have to check maybe 30 VirtualHost entries
# a day on remote systems.  Further simplified a simple troubleshooting 
# method that's fairly tedious to do by hand.
#

use Getopt::Std;
use IO::Socket; 

# -d = host
# -f = filepath
# -p = port
# -v = virtualhost
# -a = show all page content

getopt("dpvfa");

$remote_host = $opt_d; 
$remote_port = $opt_p;
$virtual_host = $opt_v;
$file_path = $opt_f;

if ($remote_port eq ""){
  $remote_port = "80";
}

if ($file_path eq ""){
 $file_path = "/";
}

if ($virtual_host eq ""){
  $virtual_host = $remote_host;
}

if ($opt_d ne "") {
  
  print "\nh-",$remote_host;
  print ":",$remote_port;
  print " v-",$virtual_host;
  print " p-",$file_path,"\n";
  print "\n";
  print "GET $file_path HTTP/1.1\nhost: $virtual_host\n";
  
  
  $socket = IO::Socket::INET->new(
				  PeerAddr => $remote_host,
				  PeerPort => $remote_port,
				  Proto => "tcp",
				  Type => SOCK_STREAM
				 )
    or die "Can't connect to $remote_host:$remote_port : $@\n";
  
  print $socket "GET $file_path HTTP/1.1\nhost: $virtual_host\n\n\n";

  @answer = <$socket>;
 
  $x=0;

  print $opt_a;

  print "- ",$#answer," lines -\n";

  if ($#answer >= 15 && $opt_a == 1) {
    $y = $#answer+1;
  } elsif ($#answer <= 15 && $opt_a != 1) {
    $y = $#answer;
  } else { $y = 16; }
  
  for ($x=0 ; $x <= $y ; $x++) {
    $tempvar = $answer[$x]; 
    print $x,"\t",$tempvar;
  }
  
  close($socket);
  exit(0);
  
} else {
  print "\n",'Usage: hostcheck.pl -d host.to.check -f /filepath -p port -v virtual.host.name [-a]',"\n\n";
}

exit (1);
