Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby ping for 1.9.1

Tags:

ruby

I want to ping a site in my ruby code and saw that net-ping was a nice library to do this with. Unfortunately, when I tried to gem install net-ping I got the following error:

C:>gem install net-ping
ERROR: Error installing net-ping:
win32-open3 requires Ruby version < 1.9.0.

upon further research, I found that net-ping was not available yet for 1.9.X. Does anyone have a good piece of code that pings that they would be willing to share.

like image 356
rahrahruby Avatar asked Aug 24 '10 22:08

rahrahruby


1 Answers

If by 'site' you mean website, then I wouldn't use ping. Ping will tell you if the host is up (unless a router or firewall is blocking ICMP), but it won't tell you if your web server or web app is responding properly.

If that's the case, I'd recommend Net::HTTP from the standard library, or any of the other HTTP libraries. One way to do it is:

def up?(site)
  Net::HTTP.new(site).head('/').kind_of? Net::HTTPOK
end

up? 'www.google.com' #=> true
like image 134
Mark Thomas Avatar answered Nov 16 '22 01:11

Mark Thomas