#!/usr/bin/perl -w

=begin COPYRIGHT

jdresolve-unresolved - Scans log file for unresolved hosts and lists them

Copyright (c) 1999 John Douglas Rowell

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

=end COPYRIGHT
=cut

use strict;
use Getopt::Long;

my $APPNAME = "jdresolve-unresolved";
my $VERSION = "0.5.2";
my $AUTHOR = "John Douglas Rowell";
my $YEAR = 1999;
my $BUGS = "bugs\@jdrowell.com";

my %opts = ( stdin => 0, help => 0, version => 0 );

my %optctl = ( "" => \$opts{stdin}, 
	"h" => \$opts{help},
	"help" => \$opts{help},
	"v" => \$opts{version},
	"version" => \$opts{version} );
my @optlst = ("", "h", "help", "v", "version");

my $usage =<<"-x-";
Usage: $APPNAME [-hv] [--help] [--version] <LOG FILE>

Report bugs to $BUGS
-x-

my $version =<<"-x-";
$APPNAME $VERSION
Copyright (C) $YEAR $AUTHOR
$APPNAME comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of $APPNAME
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.
-x-

GetOptions(\%optctl, @optlst);

if ($opts{help}) {
	print <<"-x-"; exit;
$version
$usage
Scans log file for unresolved hosts and lists them.

   --help or -h
      help (this text).
   --version or -v
      display version information.
   <LOG FILE>
      the log filename or '-' for STDIN
-x-
}

if ($opts{version}) {
	print <<"-x-"; exit;
$version
try $APPNAME --help for help
-x-
}

my $ipmask = '^(\d+)\.(\d+)\.(\d+)\.(\d+)';
my $filename = $opts{stdin} ? '-' : $ARGV[0];
my %ips;

$filename ne '-' and !-f $filename and die "can't find file '$filename'";
open FILE, $filename or die "error trying to open file '$filename'";

while (<FILE>) {
	!/$ipmask\s/ and next;
	my $ip = "$1.$2.$3.$4";
	exists $ips{$ip} and next;

	$ips{$ip} = 1;
	print "$ip\n";
}

