<p>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:</p>

<pre><code># 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 =&gt; 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
</code></pre>

<p>Everything works except it does not find a Reverse DNS.</p>

<p>Sample output that I'm getting is this:</p>

<pre><code>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
</code></pre>

<p>If I do <code>nslookup 10.26.6.12</code> then I get the correct reverse DNS so
that shows that my machine is seeing the DNS server.</p>

<p>I have tried <code>Socket.gethostbyname</code>, <code>gethostbyaddr</code>, but it doesn't work.</p>

<p>Any guidance will be much appreciated.</p>

[{'answer_id': 3012, 'author': 'Ecton', 'author_id': 457, 'author_profile': 'https://Stackoverflow.com/users/457', 'pm_score': 3, 'selected': False, 'text': '<p>I would check out <code>getaddrinfo</code>. If you replace the line:</p>\n\n<pre><code>host_name = Socket.gethostbyname(current_ip)\n</code></pre>\n\n<p>with:</p>\n\n<pre><code>host_name = Socket.getaddrinfo(current_ip, 0, Socket::AF_UNSPEC, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME)[0][1]\n</code></pre>\n\n<p>The <code>getaddrinfo</code> function returns an array of arrays. You can read more about it at:</p>\n\n<p><a href="http://www.ruby-doc.org/stdlib/libdoc/socket/rdoc/classes/Socket.html#M004531" rel="nofollow noreferrer">Ruby Socket Docs</a></p>\n'}, {'answer_id': 3306, 'author': 'garg', 'author_id': 155, 'author_profile': 'https://Stackoverflow.com/users/155', 'pm_score': 2, 'selected': False, 'text': '<p>This also works:</p>\n\n<pre><code>host_name = Socket.getaddrinfo(current_ip,nil)\nappend_to_file("#{host_name[0][2]} - #{current_ip} - #{mac_addr}\\n")\n</code></pre>\n\n<p>I\'m not sure why <code>gethostbyaddr</code> didn\'t also work.</p>\n'}, {'answer_id': 5544777, 'author': 'gertas', 'author_id': 446210, 'author_profile': 'https://Stackoverflow.com/users/446210', 'pm_score': 6, 'selected': True, 'text': "<p>Today I also needed reverse DNS lookup and I've found very simple standard solution:</p>\n\n<pre><code>require 'resolv'\nhost_name = Resolv.getname(ip_address_here)\n</code></pre>\n\n<p>It seems it uses timeout which helps in rough cases.</p>\n"}]
['https://Stackoverflow.com/questions/2993', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/155/']