开发者社区 > 域名解析DNS > DNS软件服务 > 正文

Ruby中的反向DNS

我所处的环境中有很多电脑都没有经过适当的盘点。基本上,没有人知道哪个IP对应哪个mac地址和哪个主机名。所以我写了以下内容

# 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

展开
收起
游客ufivfoddcd53c 2020-01-03 15:49:44 843 0
1 条回答
写回答
取消 提交回答
  • 这也适用:

    host_name = Socket.getaddrinfo(current_ip,nil)
    append_to_file("#{host_name[0][2]} - #{current_ip} - #{mac_addr}\n")
    
    2020-01-03 15:51:15
    赞同 展开评论 打赏
问答分类:
问答标签:
相关产品:

市场上DNS解析软件和解析服务的相关资讯

相关产品

  • 云解析DNS
  • 相关电子书

    更多
    神龙云服务器产品及技术深度解析 立即下载
    弹性创造价值:基于ECS的最佳性价比实践解析 立即下载
    又快又稳:阿里云下一代虚拟交换机解析 立即下载

    相关镜像