문제

저는 제대로 발명하지 않은 많은 컴퓨터가있는 환경에 있습니다.기본적으로 어떤 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

역방향 DNS를 찾지 못하는 것을 제외하면 모든 것이 작동합니다.

내가 얻는 샘플 출력은 다음과 같습니다.

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를 얻습니다.

나는 시도했다 Socket.gethostbyname, gethostbyaddr, 하지만 작동하지 않습니다.

어떤 지침이라도 대단히 감사하겠습니다.

도움이 되었습니까?

해결책

나는 확인해 볼 것이다 getaddrinfo.라인을 교체하는 경우:

host_name = Socket.gethostbyname(current_ip)

와 함께:

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

그만큼 getaddrinfo 함수는 배열의 배열을 반환합니다.이에 대한 자세한 내용은 다음에서 확인할 수 있습니다.

루비 소켓 문서

다른 팁

오늘은 역방향 DNS 조회도 필요했고 매우 간단한 표준 솔루션을 찾았습니다.

require 'resolv'
host_name = Resolv.getname(ip_address_here)

거친 경우에 도움이 되는 시간 초과를 사용하는 것 같습니다.

이것은 또한 작동합니다:

host_name = Socket.getaddrinfo(current_ip,nil)
append_to_file("#{host_name[0][2]} - #{current_ip} - #{mac_addr}\n")

왜 그런지 잘 모르겠습니다. gethostbyaddr 또한 작동하지 않았습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top