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