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

List,

I am looking for a simple perl script to convert a text list of IP addresses to names by querying the local DNS server

Quote “Many servers produce log files containing IP addresses, but hostnames are more useful to analysis software and humans”

I’d like the script to read a text file list of hostnames, and output a CSV or XLS files of names and resolved IP addresses.

I’d be very grateful, if someone one on this list has such a script and would be happy to share that information, would post it here.

  • Comment on perl to convert text list of ip address to a name

Replies are listed 'Best First'.
Re: perl to convert text list of ip address to a name
by saskaqueer (Friar) on May 17, 2004 at 20:41 UTC
    #!/usr/bin/perl -w use strict; use Socket; while ( <DATA> ) { chomp; print +( gethostbyaddr( inet_aton($_), AF_INET ) )[0], $/; } __DATA__ 66.39.54.27 209.197.123.153 216.109.127.30 205.188.244.138
      Edit: I'm smoking crack. Sorry saskaqueer. Old thread is shown below ...

      I think you have it backwards, the original poster asked for a way to convert IP addresses to hostnames, not hostnames to IP addresses.

      Here's some example code using Net::DNS:

      use Net::DNS; my @domains = qw/google.com perl.com perlmonks.org cpan.org/; my $res = Net::DNS::Resolver->new; foreach my $domain( @domains ) { my $query = $res->search($domain); if ($query) { foreach my $rr ($query->answer) { next unless $rr->type eq "A"; print $rr->address, "\n"; } } else { warn "query failed: ", $res->errorstring, "\n"; } }
      Be forewarned: Net::DNS is a bit slow, but it does get the job done. Printing a CSV list of the domains and their associated IP addresses is an exercise left to the reader.
        I think you have it backwards, the original poster asked for a way to convert IP addresses to hostnames, not hostnames to IP addresses.

        What do you think my code does? It reads in a list of IP addresses and converts them to hostnames. Your code on the other hand converts hostnames into (possibly multiple) IP addresses, which is what your quote above says is what the OP did not ask for... You're not making sense. Your text and your code say two different stories :)

Re: perl to convert text list of ip address to a name
by NetWallah (Canon) on May 17, 2004 at 23:41 UTC
    Here is the code I use for IP to HostName:
    use Socket; # For Binary-> IP dot quad etc... use Net::Whois::IANA; # For IP -> Name # Get IP addr # Call the sub below, and print value sub Get_Whois_Name { my $ip = shift; my $Save_ = $_; # The WhoIs module messes with $_ (BUG!!) # Save current value for restore upon exit. ##print "LOOKING FOR $ip ===="; my $iana = new Net::Whois::IANA; $iana->whois_query(-ip=>$ip); my $name = $iana->descr(); if ($name =~m/not allocated/){ $name = "Country=" .$iana->country() ."+Net=" . $iana->netname(); } #Zap commas in name $name =~s/\,/ /g; $_ = $Save_; # Restore return $name; } ##########################################

    Offense, like beauty, is in the eye of the beholder, and a fantasy.
    By guaranteeing freedom of expression, the First Amendment also guarntees offense.
      Why get rid of commas?
        They asked for a Comma Separated List. The author just took the quick route of removing commas to make a "clean" name. You could also find if there is a comma in the name and enclose the complete name in double quotes.
Re: perl to convert text list of ip address to a name
by Anonymous Monk on May 18, 2004 at 02:59 UTC

    this comes from either the flow-tools or the cflow distribution, forget which. it's rarely failed me. it has the benefit of cacheing, flexible output. it's just plain cool.

    #!/usr/usc/perl/5.6.1/bin/perl # ip2hostname - a filter to turn IP addresses into host names wherever + possible. # $Id: ip2hostname,v 1.8 2001/02/13 04:42:20 plonka Exp $ # Dave Plonka <plonka@doit.wisc.edu> use FindBin; use Socket; use Getopt::Std; sub usage { my $status = shift; print STDERR <<_EOF_ usage: $FindBin::Script [-h] [ -p printf_format ] [ [-i extension] fil +e [...] ] -h - help (shows this usage information) (mnemonic: 'h'elp) -p printf_format - use this printf format for IP address and ho +stname, respectively. The default format is '%.0s%s', which supresses the printing of the IP address (i.e. "%.0s" specifies printing a string with a maximum width of zero). To maintain column widths (since both the IP address and hostname vary in length), a format like this may be useful: '%-15.15s %-20s' (mnemonic: 'p'rintf format) -i extension - edit the files in place (rather than sending to standard output) This option requires file name(s) argument(s). The extension is added to the name of the old file to make a backup copy. If you don't wish to make a backup, use "-I". (mnemonic: edit 'i'n place) -I - like "-i" but no backup is made. (mnemonic: edit 'I'n place, trusting this script 'I'mplicitly. + ;^) _EOF_ ; exit $status } getopts('hp:Ii:') || usage(2); usage(0) if ($opt_h); $| = 1; my $oldargv; my %cache; while (<>) { # { this is straight from the "perlrun" man page: if ('-' ne $ARGV && ($opt_I || $opt_i) && $ARGV ne $oldargv) { if ('' eq $opt_i) { unlink($ARGV) or die "unlink \"$ARGV\": $!\n" } else { rename($ARGV, $ARGV . $opt_i) or die "rename \"$ARGV\": $!\n" } open(ARGVOUT, ">$ARGV"); select(ARGVOUT); $oldargv = $ARGV; } # } my $s = $_; my $prev = ''; my($name, $val); while ($s =~ m/(\d{1,3})(?:\.\d{1,3}){3}/) { my $ip = $&; $s =~ s/$1\.//; next if ($ip eq $prev); if (defined($cache{$ip})) { $name = $cache{$ip} } else { $name = gethostbyaddr(inet_aton($ip), AF_INET); $cache{$ip} = $name } if ('' eq $name) { $name = $ip } if ($opt_p) { $val = sprintf($opt_p, $ip, $name) } else { $val = $name } s/$ip/$val/g; $prev = $ip } print }