Skip to content

Instantly share code, notes, and snippets.

@jerodsanto
Created June 15, 2010 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jerodsanto/439650 to your computer and use it in GitHub Desktop.
Save jerodsanto/439650 to your computer and use it in GitHub Desktop.
A poor man's replacement for Ruby's Ping.pingecho method
# Ping.pingecho uses TCP echo requests, which sucks when you really want
# to know if the machine is pingable, ie - it answers ICMP requests
# Here's a poor man's replacement for use in a pinch:
def pingable?(host, timeout=5)
system "ping -c 1 -t #{timeout} #{host} >/dev/null"
end
@thiagopintodev
Copy link

thiagopintodev commented Jul 7, 2018

Hey! I'm speaking from the future. 8 years after you wrote this.

You can still reimplement that old code source https://ruby-doc.org/stdlib-1.8.7/libdoc/ping/rdoc/Ping.html

def ping(host, service='echo')
  Timeout.timeout(5) do
    TCPSocket.new(host, service).close
  end
  true
rescue Errno::ECONNREFUSED
  true
rescue Timeout::Error, StandardError
  false
end

# ping('localhost', 3000)
# ping('google.com', 80)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment