[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

verifying a network connection

Thomas Metz

12/28/2004 1:15:00 PM

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
12 Answers

Joao Pedrosa

12/28/2004 1:28:00 PM

0

Hi,

On Tue, 28 Dec 2004 22:16:49 +0900, Thomas Metz <metz@gmx.net> 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.
>
> 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.

http://www.ru...

Cheers,
Joao


Zach Dennis

12/28/2004 1:48:00 PM

0

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






James Britt

12/28/2004 3:57:00 PM

0

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


Premshree Pillai

12/28/2004 4:07:00 PM

0

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...))
# 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
http://www.livejournal.com/...


Thomas Metz

12/29/2004 10:05:00 AM

0

Thank you all for your help.

Thomas

Sea&Gull

12/29/2004 3:57:00 PM

0

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....
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
>
>

Josef 'Jupp' Schugt

12/29/2004 11:00:00 PM

0

Premshree Pillai wrote:

> require 'net/http'
> begin
> resp = Net::HTTP.get_response(URI.parse("http://some...))
> # 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?



Thomas Metz

12/30/2004 9:57:00 AM

0

Josef 'Jupp' Schugt wrote:
> Premshree Pillai wrote:
>
>> require 'net/http'
>> begin
>> resp = Net::HTTP.get_response(URI.parse("http://some...))
>> # 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

Premshree Pillai

12/30/2004 12:28:00 PM

0

On Thu, 30 Dec 2004 19:11:45 +0900, Thomas Metz <metz@gmx.net> wrote:
> Josef 'Jupp' Schugt wrote:
> > Premshree Pillai wrote:
> >
> >> require 'net/http'
> >> begin
> >> resp = Net::HTTP.get_response(URI.parse("http://some...))
> >> # 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. :D

>
> Thank you all for your suggestions.
>
> Thomas
>
>


--
Premshree Pillai
http://www.livejournal.com/...


Josef 'Jupp' Schugt

1/1/2005 9:27:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Premshree Pillai wrote:
| I'm psycic -- I can predict questions. :D

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?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail....

iD8DBQFB1dMZrhv7B2zGV08RAh8PAJ47DX74htvoXxhAzBtia2qlgrqg+ACeLhew
j7pOZ1F79R6XfzjjqeyDzbw=
=oerv
-----END PGP SIGNATURE-----