From: Premshree Pillai Date: 2004-12-29T01:07:01+09:00 Subject: Re: verifying a network connection 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 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/~premshree