Web page with online hosts of the neighbourhood

The following perl script uses nmap to discover online hosts in the neighbourhood and creates a static html page. The script works with all Linux distributions. Mods should be performed to work with Windows.

  • Requirements: perl, nmap, web server (optional)
  • Hostname file. The script uses a text file to read the corresponding hostnames of known IP addresses. It assumes that the text file has the following format:



Each line corresponds to an IP address in ascending order. For example the third line corresponds to Create your own file with your known hostnames.


use strict;
use warnings;

my $text = qx(nmap -sn; # run an external command and store the output
my @ips = ();
my @host_list = ();

# fetch all the local IPs (i.e. 192.168.x.x)
foreach my $line (split /[\r\n]+/, $text){
        if ($line =~ "192.168"){
                my @words = split(/ /, $line);
                push (@ips, $words[4]);

# read the hostnames from an external text file
open (MYFILE, "/var/www/host_list.txt"); # change this according to your own configuration
while (<MYFILE>) {
        next if (/^#/);
        push (@host_list, $_);
close (MYFILE);

my $export_file = "/var/www/html/whoisonline.html"; # change this according to your own configuration
open(my $fh, '>', $export_file);
print $fh "<html>\n<head>\n";
print $fh "<meta http-equiv='refresh' content='120'>\n";
print $fh "<meta charset='UTF-8'>\n<title>Who is online</title>\n</head>\n<body bgcolor=\#A3C3E0>\n";
printf $fh "<h4>Online hosts: %d</h4>\n", scalar @ips;
print $fh "<table border='1'>\n";

my $aa = 0;
print $fh "<tr>\n<td width='40' align='center'><b>\#</b></td><td width='130' align='center'><b>IP</b></td><td align='center'><b>hostname</b></td>\n</tr>\n";
foreach my $ip (@ips){
        my @decs = split(/\./, $ip);
        my $last_dec = $decs[3];
        my $host = 0;
        if ($last_dec > (scalar @host_list)){
                $host = "unknown"; # consider unknown the hosts that are not included in the hosts file
                $host = $host_list[$last_dec-1];
        print $fh "<td align='center'>$aa</td><td>$ip</td><td>$host</td>\n</tr>\n";
print $fh "</table>\n";
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
($min, $sec) = format_time($min, $sec);
$mon += 1;
$year += 1900;
print $fh "<br><font size='2'>Last update: $hour:$min:$sec $mday/$mon/$year</font><br>\n";
print $fh "</body>\n</html>\n";
close $fh;

# a sub to correctly display time
sub format_time{
        my $min = shift;
        my $sec = shift;
        if ($min < 10){
                if ($sec < 10){
                        $sec = "0$sec";
                        $min = "0$min";
                        $min = "0$min";
                if ($sec < 10){
                        $sec = "0$sec";
        return ($min, $sec);
  • You can put your script in crontab to run it periodically. Type "crontab -e" and add the following line according to your configuration. The STDERR is redirected to "errors".
*/2 * * * * perl /var/www/whoisonline.pl 2>> /var/www/html/errors
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License