#!/usr/bin/perl # DNSLookup.pl -- Lookup IPAs and get hostnames $ver = "1.0"; # 2002-11-22 JPV ########################################################################## (($myname = $0) =~ s/^.*(\/|\\)|\..*$//ig); # remove up to last "\" or "/" and after any "." $Greeting = ("$myname $ver Copyright 2002 JP Vossen (http://www.jpsdomain.org/)\n"); $Greeting .= (" Licensed under the GNU GENERAL PUBLIC LICENSE:\n"); $Greeting .= (" See http://www.gnu.org/copyleft/gpl.html for full text and details.\n"); # Version and copyright info if (("@ARGV" =~ /\?/) || ("@ARGV" =~ /-h/) || ("@ARGV" =~ /--help/)) { # Need help? print STDERR ("\n$Greeting\n\tUsage: $myname [-i {infile}] [-o {outfile}] [-q]\n"); print STDERR ("\n\tLookup IP Addresses and provide hostnames.\n"); print STDERR ("\n\t-i {infile} = Use infile as the input file, otherwise use STDIN.\n"); print STDERR ("\t-o {outfile} = Use outfile as the output file, otherwise use STDOUT.\n"); print STDERR ("\t-q = Only display the answer (IPA(s)\tHostname(s)).\n"); print STDERR ("\t-Q = Only display the Hostname(s).\n"); print STDERR ("\n\nIP Addresses may be embedded in text, they will be stripped and used anyway.\n"); die ("\n"); } use Socket; use Getopt::Std; # Use Perl5 built-in program argument handler getopts('i:o:qQ'); # Define possible args. if (! $opt_i) { $opt_i = "-"; } # If no input file specified, use STDIN if (! $opt_o) { $opt_o = "-"; } # If no output file specified, use STDOUT open (INFILE, "$opt_i") || die "$myname: error opening $opt_i $!\n"; open (OUTFILE, ">$opt_o") || die "$myname: error opening $opt_o $!\n"; if (! ($opt_q or $opt_Q)) { print STDERR ("\n$Greeting\n"); } while () { chomp; @ips = m/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/gs; foreach $ipa (@ips) { $src_ip_packed = inet_aton($ipa); # Get the packed format needed $hst_name = gethostbyaddr($src_ip_packed, AF_INET); # Look it up if (! $hst_name) { $hst_name = "Unresolvable"; } # If we can't find it, mark it as unresolvable if ($opt_Q) { print OUTFILE ("$hst_name\n"); # Print JUST the hostname(s) } else { print OUTFILE ("$ipa\t$hst_name\n"); # Print the IPA(s) and the Hostname(s) } } } if (! ($opt_q or $opt_Q)) { print STDERR ("\n\a$myname finished in ",time()-$^T," seconds.\n"); }