RayRay459 has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to run script that will lookup all of the ip's in a document and replace them with their name from a dns server. I know you can script to get dns names. Any help/insight would be greatly appreciated. ~Ray~

Replies are listed 'Best First'.
Re: resolving ip's to names in a text file
by lshatzer (Friar) on Jul 20, 2001 at 03:22 UTC
      I am not sure if Net::Whois is really what fits the case if he just wants to resolve an ip address to a dns entry. I think that Net::DNS::RR::PTR is more suitable.

      humbly -c

        I'm not sure of your input file but this should get you started in the right direction, if not maybe someone can learn a tip of two. HAVE FUN!
        #!/usr/bin/perl-w use strict; use win32; my $in_name=$ARGV[0]; my $out_name=$ARGV[1]; our (@x); # new type of global declaraton # carefull commas needed I.E. our (@x, @y); # use vars qw ( @x @y ); <- obsolete global declaration # pass a reminder if($#ARGV < 1) { die "usage: ip.pl <input file> <output file>\n" . "for example: program.pl infile.txt outfile.txt\n" } open(FILE, $in_name) || die "ip.pl can't open $in_name: $!"; open(IPFILE,">$out_name") || die "ip.pl can't open $out_name: $!"; my $dns2="toto"; #157.173.20.2 my $dns3="oz"; #157.173.20.3 my $dns4="wizard"; #157.173.20.4 my $ip2; my $ip3; my $ip4; while (<FILE>){ # sample line # Tz25,D,wizard,2,157.173.20.2,Local @x=split(/,/); next unless (($_ =~ /Local/)||($_ =~ /Network/)); if ($x[4] =~ /157.173.20.2/){ $ip2 = $dns2; print $x[0], $x[1], $x[2], $ip2; print IPFILE; } }
        That is exactly right. After doing a lookup on Net::Whois...i saw that that wasn't what i needed. I need to have it lookup the ip addresses in our local dns server. Thank you. ~Ray~
      forgive me for sounding like a moron, but i am faily new to perl. How would i encorporate it into my script...the regex at least? ~Ray~
        Taking the regex out of RE: Matching an IP address, which is from the MRE book, with a few small modifications, came up with this.
        my @ips = ($string =~ m/((?:[01]?\d\d?|2[0-4]\d|25[0-5])\.(?:[01]?\d\d +?|2[0-4]\d|25[0-5])\.(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.(?:[01]?\d\d?|2 +[0-4]\d|25[0-5]))/g);
        Now @ips will have an array full of IP's, if they are valid or not is another discussion...
Re: resolving ip's to names in a text file
by Anonymous Monk on Jul 20, 2001 at 17:31 UTC
    I don't know if this is what you mean, but it's worked for me a few times. This works on SunOS very well (the foratting may be different based on the OS -- im not much of a networking guy :) )

    #!/usr/bin/perl -w $ipaddressfile = "FILENAME-TO-PLAINTEXT-IPADDRESS-FILE"; @iplist = ""; $i = 0; open(IPLISTFILE,"<$ipaddressfile"); while(<IPLISTFILE>) { $iplist[$i] = $_; $i++; } close(IPLISTFILE)l foreach $ipaddr (@iplist) { @ns = `nslookup $ipaddr`; $ns[3] =~ m%.* (.*)%; print $ipaddr . " resolves to " . $1 . "\n"; }
      I'll see if i can get this to work for me as well. Thank you anonymous monk. ~Ray~
Re: resolving ip's to names in a text file
by Malkavian (Friar) on Jul 20, 2001 at 16:17 UTC
    Another question is, do you need to resolve the IP addresses within Perl?
    If you're doing a log analysis, and need to resolve the addresses within that log, there are a veritable cornucopia of tools out there to achieve that.
    One that I use, dns-terror (now called 'fastresolve') is screamingly fast, and beats every Perl solution I've yet seen.
    The caveat with that, is that it loads the whole DNS cache file into memory.. I've seen it chew up most of the available memory on a logging host, and bring it to it's knees.. However, that was with millions of IP addresses in cache..
    Another pre-resolver is jdresolve, which, though slower than fastresolve is extremely fast, and makes use of the ADNS Perl modules. Perhaps, downloading the source for jdresolve, and reading it, or using it as a pre-processor would help.
    For doing many processes on a file, I find that it's often better to use dedicated tools as filters, and pipe the data through them. In many cases, I've gained huge performace benefits from this, along with the ability to easily add more functionality by adding an extra filter.
    To achieve this while in a Perl script, the open function with the pipe from program output to script (open(HANDLE,"program|");) would work a treat, taking resolved data from the original file, and giving you a handle on it.

    HTH

    Malk
      I don't NEED to resolve the IP address within Perl, i just thought that it might be the easiest way for me to get the job done. I will lookup "fastresolve", jdresolve" as well as hack around with the scripts that some of the guys posted below. thank you for your help. Thank you All! ~Ray~