in reply to Domain name from ip.

Probably the easiest way to do this is through the use of the Net::hostent module. eg.

use Net::hostent; use strict; my $host = gethost("209.214.174.80"); print $host->name, "\n";

Remember though that domain resolution is only as good as the domain system - Some IP addresses may not have been set up with PTR records back to fully-qualified domain names.

Update #1 - Another excellent solution might be to use Net::Domain which provides methods not only for IP address resolution, but also hostname and hostdomain for system hostname and domain respectively.

Update #2 - Ugh, Net::Domain is only good for determining this information about the local machine.

 

perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Replies are listed 'Best First'.
Re: Re: Domain name from ip.
by dws (Chancellor) on Dec 03, 2001 at 05:23 UTC
    Remember though that domain resolution is only as good as the domain system - Some IP addresses may not have been set up with PTR records back to fully-qualified domain names.

    Indeed. Most of the odd hits in my server logs can't be backtraced to valid domain names, though there is often enough information available to find out what "neighborhood" they're in (e.g., who an IP addr's provider is).

    To dig up what info I could on suspect IP addresses, I whipped up the following script, which front-ends dig(1) on FreeBSD.

    #!/usr/bin/perl -w # # simple frontend for dig(1) # use strict; my $tracing = 1; sub usage { print <<EOM; usage: $0 ipaddr EOM exit(1); } my $ipaddr = shift @ARGV || usage(); my @ipaddr = split(/\./ , $ipaddr); # work our way through the parts of the ip address, stopping # when we're down to 1. do { dig_on(@ipaddr); pop @ipaddr; } until ( @ipaddr == 1 ); sub dig_on { my @addr = @_; my $subaddr = join('.', @addr); my $revaddr = join('.', reverse @ipaddr); print "going after $subaddr ...\n" if $tracing; open(IN, "dig -x $subaddr | grep $revaddr | ") or die "open(): $!"; while ( <IN> ) { next if /^;/; print; } close(IN); }
    Suggestions on how to improve this are welcome.