12

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.

1
  • If you really need to do ICMP ping, you always can use native OS tool like ping -c 1 host and parse result.
    – taro
    Aug 25, 2010 at 11:04

4 Answers 4

15

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
3
  • 1
    Also site can deny non-browser requests. Compare www.habrahabr.ru and www.google.ru. So you need look into response more closely and/or play with user-agents.
    – Nakilon
    Aug 28, 2010 at 11:31
  • I experienced problems with this code on windows, receiving a "Bad Gateway" for a working site. I could work around it using "get" instead of "head". Maybe this helps someone on windows.
    – mit
    Jul 8, 2012 at 21:44
  • @mit Sounds like you hit a proxy that wasn't to spec and couldn't handle 'HEAD' requests. Glad you got it working with get. Jul 8, 2012 at 23:18
12

You can always do this and use regexps to parse the result or just check the exit status:

ping_count = 10
server = "www.google.com"
result = `ping -q -c #{ping_count} #{server}`
if ($?.exitstatus == 0) do
  puts "Device is up!"
end

Ping return values that you can check against:

The ping utility returns an exit status of zero if at least one response was heard from the specified host; a status of two if the transmission was successful but no responses were received; or another value (from <sysexits.h>) if an error occurred.

http://www.manpagez.com/man/8/ping

3
  • Just checking to see if a device is up before I run the rest of my code. Also, tried your code, how do I see the results of the ping?
    – rahrahruby
    Aug 25, 2010 at 15:33
  • 2
    This was the hint I needed to use `ping -t 1 ipinfodb.com` to have a working timeout in place and to find out if ipinfodb.com is online. Timeout and SystemTimer both didn't work somehow and waited full 60 seconds to throw an error. Thanks! May 16, 2011 at 9:34
  • Shelling out to the OS for something as trivial as a ping is extraordinarily bad practice. This answer will fail under windows (as ping arguments and output format are different across different OS implementations). This is a particularly bad answer since the text of the question itself shows a "C:" prompt, clearly indicating a non-*nix environment.
    – Fopedush
    Jul 9, 2015 at 0:04
2

For windows follow these instructions:

download djberg96-net-ping-net-ping-1.5.3-0-g9252076.zip from github.com/djberg96/net-ping
unzip, cd in the folder
gem build net-ping.gemspec
gem install net-ping-1.5.3-universal-mingw32.gem
gem install win32-security

to use it:

require 'net/ping'
p Net::Ping::TCP.new('www.google.com', 'http').ping?
p Net::Ping::TCP.new('foo.bar.baz').ping?
1

Use from source

gem "net-ping", :git => 'git://github.com/djberg96/net-ping.git'
# bundle install

Now you can use functions from it in 1.9.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.