Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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 $_;


Comment on Simple WebServer Scanner
Download Code
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

      sometimes you can't install new modules...
        sometimes you can't install new modules...

        But your script here isn't a CGI script, it is run from the command line. So, if you can install this script, you can also install a module in your home directory.

        sometimes you can't install new modules...

        So I guessed. Sometimes you just have to make the best of a bad situation (here LWP not installable for whatever stupid probably managerial reason). This is also what perl is about. It is a duct tape which can be made to fit in ackward places. Sometimes you may even be obliged to use it stealthily if you want to be able to use it at all. It is ugly but perharps it made your day.

        So I don't understand why merlyn is scornful. You don't always have the choice of using libraries. On the other hand, Asmo, you have a lot of to learn anyway from merlyn.

        -- stefp

(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

Back to Code Catacombs

Log In?

What's my password?
Create A New User
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 romping around the Monastery: (3)
As of 2015-10-10 00:48 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (254 votes), past polls