Skip to content

Instantly share code, notes, and snippets.

@deepak
Last active May 6, 2019 01:47
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 deepak/9b4c4586f4611762bc31 to your computer and use it in GitHub Desktop.
Save deepak/9b4c4586f4611762bc31 to your computer and use it in GitHub Desktop.
ruby ping
# let us say you want to check if a host is reachable
# sometimes you need to be behind a private VPN or the host is just down
# and you want to do this on your local-machine and on Heroku
# can open your terminal and use ping on your local
# And can get a bash shell on Heroku as well - by `heroku run bash`
# but there is no `ping` binary there
# ```bash
# ~ $ ping
# bash: ping: command not found
# ```
# so can use this small ruby script
# to check if host is reachable from the Heroku app
# EDIT: duh! there is https://github.com/djberg96/net-ping
# but need to install it
# this is ok for quick and dirty
require 'net/http'
def up?(site)
Net::HTTP.new(site).head('/').kind_of? Net::HTTPOK
end
up?("google.com") #=> true
# can throw error for. can catch and return false if you want :-)
# 1. invalid host. SocketError: getaddrinfo: Name or service not known
# 2. connection refused. Errno::ECONNREFUSED: Connection refused - connect(2) for "<some-ip>" port 80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment