2011-09-22 2 views
3

루비가 Ping.pingecho 방법을 가지고하는 데 사용 실종,하지만 것처럼 보인다 (그리고 Ping 모듈) 언젠가 사라졌다 : 루비 : Ping.pingecho는

% rvm use 1.8.7 
Using ~/.rvm/gems/ruby-1.8.7-p334 
% ruby -rping -e 'p Ping.pingecho "127.0.0.1"' 
true 
% rvm use 1.9.2 
Using ~/.rvm/gems/ruby-1.9.2-p180 
% ruby -rping -e 'p Ping.pingecho "127.0.0.1"' 
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- ping (LoadError) 
     from <internal:lib/rubygems/custom_require>:29:in `require' 
% ruby -e 'p Ping.pingecho "127.0.0.1"' 
-e:1:in `<main>': uninitialized constant Object::Ping (NameError) 

그것이 다른 라이브러리로 이동 한 (그래서 무엇을해야 I require을로드 하시겠습니까?) 또는 이 삭제되었으며 다른 모듈로 바뀌 었습니다 (따라서 IP에 연결할 수 있는지 여부를 결정하려면 어떻게해야합니까?).

답변

4

왜 또는 어디로 갔는지 알 수 없습니다. 레일즈는 여전히 Ping 클래스입니다. 약간의 적응 (클래스 메서드 사용)은 다음과 같습니다.

require 'timeout' 
require 'socket' 

class Ping 
    def self.pingecho(host, timeout=5, service="echo") 
    begin 
     timeout(timeout) do 
     s = TCPSocket.new(host, service) 
     s.close 
     end 
    rescue Errno::ECONNREFUSED 
     return true 
    rescue Timeout::Error, StandardError 
     return false 
    end 
    return true 
    end 
end 

p Ping.pingecho("127.0.0.1") #=> true 
p Ping.pingecho("localhost") #=> true 
2

방금이 문제가 발생했습니다. 나는 대체품으로 net-ping 보석으로 정착했다. 그것은 보석의 명확한 TCP 핑의 예를 가지고/그물 핑-1.7.7/예/example_pingtcp.rb :이 글을 쓰는 시점에서

p1 = Net::Ping::TCP.new(good, 'http') 
p p1.ping? 

rubydoc.info link 작동하지만, 여기에 유용한 주석입니다하지 않습니다 이와

return Ping.pingecho(server, 5, 22) 

:

모듈의 소스 (tcp.rb는)

# This method attempts to ping a host and port using a TCPSocket with 
# the host, port and timeout values passed in the constructor. Returns 
# true if successful, or false otherwise. 

그래서 나는이 교환

p = Net::Ping::TCP.new(server, 22, 5) 
p.ping? 

새로운 상응하는 모듈로 이전에서 이동의 두 가지주의 사항이 있습니다

  1. 생성자의 인수는 반전 핑의 실제 호출은 호출하여 수행됩니다
  2. (포트 및 시간 제한) ping? 메서드를 사용하는 것이 좋습니다.