
#!/usr/bin/perl

require "sys/socket.h";

while(<STDIN>) {
    if (/^([\d\.]+) /) {
        $ipaddr = $1;
    } else {
        next;
    }
    if(defined $hosts{$ipaddr}) {
        $fqdn = $hosts{$ipaddr};
    } else {
        ($fqdn, $aliases, $type, $len) =
            gethostbyaddr(pack("C4",split(/\./,$ipaddr)),&AF_INET) || next;
        $fqdn =~ tr/[A-Z]/[a-z]/;
        $hosts{$ipaddr} = $fqdn;
    }
    $name = "";
    foreach $component (reverse(split(/\./,$fqdn))) {
        $name = $component . '.' . $name;
        if(defined $uses{$name}) {
            $uses{$name}++;
        } else {
            $uses{$name} = 1;
            print STDERR "Started out $name\n";
        }
    }
}

foreach $entry (keys %uses) {
    push(@list, $uses{$entry} . " " . $entry);
}

# Thought about adding a sort here, but didn't bother...
# you'll have to use sort -n yourself.
foreach $entry (@list) {
    print $entry, "\n";
}

