开发者

Check server status in ruby using domain name?

开发者 https://www.devze.com 2023-02-04 09:41 出处:网络
I want to be abl开发者_如何学JAVAe to survey a website and see if it is online. I would like it to return a boolean value, but i can\'t find out a way to do it. Thanks.Since @JacobRelkin yanked his an

I want to be abl开发者_如何学JAVAe to survey a website and see if it is online. I would like it to return a boolean value, but i can't find out a way to do it. Thanks.


Since @JacobRelkin yanked his answer, I'll post it. Note that you must include the protocol in the domain name.

require 'net/http'
require 'uri'

def website_online?(site_url) 
   begin
      url = URI.parse(site_url)
      res = Net::HTTP.start(url.host, url.port) {|http|
        http.get('/')
      }
      res.body.length > 0
   rescue 
      false
   end
end

p website_online?( 'phrogz.net' )
#=> false

p website_online?( 'http://phrogz.net' )
#=> true


Varies what you mean by "online" and also if ping isn't disabled.

system('ping hostname')

You also get access to all of the built in system tools feedback.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号