#!/usr/bin/perl -w # clf_lookup.plx # given common or extended-format Web logs on STDIN, outputs # them with numeric IP addresses in the first (host) field converted # to hostnames (where possible). use strict; use Socket; my %hostname; while (<>) { my $line = $_; my($host, $rest) = split / /, $line, 2; if ($host =~ /^\d+\.\d+\.\d+\.\d+$/) { # looks vaguely like an IP address unless (exists $hostname{$host}) { # no key, so haven't processed this IP before $hostname{$host} = gethostbyaddr(inet_aton($host), AF_INET); } if ($hostname{$host}) { # only processes IPs with successful lookups $line = "$hostname{$host} $rest"; } } print $line; }