在Ruby中反转DNS?

前端之家收集整理的这篇文章主要介绍了在Ruby中反转DNS?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在一个环境中有很多电脑还没有
妥善盘点基本上没有人知道哪个IP是哪个
mac地址和哪个主机名.所以我写了以下内容
  1. # This script goes down the entire IP range and attempts to
  2. # retrieve the Hostname and mac address and outputs them
  3. # into a file. Yay!
  4.  
  5. require "socket"
  6.  
  7. TwoOctets = "10.26"
  8.  
  9. def computer_exists?(computerip)
  10. system("ping -c 1 -W 1 #{computerip}")
  11. end
  12.  
  13. def append_to_file(line)
  14. file = File.open("output.txt","a")
  15. file.puts(line)
  16. file.close
  17. end
  18.  
  19.  
  20. def getInfo(current_ip)
  21. begin
  22. if computer_exists?(current_ip)
  23. arp_output = `arp -v #{current_ip}`
  24. mac_addr = arp_output.to_s.match(/..:..:..:..:..:../)
  25. host_name = Socket.gethostbyname(current_ip)
  26. append_to_file("#{host_name[0]} - #{current_ip} - #{mac_addr}\n")
  27. end
  28. rescue SocketError => mySocketError
  29. append_to_file("unknown - #{current_ip} - #{mac_addr}")
  30. end
  31. end
  32.  
  33.  
  34. (6..8).each do |i|
  35. case i
  36. when 6
  37. for j in (1..190)
  38. current_ip = "#{TwoOctets}.#{i}.#{j}"
  39. getInfo(current_ip)
  40. end
  41. when 7
  42. for j in (1..255)
  43. current_ip = "#{TwoOctets}.#{i}.#{j}"
  44. getInfo(current_ip)
  45. end
  46. when 8
  47. for j in (1..52)
  48. current_ip = "#{TwoOctets}.#{i}.#{j}"
  49. getInfo(current_ip)
  50. end
  51. end
  52. end

一切正常,除了没有找到反向DNS.

我得到的示例输出是:

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

如果我做nslookup 10.26.6.12那么我得到正确的反向DNS所以
这表明我的机器正在看到DNS服务器.

我试过Socket.gethostbyname,gethostbyaddr,但它不工作.

任何指导将不胜感激.

解决方法

我会查看getaddrinfo.如果换行:
  1. host_name = Socket.gethostbyname(current_ip)

有:

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

getaddrinfo函数返回数组数组.您可以在以下位置阅读更多信息:

Ruby Socket Docs

猜你在找的Ruby相关文章