James Britt
12/28/2004 3:57:00 PM
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