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

Discussion in 'Perl Misc' started by eloelo, Aug 1, 2004.

  1. eloelo

    eloelo Guest

    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.
    eloelo, Aug 1, 2004
    #1
    1. Advertising

  2. eloelo <> 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\\
    Brian McCauley, Aug 1, 2004
    #2
    1. Advertising

  3. eloelo

    Facco Eloelo Guest

    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 <> 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
    Facco Eloelo, Aug 2, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Goche
    Replies:
    1
    Views:
    784
    Ranganath Kini
    Feb 9, 2006
  2. Luigi Donatello Asero

    A text which neither gets very narrow nor disappears

    Luigi Donatello Asero, Oct 3, 2005, in forum: HTML
    Replies:
    21
    Views:
    852
    Jonathan N. Little
    Jan 6, 2006
  3. Anonieko

    HttpHandlers - Learn Them. Use Them.

    Anonieko, Jun 15, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    507
    tdavisjr
    Jun 16, 2006
  4. why the lucky stiff
    Replies:
    5
    Views:
    134
    why the lucky stiff
    Sep 22, 2004
  5. Jake Barnes
    Replies:
    2
    Views:
    116
    Jake Barnes
    Apr 7, 2006
Loading...

Share This Page