Verifying a network connection

Hi,

is there a way in Ruby to find out, whether my computer has built up a
network connection or not?

I'm using Windows2000.

Thank you for helping a Ruby-newbie.

Thomas

Hi,

Hi,

is there a way in Ruby to find out, whether my computer has built up a
network connection or not?

I'm using Windows2000.

Thank you for helping a Ruby-newbie.

Thomas

If you want to see if there is an Internet connection available try to
connect to some known site. At first I thought of the ipconfig command
but it's too low level. The simple connection to a known website will
tell you if there is an internet connection and if no firewall is
blocking the connection. There are many ways to do that with Ruby. The
online book has some info.

Cheers,
Joao

···

On Tue, 28 Dec 2004 22:16:49 +0900, Thomas Metz <metz@gmx.net> wrote:

Thomas Metz wrote:

Hi,

is there a way in Ruby to find out, whether my computer has built up a
network connection or not?

I'm using Windows2000.

Thank you for helping a Ruby-newbie.

Hi Thomas,

A quick and easy solution is the following, which work on a windows machine.

ip_str = `ipconfig`
ip_str =~ /IP Address[^\d]*((\d+\.){3}\d+)/
ip_addr = $1

Note that those are backticks around "ipconfig". I haven't tested this for a machine that doesn't have a ip address, so I'll leave that up to you.

You can shorten this by just saying:

`ipconfig` =~ /IP Address[^\d]*((\d+\.){3}\d+)/
ip_addr = $1

Here's a quick explanation (I dont know if you're new to ruby or to programming, so bare with me). The parts of the regex are in quotes:

  - `ipconfig` executes a shell command, aka ipconfig!
  - /IP Address[^\d]*((\d+\.){3}\d+)/ is a regular expression meaning:
  - Find the word IP Address, "IP Address"
  - which is followed by a non-digit as many times as possible "[^\d]*"
  - until we find one or more digits "\d+"
  - (which is followed by a period) at least 3 times "\.{3}"
  - and is followed by one or more digits "\d+"
  - The parenthesis's are used to group the match

HTH,

Zach

Thomas Metz wrote:

Hi,

is there a way in Ruby to find out, whether my computer has built up a
network connection or not?

I'm using Windows2000.

Thank you for helping a Ruby-newbie.

My laptop run win2k. I like to start the machine and go make coffee, and have the box launch a bunch of useful apps in the meantime.

If I have a network connection then I want a browser, a mail reader, a firewall, and a few other things running.

I have a script that tries to ping some reliable site; it simple shells out and regexes the results. Low-tech, but works quite well.

If it has a network connection, it reads in a config file wth a list of apps, and starts them up in sequence.

Some sample code:

$re_unk = Regexp.new( "Unknown", Regexp::MULTILINE )
$re_loss = Regexp.new( "(00% loss)", Regexp::MULTILINE )

def connected?( resp )
   return false if $re_unk =~ resp
   return false if $re_loss =~ resp
   true
end

resp = `#{ping w3.org}`

do_stuff() if connected?( resp )

James

Thank you all for your help.

Thomas

Interesting... but it seems unncessarily complicated.

You could do something simple like:

require 'net/http'
begin
  resp = Net::HTTP.get_response(URI.parse("http://some.url"))
  # do stuff
rescue
  print "No Network connection"
end

No?

···

On Tue, 28 Dec 2004 22:48:04 +0900, Zach Dennis <zdennis@mktec.com> wrote:

Thomas Metz wrote:
> Hi,
>
> is there a way in Ruby to find out, whether my computer has built up a
> network connection or not?
>
> I'm using Windows2000.
>
> Thank you for helping a Ruby-newbie.
>

Hi Thomas,

A quick and easy solution is the following, which work on a windows machine.

ip_str = `ipconfig`
ip_str =~ /IP Address[^\d]*((\d+\.){3}\d+)/
ip_addr = $1

Note that those are backticks around "ipconfig". I haven't tested this
for a machine that doesn't have a ip address, so I'll leave that up to you.

You can shorten this by just saying:

`ipconfig` =~ /IP Address[^\d]*((\d+\.){3}\d+)/
ip_addr = $1

Here's a quick explanation (I dont know if you're new to ruby or to
programming, so bare with me). The parts of the regex are in quotes:

  - `ipconfig` executes a shell command, aka ipconfig!
  - /IP Address[^\d]*((\d+\.){3}\d+)/ is a regular expression meaning:
        - Find the word IP Address, "IP Address"
        - which is followed by a non-digit as many times as possible "[^\d]*"
        - until we find one or more digits "\d+"
        - (which is followed by a period) at least 3 times "\.{3}"
        - and is followed by one or more digits "\d+"
        - The parenthesis's are used to group the match

HTH,

Zach

--
Premshree Pillai

James Britt wrote:

Thomas Metz wrote:

Hi,

is there a way in Ruby to find out, whether my computer has built up a
network connection or not?

I'm using Windows2000.

Thank you for helping a Ruby-newbie.

My laptop run win2k. I like to start the machine and go make coffee, and have the box launch a bunch of useful apps in the meantime.

If I have a network connection then I want a browser, a mail reader, a firewall, and a few other things running.

I have a script that tries to ping some reliable site; it simple shells out and regexes the results. Low-tech, but works quite well.

If it has a network connection, it reads in a config file wth a list of apps, and starts them up in sequence.

Also you may use "nncron", http://www.nncron.ru/
It is something like "cron" in *nix.
"nncron" has a possibility to handle "Network connection is on" event.

···

Some sample code:

$re_unk = Regexp.new( "Unknown", Regexp::MULTILINE )
$re_loss = Regexp.new( "(00% loss)", Regexp::MULTILINE )

def connected?( resp )
  return false if $re_unk =~ resp
  return false if $re_loss =~ resp
  true
end

resp = `#{ping w3.org}`

do_stuff() if connected?( resp )

James

Premshree Pillai wrote:

require 'net/http'
begin
  resp = Net::HTTP.get_response(URI.parse("http://some.url"))
  # do stuff
rescue
  print "No Network connection"
end

Your solution *may* solve the problem that Thomas has but it does not
fit the one he actually described.

According to what he wrote, Thomas wants to check that the given Win2k
system is connected to a network. Being connected to a network does not
require that one is connected to the Internet. It also does not require
that a given URL can be resolved, that a given webserver is reachable,
or that a given URL can be fetched. It only means physical access to the
network.

To be precise even the assumption that TCP (IP) is used needs further
justification - in the given case by mentioning that Win2k is used and
not mentioning IPX, NetBIOS, or other alternative protocols that can be
used for networking.

Josef 'Jupp' Schugt

···

--
Where on the ringworld does that 'Lord of the Rings' guy live?

Josef 'Jupp' Schugt wrote:

Premshree Pillai wrote:

require 'net/http'
begin
    resp = Net::HTTP.get_response(URI.parse("http://some.url"))
    # do stuff
rescue
    print "No Network connection"
end

Your solution *may* solve the problem that Thomas has but it does not
fit the one he actually described.

Yes, I described my problem not very precise, but Premshree's solution was exaxtly, what I was looking for.

Thank you all for your suggestions.

Thomas

Josef 'Jupp' Schugt wrote:
> Premshree Pillai wrote:
>
>> require 'net/http'
>> begin
>> resp = Net::HTTP.get_response(URI.parse("http://some.url"))
>> # do stuff
>> rescue
>> print "No Network connection"
>> end
>
>
> Your solution *may* solve the problem that Thomas has but it does not
> fit the one he actually described.

Yes, I described my problem not very precise, but Premshree's solution
was exaxtly, what I was looking for.

I'm psycic -- I can predict questions. :smiley:

···

On Thu, 30 Dec 2004 19:11:45 +0900, Thomas Metz <metz@gmx.net> wrote:

Thank you all for your suggestions.

Thomas

--
Premshree Pillai

Premshree Pillai wrote:

I'm psycic -- I can predict questions. :smiley:

Some illusionist (I cannot recall his name) will pay you 1M (USD IIRC)
if you mange to prove that in a scientific (double-blind) experiment.
The money is still available. Prove it and make a donation to the Ruby
community >;->

No that is *no* kidding. The guy simply is sure that nobody will ever
pass the test.

Josef 'Jupp' Schugt
- --
Where on the ringworld does that 'Lord of the Rings' guy live?

That'd be James Randi

martin

···

Josef 'Jupp' Schugt <jupp@gmx.de> wrote:

Premshree Pillai wrote:
> I'm psycic -- I can predict questions. :smiley:

Some illusionist (I cannot recall his name) will pay you 1M (USD IIRC)
if you mange to prove that in a scientific (double-blind) experiment.
The money is still available. Prove it and make a donation to the Ruby
community >;->S

Martin DeMello wrote:

That'd be James Randi

Sounds familiar.

Josef 'Jupp' Schugt
- --
Where on the ringworld does that 'Lord of the Rings' guy live?