Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > 2 methods to get the domains's IP,but neither of them is good.

Reply
Thread Tools

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

 
 
eloelo
Guest
Posts: n/a
 
      08-01-2004
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:NS;

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:NS::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.






 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      08-01-2004
eloelo <(E-Mail Removed)> writes:

> 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\\
 
Reply With Quote
 
 
 
 
Facco Eloelo
Guest
Posts: n/a
 
      08-02-2004
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";
}
}
--
>eloelo <(E-Mail Removed)> writes:
>
>> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
can't get the text from a div, neither when I try to use innerHTML or when I try to walk the DOM Jake Barnes Javascript 2 04-07-2006 02:42 AM
incompatible types for ?: neither is a subtype of the other John Goche Java 1 02-09-2006 01:06 PM
A text which neither gets very narrow nor disappears Luigi Donatello Asero HTML 21 01-06-2006 11:32 PM
Search box will not go away and neither will Superbar! Phyloe Computer Support 3 11-29-2003 03:55 AM



Advertisments