5

I'm trying to get all the IP addresses for a host.

This is the nslookup output:

>>nslookup site.com

Server:         8.8.8.8
Address:        8.8.8.8#53

Non-authoritative answer:
Name:   site.com
Address: 1.1.1.1
Name:   site.com
Address: 2.2.2.2

I tried this code:

use Socket;
use Data::Dumper;
my $name = "site.com";
@addresses = gethostbyname($name)   or die "Can't resolve $name: $!\n";
@addresses = map { inet_ntoa($_) } @addresses[4 .. $#addresses];
print Dumper(\@addresses);

And this is the output:

['1.1.1.1'];

Anyway to get both 1.1.1.1 and 2.2.2.2?

Navid Zarepak
  • 4,148
  • 1
  • 12
  • 26
  • Note that `perldoc -f gethostbyname` tells us: _"Make sure "gethostbyname" is called in SCALAR context and that its return value is checked for definedness."_ Your code sample does neither. – James E Keenan Sep 11 '18 at 23:31
  • 1
    @JamesEKeenan That's for resolving a hostname to a single IP address when you don't care about all the other information a list context `gethostbyname` returns. – Shawn Sep 11 '18 at 23:35

2 Answers2

8

You can use Net::DNS::Resolver to get the IPv4 addresses (A records) for a hostname:

use warnings;
use strict;

use feature 'say';

use Net::DNS::Resolver;

my $res = Net::DNS::Resolver->new;

my $name = 'stackoverflow.com';
my $q = $res->query($name);

if ($q){
    print "$name has the following IPv4 addresses:\n";
    for ($q->answer){
        say $_->address if $_->type eq 'A';
    }
}

Output:

stackoverflow.com has the following IPv4 addresses:
151.101.65.69
151.101.193.69
151.101.1.69
151.101.129.69
stevieb
  • 9,065
  • 3
  • 26
  • 36
  • 1
    That gives you all of the IPs that nameserver hands out, which is probably what OP wanted, but it's not the same as all IP addresses of a hostname. – Cupcake Protocol Sep 11 '18 at 19:51
  • 1
    @CupcakeProtocol `cnn.com`'s authoritative name server hands out the same IP addresses. Please clarify what you're getting at. – stevieb Sep 11 '18 at 20:00
  • I get completely different answers for www.cnn.com querying from California and New York. What I mean is DNS tricks can be used to direct you to a particular instance or set of instances, and an exhaustive list of IP addresses is not practical to determine as an outsider. – Cupcake Protocol Sep 11 '18 at 20:49
  • @CupcakeProtocol Examples are desired. – stevieb Sep 11 '18 at 21:10
  • @CupcakeProtocol Test against authoritative sources; not against ad-placing, DNS hijacking ISPs. – stevieb Sep 11 '18 at 21:11
  • Look at the first example in the section called "stupid dns tricks": https://queue.acm.org/detail.cfm?id=1647302 – Cupcake Protocol Sep 11 '18 at 21:26
  • I understand. I've met Paul Vixie at ARIN conferences. There's many ambiguities. If you have a better answer for OP, write one ;) – stevieb Sep 11 '18 at 21:28
  • 3
    I think your answer is what OP wanted, but OP's question is vague and for posterity I wanted to point that out. – Cupcake Protocol Sep 11 '18 at 21:31
  • Personally, I would like to see your digression. Look at my [CPAN](https://metacpan.org/author/STEVEB) page for my email address. This is a Perl-esque question, but I'm still interested (although kind of detached) from DNS stuff. Love to discuss. – stevieb Sep 11 '18 at 21:33
  • How many IP addresses do companies like Facebook or Google have (indicatively) in order to serve billions of requests? Thank you! – tonix Sep 26 '20 at 12:23
  • 1
    @tonix Billions of requests can be served from a single IP address... – stevieb Oct 10 '20 at 18:19
1

Simple pure perl to print the IP of the domain stackoverflow.com :

use Socket;
print join'.',unpack('C4',inet_aton('stackoverflow.com'));
print "\n";
MUY Belgium
  • 2,330
  • 4
  • 30
  • 46