Reverse DNS in Ruby? Reverse DNS in Ruby? ruby ruby

Reverse DNS in Ruby?


Today I also needed reverse DNS lookup and I've found very simple standard solution:

require 'resolv'host_name = Resolv.getname(ip_address_here)

It seems it uses timeout which helps in rough cases.


I would check out getaddrinfo. If you replace the line:

host_name = Socket.gethostbyname(current_ip)

with:

host_name = Socket.getaddrinfo(current_ip, 0, Socket::AF_UNSPEC, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME)[0][1]

The getaddrinfo function returns an array of arrays. You can read more about it at:

Ruby Socket Docs


This also works:

host_name = Socket.getaddrinfo(current_ip,nil)append_to_file("#{host_name[0][2]} - #{current_ip} - #{mac_addr}\n")

I'm not sure why gethostbyaddr didn't also work.