http://www.perlmonks.org?node_id=113876
Category: Networking Code
Author/Contact Info Asmo - asmo@mail.be
Description: This script prints out the webserver version the scanned host is running (with a simple HEAD request)
#!/usr/bin/perl

use Socket;
use Strict;
use warnings;

my $remote = $ARGV[0];
my $port=80;

if ($ARGV[0] eq "") {
affichage();
}

sub affichage
{
        print "\n\nUsage : $0 <remote host>\n";
exit();
}



$submit = "HEAD / HTTP/1.0\r\n\r\n";
if($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
die "no port specified" unless $port;
my $iaddr = inet_aton($remote) || die "$remote";
my $paddr = sockaddr_in($port, $iaddr) || die "Caca !!";
my $proto = getprotobyname('tcp') || die "protocol !!";

socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "cannot open socket 
+: $!";
connect(SOCK, $paddr) || die "cannot connect to $remote: $!";
send(SOCK,$submit,0);
while(<SOCK>)
{
if($_ =~ /Server: (.*)/) {
        print $_;
}

}
close(SOCK);
Replies are listed 'Best First'.
Re: Simple WebServer Scanner (New perler, why downvotes?)
by ybiC (Prior) on Sep 21, 2001 at 21:19 UTC
    It's sad to see young Asmo's reasonable effort at coding being downvoted.   Of course the Other Ways To Do It in this thread are beneficial, and doubtless better, but the downvotes aren't a terribly monkish way to encourage new Perlers to learn and grow.
        a bit less cheerful,
        Don
        striving toward Perl Adept
        (it's pronounced "why-bick")
      I agree completely. Have mercy o great monks!!! %^+^%
Re: Simple WebServer Scanner
by merlyn (Sage) on Sep 21, 2001 at 19:09 UTC
    Your perl4-style code ignores the advances made in Perl in the past 8 years. Socket code without "use IO::Socket", and HTTP code without touching the LWP library. Feh!
    use LWP; use HTTP::Request::Common; my $response = LWP::UserAgent->new-> simple_request(HEAD "http://perltraining.stonehenge.com"); unless ($response->is_error) { print "server: ", $response->server, "\n"; } else { print "error: ", $response->status_line, "\n"; }

    -- Randal L. Schwartz, Perl hacker

      You're right about LWP of course and your version is much nicer, but the original script does "use Socket".

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you don't talk about Perl club."

      I am not advocating writing perl4 code but not all programmers have the right of installing new modules on a given site. I don't know for this particular monk though.

      -- stefp

        I already covered this in "No excuses about not using CGI.pm".

        And hosting solutions are a dime-a-dozen these days. Not having basic tools like IO::Socket (which is part of the core distribution) or LWP available to you means you are burning a lot of time needlessly. Move to a different server.

        -- Randal L. Schwartz, Perl hacker

    A reply falls below the community's threshold of quality. You may see it by logging in.
(jeffa) Re: Simple WebServer Scanner
by jeffa (Bishop) on Sep 21, 2001 at 19:42 UTC
    Hmmmm . . . let's get the with times, eh? ;)
    use strict; use warnings; use LWP; my $url = shift || 'localhost'; $url = "http://$url"; my $agent = LWP::UserAgent->new(); my $request = HTTP::Request->new(HEAD => $url); my $response = $agent->request($request); print $response->{_headers}->{server}, "\n";

    jeffa

Re: Simple WebServer Scanner
by echo (Pilgrim) on Sep 21, 2001 at 19:32 UTC
    Writing perl code is always nice, but in this case it seems overkill:
    % perl -MCPAN -e 'install LWP' % HEAD http://www.example.com/ | grep ^Server
      perl -MCPAN -e 'install LWP' Can't locate object method "install" via package "LWP" at -e line 1.
        To work around this problem use: perl -MCPAN -e shell then at the prompt enter: install LWP Linuxboy