Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Simple WebServer Scanner

by Asmo (Monk)
on Sep 21, 2001 at 18:07 UTC ( #113876=sourcecode: print w/replies, xml ) Need Help??
Category: Networking Code
Author/Contact Info Asmo -
Description: This script prints out the webserver version the scanned host is running (with a simple HEAD request)

use Socket;
use Strict;
use warnings;

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

if ($ARGV[0] eq "") {

sub affichage
        print "\n\nUsage : $0 <remote host>\n";

$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: $!";
if($_ =~ /Server: (.*)/) {
        print $_;

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,
        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 ""); 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".


      "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".

        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";


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 | 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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://113876]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2022-08-14 09:01 GMT
Find Nodes?
    Voting Booth?

    No recent polls found