-1

In a bash, I have an IP like 104.16.117.182. This is the address of unix.stackexchange.com.

But, from the IP, how can I resolve it to the hostname? Like:

>>> magic 104.16.117.182
you get: unix.stackexchange.com

Any idea?

1 Answers1

1

Reverse records (for IPv4) are stored (backwards) somewhere under the in-addr.arpa zone, which tools like host will handily reverse for you, while other tools may need to be fed the reversed IP address and so forth.

% host 8.8.8.8 | awk '{print $NF}'
google-public-dns-a.google.com.
% host 104.16.117.182
Host 182.117.16.104.in-addr.arpa. not found: 3(NXDOMAIN)
% dig +short PTR 4.4.8.8.in-addr.arpa
google-public-dns-b.google.com.
% 

Whether appropriate reverse records have been setup, well, maybe you get lucky, maybe you do not.

Over in IPv6istan, you'll probably want a tool (e.g. sipcalc) that can expand out an IPv6 address instead of typing things out...

% dig +short PTR 8.8.8.8.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.6.8.4.0.6.8.4.1.0.0.2.ip6.arpa.
google-public-dns-a.google.com.
% 
thrig
  • 34,938
  • It din't work for me yet. When I tried the google (8.8.8.8) I have no problem. But to get the hostname of stackexchange don't work. But if I copy and paste the IP (104.16.117.182) in my browser I don't get to stackexchange. The problem now is the cloudflare? – Pedro Henrique Pedro Apr 13 '16 at 16:01
  • If there's no reverse record, then there's no reverse record. They're not always set these days, especially for things like load balancers that probably host all sorts of different domains, or cloud services where there's no API to fiddle around with records in the appropriate reverse zone. – thrig Apr 13 '16 at 17:14