#!/usr/bin/perl -w

use warnings;
use strict;

# Linksys router address.
my $addr = '192.168.1.1';

# The username may not matter, but leave it alone if you don't know better.
my $user = 'admin';

# Your router password.
my $pass = 'XXXXX';

my $linksys = 0;
my $dlink = 0;
my $netgear = 1;

# don't touch below here.

my $date = `date`;
chomp($date);

print("# For loopbacking.\n");
print("127.0.0.1\tlocalhost\n\n");

print("# (This is us.)\n");
print("192.168.1.99\twickedsick\n\n");

if ($linksys) {
    my $htm = `/usr/local/bin/wget -q http://$user:$pass\@$addr/DHCPTable.htm ; cat DHCPTable.htm ; rm DHCPTable.htm`;

    print("# Addresses from Linksys router DHCP table at $date ... \n");
    while ($htm ne '') {
        last if not ($htm =~ s#.*?<td>(.*?)</td><td>(\d+\.\d+\.\d+\.\d+)</td>##);
        my $host = $1;
        my $ip = $2;
        $host =~ s/.*<td>(.*)\Z/$1/;  # skip HTML form shite.
        next if ($host eq '');   # no host name for this DHCP client.
        next if ($host eq 'wickedsick');   # no host name for this DHCP client.
        print("$ip\t$host\n");
    }
}

if ($dlink) {
    my $htm = `/usr/local/bin/wget -q http://$user:$pass\@$addr/h_dhcp.html ; cat h_dhcp.html ; rm h_dhcp.html`;
    print("# Addresses from D-Link DI-614+ router DHCP table at $date ... \n");
    while ($htm ne '') {
        last if not ($htm =~ s#.*?<tr bgcolor="\#FFFFFF"><td>(.*?)</td><td>(\d+\.\d+\.\d+\.\d+)</td>##);
        my $host = $1;
        my $ip = $2;
        next if ($host eq '');   # no host name for this DHCP client.
        next if ($host eq 'unknown');   # no host name for this DHCP client.
        next if ($host eq 'wickedsick');   # no host name for this DHCP client.
        print("$ip\t$host\n");
    }
}

if ($netgear) {
    my $htm = `/usr/local/bin/wget -q http://$user:$pass\@$addr/devices.html ; cat devices.html ; rm devices.html`;
    print("# Addresses from Netgear FVS318 router DHCP table at $date ... \n");
    #$htm =~ s/\r/\n/g; print "$htm\n";
    while ($htm ne '') {
        last if not ($htm =~ s#.*?<span class="ttext">(\d+\.\d+\.\d+\.\d+)</span></td><td width="40%" align="center"><span class="ttext">(.*?)</span></td><td width="37%" align="center"><span class="ttext">##);
        my $ip = $1;
        my $host = $2;
        next if ($host eq '');   # no host name for this DHCP client.
        next if ($host eq 'unknown');   # no host name for this DHCP client.
        next if ($host eq 'wickedsick');   # no host name for this DHCP client.
        print("$ip\t$host\n");
    }
}


print("\n");
print("# End of hosts ...\n\n");

exit 0;

