How do we check if there is internet connection with Ruby?

1,000 views
Skip to first unread message

Joshua Partogi

unread,
Nov 19, 2009, 11:53:29 PM11/19/09
to rails-...@googlegroups.com
Hi all,

I'm wondering if anyone has ever been in this situation before. I want
to be able to load files dynamically from the internet, but I need to
check whether the customer has internet connection or not. Is this
doable with Ruby? Has anyone done this before?

Many thanks for the insights.

--
Certified Scrum Master
http://blog.scrum8.com | http://jobs.scrum8.com | http://twitter.com/scrum8

Julio Cesar Ody

unread,
Nov 20, 2009, 12:03:34 AM11/20/09
to rails-...@googlegroups.com
You *could* use ICMP (popularly known as "ping") and try to reach a
host like google.com, which would be what I think it's the correct
way. That of course means you're also verifying whether you have a DNS
server available. I believe you'll anyway be relying on that for
whatever your need is.

But as a quickie, this will do

require 'socket'
begin
TCPSocket.new 'google.com', 80
rescue SocketError
STDERR.puts "not connected sir"
end

Replace error handling with whatever you need.

Ping in Ruby: http://raa.ruby-lang.org/project/net-ping/.
> --
>
> You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group.
> To post to this group, send email to rails-...@googlegroups.com.
> To unsubscribe from this group, send email to rails-oceani...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=.
>
>
>

Lyndon Maydwell

unread,
Nov 20, 2009, 12:14:17 AM11/20/09
to rails-...@googlegroups.com
I wonder how much traffic Google gets from these kind of connectivity tests...

Mark Ryall

unread,
Nov 20, 2009, 12:17:14 AM11/20/09
to rails-...@googlegroups.com
Wouldn't it be easier and more sensible to attempt what you actually
want to do and handle failures appropriately?

Whether you can ping google is irrelevant if the remote host you need is down.

On Fri, Nov 20, 2009 at 3:53 PM, Joshua Partogi
<joshua....@gmail.com> wrote:

Joshua Partogi

unread,
Nov 20, 2009, 12:17:25 AM11/20/09
to rails-...@googlegroups.com
You're right. I thought there's a more networking library in Ruby for
this kind of stuff.

On Fri, Nov 20, 2009 at 4:14 PM, Lyndon Maydwell <mayd...@gmail.com> wrote:
> I wonder how much traffic Google gets from these kind of connectivity tests...



Julio Cesar Ody

unread,
Nov 20, 2009, 12:33:19 AM11/20/09
to rails-...@googlegroups.com
Yeah. That's a generic response for "check if there's internet connection".

If there's a specific host to be connected to, then +1.

Joshua Partogi

unread,
Nov 20, 2009, 1:12:45 AM11/20/09
to rails-...@googlegroups.com
Not really.The Rails app might be in an intranet server, but there's a
possibility that the intranet server can not connect to the internet.
So user would still be able to access the app locally, but the app
might not be able to access the internet.

On Fri, Nov 20, 2009 at 4:17 PM, Mark Ryall <mark....@gmail.com> wrote:
> Wouldn't it be easier and more sensible to attempt what you actually
> want to do and handle failures appropriately?
>
> Whether you can ping google is irrelevant if the remote host you need is down.


Korny Sietsma

unread,
Nov 23, 2009, 1:26:19 AM11/23/09
to rails-...@googlegroups.com
Also, if there's a risk of running inside a corporate network, you may
well have an authenticating proxy to go through - so merely opening a
connection to google.com might not be enough, you might need to check
for 'http_proxy' environment variables, or platform-specific proxy
settings, or prompt the user - which defeats the purpose of the
automated check.

I suspect that in these cases you often *won't* be able to
automatically check for internet existence - so whatever your app
does, you should give the user some way to override it and say "yes, I
*do* have teh internets, but you need to access it via
http://proxy.evilcorp.com:666 with username 'fred' and password
'fredNov2009'*"

And, of course, your user may well have *partial* internet access -
they might have google.com but have firewall rules blocking gmail.com.
So I'm with Mark, check what they *need* to access. And allow them
to override your check!

- Korny (all too familiar with loading apps that fail, as they don't
work with proxies)
* of course any big corporate will 'secure' their network by forcing
monthly password changes, resulting in lots of passwords based on the
date. :)
> --
>
> You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group.
> To post to this group, send email to rails-...@googlegroups.com.
> To unsubscribe from this group, send email to rails-oceani...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=.
>
>
>



--
Kornelis Sietsma korny at my surname dot com
kornys on twitter/fb/gtalk/gwave www.sietsma.com/korny
"Every jumbled pile of person has a thinking part
that wonders what the part that isn't thinking
isn't thinking of"

Jonathan Clarke

unread,
Nov 23, 2009, 4:11:33 AM11/23/09
to rails-...@googlegroups.com
I wrote a little gem for this....nothing for proxies but handy for checking the domain / port availability...fork away

http://github.com/beilabs/netchecker

Jonathan

2009/11/23 Korny Sietsma <ko...@sietsma.com>

Joshua Partogi

unread,
Nov 23, 2009, 5:18:53 AM11/23/09
to rails-...@googlegroups.com
Perfect. Exactly what I am looking for.

Cheers.

On Mon, Nov 23, 2009 at 8:11 PM, Jonathan Clarke
<clarke....@gmail.com> wrote:
> I wrote a little gem for this....nothing for proxies but handy for checking
> the domain / port availability...fork away
>
> http://github.com/beilabs/netchecker
>

Ryan Bigg

unread,
Nov 23, 2009, 6:06:56 PM11/23/09
to rails-...@googlegroups.com

Julio Cesar Ody

unread,
Nov 23, 2009, 6:13:57 PM11/23/09
to rails-...@googlegroups.com
And I looked in the Net namespace and couldn't find anything *sad face*

Thanks!

ps: reiterating, ICMP is what you're looking for if the purpose of the
test is to verify if a host can be reached.



On Tue, Nov 24, 2009 at 10:06 AM, Ryan Bigg <radarl...@gmail.com> wrote:
> Ruby has this built
> in: http://ruby-doc.org/stdlib/libdoc/ping/rdoc/index.html
>

Mark Mansour

unread,
Nov 23, 2009, 6:29:44 PM11/23/09
to rails-...@googlegroups.com
There are a few reasons ICMP is not the way to go:

* ICMP is not reliable because some hosts deliberately block ICMP
traffic to stop the ping of death
[http://en.wikipedia.org/wiki/Ping_of_death].
* It is a different port and protocol than you probably want to
communicate on. So reaching (or not reaching) a host via ICMP does
not guarantee you can make a normal socket connection (and vice
versa).
* ICMP is normally blocked from behind firewalls.

The best approach, as has already been stated, is to make the request
and handle the failed attempt gracefully.

Mark
--
Mark Mansour
ma...@agilebench.com
http://agilebench.com/

Ryan Bigg

unread,
Nov 23, 2009, 6:35:36 PM11/23/09
to rails-...@googlegroups.com
2009/11/24 Mark Mansour <ma...@stateofflux.com>

There are a few reasons ICMP is not the way to go:

* ICMP is not reliable because some hosts deliberately block ICMP
traffic to stop the ping of death
[http://en.wikipedia.org/wiki/Ping_of_death].
* It is a different port and protocol than you probably want to
communicate on.  So reaching (or not reaching) a host via ICMP does
not guarantee you can make a normal socket connection (and vice
versa).
* ICMP is normally blocked from behind firewalls.

The best approach, as has already been stated, is to make the request
and handle the failed attempt gracefully.

Mark


The ping library also supports checking the response on a different port; check out the service argument in the pingecho command.
--
Ryan Bigg
Reply all
Reply to author
Forward
0 new messages