From: "ara.t.howard" Date: 2007-07-04T03:23:29+09:00 Subject: Re: mac addr determination On Jul 3, 2007, at 11:56 AM, Tim Pease wrote: > On 7/3/07, ara.t.howard wrote: >> >> > def mac_address >> > return @mac_address if defined? @mac_address >> > re = %r<(?:hwaddr|:)\s+((?:[0-9a-f]{1,2}[-:]){5}[0-9a-f] >> {1,2}) >> > \s*$>i >> > lines = >> > begin >> > IO.popen('ifconfig'){|fd| fd.readlines} >> > rescue >> > IO.popen('ipconfig /all'){|fd| fd.readlines} >> > end >> > candidates = lines.map{|l| re.match( l )[1] rescue >> nil }.compact >> > @mac_address = candidates.first >> > end >> > >> >> can someone give this version a whirl on windows? >> > > $ ruby --version > ruby 1.8.5 (2006-12-25 patchlevel 12) [i386-cygwin] > > $ ruby t.rb > t.rb:14: command not found: ifconfig > t.rb:11:in `mac_address': undefined method `[]' for nil:NilClass > (NoMethodError) > from t.rb:14 > > $ cat t.rb > def mac_address > return @mac_address if defined? @mac_address > re = %r/[^:\-](?:[0-9A-Za-z][0-9A-Za-z][:\-]){5}[0-9A-Za-z][0-9A- > Za-z][^:\-]/o > lines = > begin > IO.popen('ifconfig'){|fd| fd.readlines} > rescue > IO.popen('ipconfig /all'){|fd| fd.readlines} > end > candidates = lines.select{|line| line =~ re} > @mac_address = candidates.first[re].strip > end > > puts mac_address > > > IO.popen, it appears, does not raise an exception when the ifconfig > command is not found. Strange. > forgot about that! check my open4 code to see the work around: an exception must be propogated back up the pipe! > Blessings, > TwP > -a -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama