Using Perl LWP::UserAgent to View Server Headers

Unless configured to do otherwise, most HTTP and FTP servers will supply you with identifying information in the form of a server header. Using Perl's LWP::UserAgent, you can connect to a server and display the header information.

First, you'll need LWP::UserAgent. You can install this using Perl's CPAN repositories:

sudo perl -MCPAN -e 'install LWP::UserAgent'

After LWP::UserAgent is installed, you can use Perl to connect to the server and download the information. Here is an example script which takes one argument and displays the information:

#!/bin/env perl
 
use warnings;           # keep us warned
use strict;             # keep us honest
use LWP::UserAgent;     # for web requests
 
my $url;                # init url var
my $serverheader;       # init server header var
 
# require one argument
if ($#ARGV != 0) {
 
    printf("Usage: %s <URL>\n", $0);
    exit(1);
 
} else { $url = $ARGV[0]; }
 
# build connection properties
my $ua = LWP::UserAgent->new();
$ua->timeout(10);
$ua->agent('Mozilla/5.0');
 
# connect and get
my $response = $ua->get($url);
 
# if we can connect...
if ($response->is_success) {
 
    # grab server header
    $serverheader = $response->server;
 
    # if server header exists...
    if (defined($serverheader)) {
 
        # print server header
        printf("%s\n", $response->server);
 
    # else, print a message
    } else { printf("No server header available.\n"); }
 
# else, print connection status
} else { printf("%s\n", $response->status_line); }
 
exit(0);

Assuming that the code is saved as getinfo.pl and executable (chmod +x), you can use it to fetch interesting information from various HTTP and FTP servers.

bash$ ./getinfo.pl http://www.apache.org
Apache/2.3.15-dev (Unix) mod_ssl/2.3.15-dev OpenSSL/1.0.0c
 
bash$ ./getinfo.pl ftp://apache.mirrors.pair.com
apache.mirrors.pair.com NcFTPd Server (licensed copy)

Of course, some sites do not publish HTTP headers (but most do).

bash$ ./getinfo.pl http://www.facebook.com
No server header available.

Finally, the code will show you if there is an error:

bash$ ./getinfo.pl http://badurl
500 Can't connect to badurl:80 (Bad hostname 'badurl')