问题描述
我所在的环境中有很多未使用过的计算机妥善盘点.基本上,没有人知道哪个 IP 配哪个mac 地址和哪个主机名.所以我写了以下内容:
I'm in an environment with a lot of computers that haven't beenproperly inventoried. Basically, no one knows which IP goes with whichmac 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}
")
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
除了找不到反向 DNS 之外,一切正常.
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
如果我执行 nslookup 10.26.6.12
然后我得到正确的反向 DNS 所以这表明我的机器正在查看 DNS 服务器.
If I do nslookup 10.26.6.12
then I get the correct reverse DNS sothat shows that my machine is seeing the DNS server.
我试过Socket.gethostbyname
、gethostbyaddr
,但没有用.
任何指导将不胜感激.
推荐答案
今天我也需要反向 DNS 查找,我找到了非常简单的标准解决方案:
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.
这篇关于Ruby 中的反向 DNS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!