# Reverse DNS lookup?

**URL:** <https://www.ruby-forum.com/t/reverse-dns-lookup/145102>\
**Category:** Ruby\
**Created:** [August 6, 2008, 2:49am UTC](https://www.ruby-forum.com/t/reverse-dns-lookup/145102 "2008-08-06T02:49:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![garg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@garg](https://www.ruby-forum.com/u/garg)\
**Post date:** [August 6, 2008, 2:49am UTC](https://www.ruby-forum.com/t/reverse-dns-lookup/145102/1 "2008-08-06T02:49:36Z")

</div>

I’m in an environment with a lot of computers that haven’t been  
properly inventoried. Basically, no one knows which IP goes with which  
mac address and which hostname. So I wrote the following:

# This script goes down the entire IP range and attempts to

# retrieve the Hostname and mac address and outputs them

# into a file. Yay!

require “socket”

TwoOctets = “10.26”

def computer\_exists?(computerip)  
system(“ping -c 1 -W 1 #{computerip}”)  
end

def append\_to\_file(line)  
file = File.open(“output.txt”, “a”)  
file.puts(line)  
file.close  
end

def getInfo(current\_ip)  
begin  
if computer\_exists?(current\_ip)  
arp\_output = `arp -v #{current_ip}`  
mac\_addr = arp\_output.to\_s.match(/…:…:…:…:…:…/)  
host\_name = Socket.gethostbyname(current\_ip)  
append\_to\_file("#{host\_name[0]} - #{current\_ip} - #{mac\_addr}\n")  
end  
rescue SocketError =\> mySocketError  
append\_to\_file(“unknown - #{current\_ip} - #{mac\_addr}”)  
end  
end

(6…8).each do |i|  
case i  
when 6  
for j in (1…190)  
current\_ip = “#{TwoOctets}.#{i}.#{j}”  
getInfo(current\_ip)  
end  
when 7  
for j in (1…255)  
current\_ip = “#{TwoOctets}.#{i}.#{j}”  
getInfo(current\_ip)  
end  
when 8  
for j in (1…52)  
current\_ip = “#{TwoOctets}.#{i}.#{j}”  
getInfo(current\_ip)  
end  
end  
end

Everything works except it does not find a Reverse DNS.

Sample output that I’m getting is this:

10.26.6.12 - 10.26.6.12 - 00:11:11:9B:13:9F  
10.26.6.17 - 10.26.6.17 - 08:00:69:9A:97:C3  
10.26.6.18 - 10.26.6.18 - 08:00:69:93:2C:E2

If I do `nslookup 10.26.6.12` then I get the correct reverse DNS so  
that shows that my machine is seeing the dns server.

I have tried Socket.gethostbyname, gethostbyaddr, but it doesn’t work :  
(

Any guidance will be much appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![garg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@garg](https://www.ruby-forum.com/u/garg)\
**Post date:** [August 6, 2008, 9:10am UTC](https://www.ruby-forum.com/t/reverse-dns-lookup/145102/2 "2008-08-06T09:10:49Z")

</div>

Hi,

2008/8/6 ga rg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> TwoOctets = “10.26”
> 
> append\_to\_file(“unknown - #{current\_ip} - #{mac\_addr}”)  
> end  
> end
> 
> If I do `nslookup 10.26.6.12` then I get the correct reverse DNS so  
> that shows that my machine is seeing the dns server.
> 
> I have tried Socket.gethostbyname, gethostbyaddr, but it doesn’t work :  
> (
> 
> Any guidance will be much appreciated. Thanks!

Try getaddrinfo like this:

s = Socket.getaddrinfo(‘10.26.6.12’,nil)  
hostname = s[0][2]

Regards,

Park H.

---

<div class="post-metadata">

**Author:** ![garg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dec6dc/32.png) [@garg](https://www.ruby-forum.com/u/garg)\
**Post date:** [August 6, 2008, 2:01pm UTC](https://www.ruby-forum.com/t/reverse-dns-lookup/145102/3 "2008-08-06T14:01:16Z")

</div>

Thank you very much. That works perfectly :). Any reason why  
getnamebyaddr didn’t work because rubydocs seems to suggest that it  
should.

Heesob P. wrote:

> Hi,
> 
> 2008/8/6 ga rg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > TwoOctets = “10.26”
> > 
> > append\_to\_file(“unknown - #{current\_ip} - #{mac\_addr}”)  
> > end  
> > end
> > 
> > If I do `nslookup 10.26.6.12` then I get the correct reverse DNS so  
> > that shows that my machine is seeing the dns server.
> > 
> > I have tried Socket.gethostbyname, gethostbyaddr, but it doesn’t work :  
> > (
> > 
> > Any guidance will be much appreciated. Thanks!
> 
> Try getaddrinfo like this:
> 
> s = Socket.getaddrinfo(‘10.26.6.12’,nil)  
> hostname = s[0][2]
> 
> Regards,
> 
> Park H.
