From: Sea&Gull Date: 2004-12-30T01:01:49+09:00 Subject: Re: verifying a network connection 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 > >