#!/usr/bin/perl
####################
#
# gethead.pl
#
# gets web server version... w0w... INET skillz...
#
# shoutz to:
#   -[ all the [geeks], and #31337 on altair, 
#      and to vim of course, the best editor in the world! ]-
#
# DmuZ@angrypacket.com
#
####################

##### MODS
# We need the IO::Socket Mod
use IO::Socket::INET;
# This is a great mod for grabbing CL args
use Getopt::Std;

##### VARS
$version="10242000";
# This is the string we send to the web server,
# for some reason two \n gets it to pop back what we want
$stinkstring = "here comes an angrypacket!\n\n";

##### SUBROUTINES
sub INTRO {
        &GETVARS;
        print "\nGetHead.pl version $version by DmuZ\n\n";
        }

sub GETVARS {
        getopt(hp);
        # Check $host, run &USAGE if not there
        if ($opt_h) {
                $host = "$opt_h";
        } else {
                &USAGE;
                }
        # Check for port, or set default 80
        if ($opt_p) {
                $port = "$opt_p";
        } else {
                $port = "80";
                }
        }

sub USAGE {
        print "\n$0 -h hostname -p port\n\n";
        exit;
        }

##### MAIN - GO GET THE FUNGUS MANG!!!!!
&INTRO();

# Initiate our socket
$smelly_sock = new IO::Socket::INET (
                PeerAddr => $host,
                PeerPort => $port,
                Proto => 'tcp');
# If we did not connect no point in going on
die $! unless $smelly_sock;
#print "\n\nSucces! Our smelly sock(et) is connected to $host!\n\n";

# Send our string to the web server
print $smelly_sock $stinkstring;

# Grab the web servers reply into var $fungus, up to a 1000 bytes
read $smelly_sock, $fungus, 1000;

# Split fungus up, so each newline is a seperate element in our array
# This is so we can grab the info we want, and toss the rest
@datar = split(/\n/, $fungus);

# Check all the lines we got from the web server
# We only want ones that contain the string "Server"
foreach $i (@datar) {
        $toejam = $i if ($i =~ /Server/);
        }

# This ugly little bit gets rid of the html tags around our data
$_ = $toejam;
s/<ADDRESS>*//;
s/<\/ADDRESS>//;
$toejam = $_;

# Print out whatever we got
print "----------------------------------------------------\n";
print "The web server is running: \n$toejam\n";
print "----------------------------------------------------\n\n";

# Make sure we clean up after our selves!!!
close $smelly_sock;


