# Test for Internet Connection

**URL:** <https://rubytalk.org/t/test-for-internet-connection/45238>\
**Category:** ruby-talk\
**Created:** [17 March 2008 13:32 UTC](https://rubytalk.org/t/test-for-internet-connection/45238 "2008-03-17T13:32:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cory\_Cory](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@Cory\_Cory](https://rubytalk.org/u/Cory_Cory)\
**Post date:** [17 March 2008 13:32 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/1 "2008-03-17T13:32:38Z")

</div>

Does anyone know a way to test to see if the computer is connected to  
the internet in Ruby?

I wrote a Ruby program with WWW::Mechanize, but it completely hangs Ruby  
when my internet is turned off. Because Ruby does not use system  
threading, there is no way to check for a timeout when WWW::Mechanizer  
freezes due to not having an internet connection.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Pascal\_Bourguignon](https://avatars.discourse-cdn.com/v4/letter/p/ba9def/32.png) [@Pascal\_Bourguignon](https://rubytalk.org/u/Pascal_Bourguignon)\
**Post date:** [17 March 2008 13:49 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/2 "2008-03-17T13:49:53Z")

</div>

Cory Cory \<cg821105@ohio.edu\> writes:

> Does anyone know a way to test to see if the computer is connected to  
> the internet in Ruby?
> 
> I wrote a Ruby program with WWW::Mechanize, but it completely hangs Ruby  
> when my internet is turned off. Because Ruby does not use system  
> threading, there is no way to check for a timeout when WWW::Mechanizer  
> freezes due to not having an internet connection.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'ping'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ping.pingecho("google.com",10,80) # --\> true or false

> **···**
>
> --  
> \_\_Pascal Bourguignon\_\_

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [17 March 2008 14:23 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/3 "2008-03-17T14:23:20Z")

</div>

I usually do something like this on my machine

loop do  
break if system("ping google.com")  
sleep 1  
end  
puts "Internet working!"

Cory Cory wrote:

> **···**
>
> > Does anyone know a way to test to see if the computer is connected to  
> > the internet in Ruby?
> > 
> > I wrote a Ruby program with WWW::Mechanize, but it completely hangs Ruby  
> > when my internet is turned off. Because Ruby does not use system  
> > threading, there is no way to check for a timeout when WWW::Mechanizer  
> > freezes due to not having an internet connection.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Avdi\_Grimm1](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@Avdi\_Grimm1](https://rubytalk.org/u/Avdi_Grimm1)\
**Post date:** [17 March 2008 14:27 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/4 "2008-03-17T14:27:44Z")

</div>

What OS?

> **···**
>
> On Mon, Mar 17, 2008 at 9:32 AM, Cory Cory \<cg821105@ohio.edu\> wrote:
> 
> > Does anyone know a way to test to see if the computer is connected to  
> > the internet in Ruby?
> 
> --  
> Avdi

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [15 February 2014 00:36 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/5 "2014-02-15T00:36:05Z")

</div>

Cory Cory wrote in post #647411:

> Does anyone know a way to test to see if the computer is connected to  
> the internet in Ruby?

Apologies for necro threading but let my answer help other people coming  
to this link via google.

if system("ping google.com")  
puts "Connected to the internet."  
else puts "No internet connection."  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [16 February 2014 19:30 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/6 "2014-02-16T19:30:49Z")

</div>

The original issue was that Mechanize was not timing out, so perhaps  
this would be a more universal fix:

require 'timeout'  
Timeout::timeout(seconds) do  
&nbsp;&nbsp;Mechanize.something  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Cory\_Cory](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@Cory\_Cory](https://rubytalk.org/u/Cory_Cory)\
**Post date:** [17 March 2008 14:20 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/7 "2008-03-17T14:20:59Z")

</div>

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'ping'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ping.pingecho("google.com",10,80) # --\> true or false

This did not work. The same exact problem occurred: Ruby froze and  
there was no way to unfreeze it besides connected to the internet or  
force quit.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Cory\_Cory](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@Cory\_Cory](https://rubytalk.org/u/Cory_Cory)\
**Post date:** [17 March 2008 15:08 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/8 "2008-03-17T15:08:45Z")

</div>

Roger Pack wrote:

> I usually do something like this on my machine
> 
> loop do  
> break if system("ping google.com")  
> sleep 1  
> end  
> puts "Internet working!"

Even that caused Ruby to hang.  
I am using Ubuntu.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Panagiotis\_Atmatzidi](https://avatars.discourse-cdn.com/v4/letter/p/a88e4f/32.png) [@Panagiotis\_Atmatzidi](https://rubytalk.org/u/Panagiotis_Atmatzidi)\
**Post date:** [15 February 2014 11:24 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/9 "2014-02-15T11:24:32Z")

</div>

Hello,

> **···**
>
> On 15 Φεβ 2014, at 02:36 , cynic limbu \<lists@ruby-forum.com\> wrote:
> 
> > Cory Cory wrote in post #647411:
> > 
> > > Does anyone know a way to test to see if the computer is connected to  
> > > the internet in Ruby?
> > 
> > Apologies for necro threading but let my answer help other people coming  
> > to this link via google.
> > 
> > if system("ping google.com")  
> > puts "Connected to the internet."  
> > else puts "No internet connection."  
> > end
> 
> The code is not completely wrong but in real case scenarios is impractical.
> 
> First off you need to issue a command that will send 1 ping request (or at least a finite number), otherwise  
> the process will never end. So you should use something like 'ping -c 1 some.host'.
> 
> Second, you need to somehow handle kernel#system output which is not \*easy\*. Either you have to redirect the output to /dev/null in which scenario  
> you might better of using bash instead of ruby all together, or use a gem to handle everything for you and return just true/false:  
> --  
> #!/usr/bin/env ruby
> 
> require 'net/ping'
> 
> def up?(host)  
> &nbsp;&nbsp;&nbsp;&nbsp;check = Net::Ping::TCP.new(host)  
> &nbsp;&nbsp;&nbsp;&nbsp;check.ping  
> end
> 
> chost = '10.0.0.1'  
> puts up?(chost) # returns true if ping replies  
> --
> 
> Generally speaking though, it's better to use other approaches like backticks or open3[1] which are considered more 'secure' and offer more options, although kinda hard to grasp at first.
> 
> [1] [Module: Open3 (Ruby 1.9.3)](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open3/rdoc/Open3.html)
> 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> Panagiotis (atmosx) Atmatzidis
> 
> email: atma@convalesco.org  
> URL: [http://www.convalesco.org](http://www.convalesco.org)  
> GnuPG ID: 0x1A7BFEC5  
> gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5  
> --  
> The wise man said: "Never argue with an idiot. They bring you down to their level and beat you with experience."

---

<div class="post-metadata">

**Author:** ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)\
**Post date:** [15 February 2014 21:06 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/10 "2014-02-15T21:06:22Z")

</div>

A more OS-independent solution would e.g. be using the net-ping gem:

&nbsp;&nbsp;require 'net/ping'

&nbsp;&nbsp;host = 'ruby-lang.org'  
&nbsp;&nbsp;Net::Ping::External.new(host).ping # =\> true

Regards,  
Marcus

> **···**
>
> Am 15.02.2014 01:36, schrieb cynic limbu:
> 
> > Cory Cory wrote in post #647411:
> > 
> > > Does anyone know a way to test to see if the computer is connected to  
> > > the internet in Ruby?
> > 
> > Apologies for necro threading but let my answer help other people coming  
> > to this link via google.
> > 
> > if system("ping google.com")  
> > puts "Connected to the internet."  
> > else puts "No internet connection."  
> > end
> 
> --  
> GitHub: [stomar (Marcus Stollsteimer) · GitHub](https://github.com/stomar/)  
> PGP: 0x6B3A101A

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [17 March 2008 15:14 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/11 "2008-03-17T15:14:22Z")

</div>

require 'resolv-replace'  
might help, or google for ruby asynchronous dns  
I know rev has one, and also there is a package for eventmachine that  
does it.

The problem is that when it does DNS lookup it hangs forever (I'd  
imagine) waiting for a response.

This might help it.

loop do  
break if system("ping 64.233.187.99")  
sleep 1  
end  
puts "Internet working!"

GL.  
-R

Cory Cory wrote:

> **···**
>
> > Roger Pack wrote:
> > 
> > > I usually do something like this on my machine
> > 
> > Even that caused Ruby to hang.  
> > I am using Ubuntu.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Avdi\_Grimm1](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@Avdi\_Grimm1](https://rubytalk.org/u/Avdi_Grimm1)\
**Post date:** [17 March 2008 15:25 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/12 "2008-03-17T15:25:43Z")

</div>

AFAIK the "right" way to test for a network connection on Ubuntu is to  
use the NetworkManager D-Bus interface. I believe there is a D-Bus  
library for Ruby. A quick google for the NetworkManager D-Bus API  
yields this: [http://people.redhat.com/dcbw/NetworkManager/NetworkManager%20DBUS%20API.txt](http://people.redhat.com/dcbw/NetworkManager/NetworkManager%20DBUS%20API.txt)

> **···**
>
> On Mon, Mar 17, 2008 at 11:08 AM, Cory Cory \<cg821105@ohio.edu\> wrote:
> 
> > I am using Ubuntu.
> 
> --  
> Avdi

---

<div class="post-metadata">

**Author:** ![Cory\_Cory](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@Cory\_Cory](https://rubytalk.org/u/Cory_Cory)\
**Post date:** [17 March 2008 16:45 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/13 "2008-03-17T16:45:21Z")

</div>

> use the NetworkManager D-Bus interface. I believe there is a D-Bus  
> library for Ruby. A quick google for the NetworkManager D-Bus API  
> yields this:  
> [http://people.redhat.com/dcbw/NetworkManager/NetworkManager%20DBUS%20API.txt](http://people.redhat.com/dcbw/NetworkManager/NetworkManager%20DBUS%20API.txt)

The ruby-dbus package is still being developed. Most importantly, it is  
not available in any deb package list. This means I cannot write code  
that would be very portable with this.

Any other suggestions?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Avdi\_Grimm1](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@Avdi\_Grimm1](https://rubytalk.org/u/Avdi_Grimm1)\
**Post date:** [17 March 2008 17:35 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/14 "2008-03-17T17:35:01Z")

</div>

> The ruby-dbus package is still being developed.

This describes nearly every Ruby library in existence.

> Most importantly, it is  
> not available in any deb package list. This means I cannot write code  
> that would be very portable with this.

&nbsp;&nbsp;sudo gem install dbus

If you are depending on only libraries that are deb-packaged, your  
code will neither be portable (to non-deb linuxen) nor will you be  
able to take advantage of many Ruby libraries (since very few are deb  
packaged).

> **···**
>
> On Mon, Mar 17, 2008 at 12:45 PM, Cory Cory \<cg821105@ohio.edu\> wrote:
> 
> --  
> Avdi

---

<div class="post-metadata">

**Author:** ![Mikel\_Lindsaar1](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@Mikel\_Lindsaar1](https://rubytalk.org/u/Mikel_Lindsaar1)\
**Post date:** [19 March 2008 02:22 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/15 "2008-03-19T02:22:05Z")

</div>

Grab the gem onto your box, after check the license of the gem, copy  
the core code you need into your projects lib/ folder in it's own file  
then use it as part of your own package.

That way as well you can help fix / improve / finalize the gem and deb  
package your own code all you want

Win win.

Mikel

> **···**
>
> On Tue, Mar 18, 2008 at 3:45 AM, Cory Cory \<cg821105@ohio.edu\> wrote:
> 
> > \> [http://people.redhat.com/dcbw/NetworkManager/NetworkManager%20DBUS%20API.txt](http://people.redhat.com/dcbw/NetworkManager/NetworkManager%20DBUS%20API.txt)
> > 
> > The ruby-dbus package is still being developed. Most importantly, it is  
> > not available in any deb package list. This means I cannot write code  
> > that would be very portable with this.
> > 
> > Any other suggestions?

---

<div class="post-metadata">

**Author:** ![Cory\_Cory](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@Cory\_Cory](https://rubytalk.org/u/Cory_Cory)\
**Post date:** [18 March 2008 20:01 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/16 "2008-03-18T20:01:18Z")

</div>

Avdi Grimm wrote:

> > The ruby-dbus package is still being developed.
> 
> This describes nearly every Ruby library in existence.

I was referring to the fact that ruby-dbus is still in a pre-beta  
release form.

> &nbsp;&nbsp;sudo gem install dbus

Are you suggesting that I should just write a C function to handle  
internet detection? That seems like a clean answer, but C in Ruby is  
something I still have yet to learn. Seems like a good time to start.

> **···**
>
> > On Mon, Mar 17, 2008 at 12:45 PM, Cory Cory \<cg821105@ohio.edu\> wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Avdi\_Grimm1](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@Avdi\_Grimm1](https://rubytalk.org/u/Avdi_Grimm1)\
**Post date:** [18 March 2008 20:20 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/17 "2008-03-18T20:20:18Z")

</div>

No, I'm suggesting you use the existing dbus gem as-is unless there  
you have actually tested and verified that it doesn't do what you  
need.

> **···**
>
> On Tue, Mar 18, 2008 at 4:01 PM, Cory Cory \<cg821105@ohio.edu\> wrote:
> 
> > \> sudo gem install dbus
> 
> --  
> Avdi

---

<div class="post-metadata">

**Author:** ![Cory\_Cory](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@Cory\_Cory](https://rubytalk.org/u/Cory_Cory)\
**Post date:** [21 March 2008 14:43 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/18 "2008-03-21T14:43:30Z")

</div>

> &nbsp;&nbsp;sudo gem install dbus

Well after all of that, I can't compile the dbus gem unless I download  
and compile the newest version of the real dbus (because the deb is  
still out of date). Interfering with my deb's is not something I like  
doing because of updates, so I guess I'll just have to wait.

Maybe Hardy Heron will solve my problems.

If Ruby just had system threading this would not even be an issue. Once  
again, I guess I will just have to wait.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Lionel\_Bouton](https://avatars.discourse-cdn.com/v4/letter/l/a88e4f/32.png) [@Lionel\_Bouton](https://rubytalk.org/u/Lionel_Bouton)\
**Post date:** [21 March 2008 15:04 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/19 "2008-03-21T15:04:52Z")

</div>

Cory Cory wrote:

> > &nbsp;&nbsp;sudo gem install dbus
> 
> Well after all of that, I can't compile the dbus gem unless I download and compile the newest version of the real dbus (because the deb is still out of date). Interfering with my deb's is not something I like doing because of updates, so I guess I'll just have to wait.
> 
> Maybe Hardy Heron will solve my problems.
> 
> If Ruby just had system threading this would not even be an issue.

Your problem isn't clear enough for a good advice : do you need an answer to the "Is net available?" in finite time or do you want to continue processing while you wait for the answer?

If you want to continue processing, your only need is a non-blocking communication channel with another process/thread. The usual solution is to use an event loop, in this case something like popen with the ping program or whatever you need would give you the asynchronous behaviour you need.

I've a strong opinion on threads (built on several projects where avoiding deadlocks between them took most of my time instead of adding functionnalities): if you can avoid them (don't need the power of several CPU cores and even then multi-process is an option), go for the event-loop. In Ruby's case, where multi-threading doesn't use multiple cores, I only use threads in the simplest conditions (fetching several URIs in parallel with no interlocking for example).

If you only need an answer in finite time, see man ping on your OS.

Lionel

---

<div class="post-metadata">

**Author:** ![Felix\_Windt](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@Felix\_Windt](https://rubytalk.org/u/Felix_Windt)\
**Post date:** [21 March 2008 17:30 UTC](https://rubytalk.org/t/test-for-internet-connection/45238/20 "2008-03-21T17:30:21Z")

</div>

I'd just like to add to this thread that regardless of locking issues,  
pinging may not be the definitive test for whether or not you have internet  
connectivity. Just that you cannot ping doesn't mean you can't build TCP  
circuits to a port 80, and conversely just being able to ping doesn't mean  
you're not behind some evilly or otherwise misconfigured firewall that will  
drop all you traffic to destination port 80. In short, you may be getting  
false positives as well as false negatives, and testing for internet  
connectivity should involve testing what you actually intend to do. If you  
application needs to fetch something via TCP from destination port 80, do  
that.

Felix

> **···**
>
> On Fri, Mar 21, 2008 at 8:04 AM, Lionel Bouton \< lionel-subscription@bouton.name\> wrote:
> 
> > Cory Cory wrote:  
> > \>\> sudo gem install dbus  
> > \>  
> > \> Well after all of that, I can't compile the dbus gem unless I download  
> > \> and compile the newest version of the real dbus (because the deb is  
> > \> still out of date). Interfering with my deb's is not something I like  
> > \> doing because of updates, so I guess I'll just have to wait.  
> > \>  
> > \> Maybe Hardy Heron will solve my problems.  
> > \>  
> > \> If Ruby just had system threading this would not even be an issue.
> > 
> > Your problem isn't clear enough for a good advice : do you need an  
> > answer to the "Is net available?" in finite time or do you want to  
> > continue processing while you wait for the answer?
> > 
> > If you want to continue processing, your only need is a non-blocking  
> > communication channel with another process/thread. The usual solution is  
> > to use an event loop, in this case something like popen with the ping  
> > program or whatever you need would give you the asynchronous behaviour  
> > you need.
> > 
> > I've a strong opinion on threads (built on several projects where  
> > avoiding deadlocks between them took most of my time instead of adding  
> > functionnalities): if you can avoid them (don't need the power of  
> > several CPU cores and even then multi-process is an option), go for the  
> > event-loop. In Ruby's case, where multi-threading doesn't use multiple  
> > cores, I only use threads in the simplest conditions (fetching several  
> > URIs in parallel with no interlocking for example).
> > 
> > If you only need an answer in finite time, see man ping on your OS.
> > 
> > Lionel
