2 methods to get the domains's IP,but neither of them is good.

E

eloelo

I have lots of domains' name(nearly 1000) in a text file which called
"domain.txt".It looks like this:

domain.txt
www.yahoo.com
www.msn.com
www.aol.com
....

Now,I want to get their IP addresses(include all IPs. e.g,yahoo has many
IP,I want to get them all),and put the results into a new text file called
"IPlist.txt".It looks like this:

IPlist.txt
www.yahoo.com,66.94.230.51
www.yahoo.com,66.94.230.52
www.yahoo.com,66.94.230.43
....
www.msn.com,202.108.250.249
www.msn.com,61.135.152.77
www.msn.com,61.135.150.75
....


I have two methods to achive my goal above but neither is good.

Method#1
It can only get one IP of each domain.In most cases it's not a
problem.But,when a domain has more than one IP,like yahoo,it can get only
one IP.And it costs lots of time before it gets the result.

#!/usr/bin/perl
use warnings;
use strict;
use Socket;

my $domain_file = 'domain.txt';
my $IPlist_file = 'IPlist.txt';

open my $domain, '<', $domain_file or die "Cannot open $domain_file: $!";
open my $IPlist, '>', $IPlist_file or die "Cannot open $IPlist_file: $!";

while ( <$domain> ) {
chomp;
my $ip = gethostbyname $_;
print $IPlist "$_,",",",inet_ntoa( $ip ), "\n" if defined $ip;
}

Method#2
It can get all of the IP of some domain which has more than one IP,but at
the same time I can also get lots of query failed domain.

#!/usr/local/bin/perl
use Net::DNS;

open(D1,"domain.txt");
@line=<D1>;
$num=@line;
close(D1);
open(D2,">IPlist.txt");
open(FP,">fail");
for($i=0;$i<$num;$i++)
{

chop($line[$i]);
my $timeout = 15;
my $res = Net::DNS::Resolver->new;
my $query = $res->search($line[$i]);

if ($query) {
foreach my $rr ($query->answer) {
next unless $rr->type eq "A";
print D2 $line[$i],',',$rr->address,"\n";
}
} else {
print FP $line[$i], "query failed: ", $res->errorstring, "\n";
}
}


Anyone has a better way to solve this problem?Thanks in advance.
 
B

Brian McCauley

eloelo said:
It can only get one IP of each domain.
my $ip = gethostbyname $_;

Are you under the mistaken impression that gethostbyname will only
return one IP address?

perldoc -f gethostbyname

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
F

Facco Eloelo

This is what I did:

#!/usr/bin/perl
use Socket;
use Net::hostent;

my $domain_file = 'D1.txt';
my $IPlist_file = 'D2.txt';

open my $domain, '<', $domain_file or die "Cannot open $domain_file: $!";
open my $IPlist, '>', $IPlist_file or die "Cannot open $IPlist_file: $!";

while ( <$domain> )
{
chomp;
$name = $_;

if ($hent = gethostbyname($name))
{
$addr_ref = $hent->addr_list;
@addresses = map { inet_ntoa($_) } @$addr_ref;
}

$num=@addresses;
for ($i=0;$i<$num;$i++)
{
print $IPlist "$name,$addresses[$i]\n";
}
}
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top