Last active
May 6, 2019 01:47
-
-
Save deepak/9b4c4586f4611762bc31 to your computer and use it in GitHub Desktop.
ruby ping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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