#!/usr/bin/perl
#
# wl-ip - web server log requestor ip list
#
# List all requestor IPs in the web server log given on STDIN.  Sort them, and
# print all the unique addresses with any associated domain names.
#
# This and other hacks can be found at: http://oddgeek.info/
#
# Copyright (c) 2005 Jason A. Dour
#
# This software is provided 'as-is', without any express or implied warranty.
# In no event will the authors be held liable for any damages arising from the
# use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
#     1. The origin of this software must not be misrepresented; you must not
#     claim that you wrote the original software. If you use this software in a
#     product, an acknowledgment in the product documentation would be
#     appreciated but is not required.
#
#     2. Altered source versions must be plainly marked as such, and must not
#     be misrepresented as being the original software.
#
#     3. This notice may not be removed or altered from any source
#     distribution.
#

#
# Version Information
#
# 1.0	2005.06.02
#
# 	Tired of doing this on the command line.
#

# Required Modules
use Net::DNS;

# Loop over each line of input on STDIN...
while (<STDIN>) {
    # Parsing the web server log line...
    /^(\S+) (\S+) (\S+) \[(.+)\] \"(.+)\" (\S+) (\S+) \"(.+)\" \"(.+)\"/;
    # And shoving the referer into an array.
    push(@ipaddrs,$1);

}

$resolver = Net::DNS::Resolver->new;

# Loop over a sorted, unique-entries list...
$prev = undef;
foreach $refent ( grep($_ ne $prev && (($prev) = $_), sort @ipaddrs) ) {
    # Lookup DNS...
    $result = $resolver->search($refent);
    # Printing each IP to STDOUT...
    print "$refent ";
    # And if the dns came back with data...
    if ($result) {
	# Loop over the data returned by DNS...
	foreach $rr ($result->answer) {
	    # Skipping invalid...
	    next unless $rr->type eq "PTR";
	    # And printing the domain name for the IP.
	    print " - " . $rr->ptrdname;
	}
    }
    # Close the line and loop to next entry.
    print "\n";
}

# We're done.  Rawk!
exit(0);
