Mac addr determination

can someone run this on windows and let me know if it works?

 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

thanks!

-a

On 7/3/07, ara.t.howard [email protected] wrote:

can someone run this on windows and let me know if it works?

Candidates comes back empty. :-/

On 7/3/07, ara.t.howard [email protected] wrote:

     rescue
       IO.popen('ipconfig /all'){|fd| fd.readlines}
     end
   candidates = lines.select{|line| line =~ re}
   @mac_address = candidates.first[re].strip
 end

thanks!

works.

J.

On 7/3/07, ara.t.howard [email protected] wrote:

thanks!

-a

Nope on Linux

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

but not tested on Windows
HTH
Robert

On 7/3/07, Gregory B. [email protected] wrote:

On 7/3/07, ara.t.howard [email protected] wrote:

can someone run this on windows and let me know if it works?

Candidates comes back empty. :-/

Did you join back the regexp line?

J.

On 7/3/07, ara.t.howard [email protected] wrote:

       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

thanks!

Works on windows xp!

On 7/3/07, ara.t.howard [email protected] wrote:

Nope on Linux

huh. does for me!?

It seems to assume ifconfig is in the $PATH, which on many distros is
not the normal state of affairs for a non-root user (ifconfig tends to
live in /sbin, I think).

-A

On Jul 3, 2007, at 9:52 AM, Robert D. wrote:

On 7/3/07, ara.t.howard [email protected] wrote:

thanks!

-a

Nope on Linux

huh. does for me!?

 candidates = lines.map{|l| re.match( l )[1] rescue nil }.compact
 @mac_address = candidates.first

end

but not tested on Windows
HTH
Robert

can someone give this version a whirl on windows?

-a

On Jul 3, 2007, at 11:56 AM, Tim P. wrote:

   rescue

def mac_address
@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

On 7/3/07, ara.t.howard [email protected] wrote:

   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/^:-{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.

Blessings,
TwP

On Jul 3, 2007, at 12:28 PM, Jeff B. wrote:

Yes, it is not in my user path for gentoo linux either. It is in
my /sbin
directory which my non-admin users don’t normally have in their path.

latest version. can everyone test on various platforms and report
results and/or patch?

 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 = nil
cmds = ‘/sbin/ifconfig’, ‘/bin/ifconfig’, ‘ifconfig’,
‘ipconfig /all’

   cmds.each do |cmd|
     stdout = IO.popen('ifconfig'){|fd| fd.readlines}
     next unless stdout and stdout.size > 0
     lines = stdout and break
   end
   raise 'ifconfig failed' unless lines

   candidates = lines.select{|line| line =~ re}
   raise 'no mac address candidates' unless candidates.first

   maddr = candidates.first[re]
   raise 'no mac address found' unless maddr
   @mac_address = maddr.strip
 end

i promise to post this when we’re done so we all have it.

kind regards.

-a

On 7/3/07, ara.t.howard [email protected] wrote:

‘ipconfig /all’

   maddr = candidates.first[re]
   raise 'no mac address found' unless maddr
   @mac_address = maddr.strip
 end

On windows I get ifconfig not found exception

testara.rb:9:in popen': No such file or directory - ifconfig (Errno::ENOENT) from testara.rb :9:in mac_address’
from testara.rb:8:in `mac_address’
from testara.rb:23

On linux I get

testara.rb:23: command not found: ifconfig
testara.rb:23: command not found: ifconfig
testara.rb:23: command not found: ifconfig
testara.rb:23: command not found: ifconfig
testara.rb:13:in `mac_address’: ifconfig failed (RuntimeError)
from testara.rb:23

On 7/3/07, Alex LeDonne [email protected] wrote:

-a

Nope on Linux

huh. does for me!?

It seems to assume ifconfig is in the $PATH, which on many distros is
not the normal state of affairs for a non-root user (ifconfig tends to
live in /sbin, I think).

Yes, it is not in my user path for gentoo linux either. It is in my
/sbin
directory which my non-admin users don’t normally have in their path.

On 7/3/07, ara.t.howard [email protected] wrote:

‘ipconfig /all’

   cmds.each do |cmd|
     stdout = IO.popen('ifconfig'){|fd| fd.readlines}

fix small typo

stdout = IO.popen(cmd){|fd| fd.readlines}

On 7/3/07, ara.t.howard [email protected] wrote:

   cmds = '/sbin/ifconfig', '/bin/ifconfig', 'ifconfig',
   raise 'no mac address candidates' unless candidates.first

   maddr = candidates.first[re]
   raise 'no mac address found' unless maddr
   @mac_address = maddr.strip
 end

i promise to post this when we’re done so we all have it.

$ ruby --version
ruby 1.8.5 (2006-12-25 patchlevel 12) [i386-cygwin]

$ ruby t.rb
t.rb:22: command not found: /sbin/ifconfig
t.rb:22: command not found: /bin/ifconfig
t.rb:22: command not found: ifconfig
XX-XX-XX-XX-XX-XX

Seems to be working on windows (except the annoying “command not
found” messages).

Blessings,
TwP

On 7/3/07, ara.t.howard [email protected] wrote:

   cmds.each do |cmd|
     stdout = IO.popen('ifconfig'){|fd| fd.readlines}
      stdout = IO.popen(cmd){|fd| fd.readlines}
     next unless stdout and stdout.size > 0
     lines = stdout and break
   end
   raise 'ifconfig failed' unless lines
    raise "#{cmd} failed" unless lines

On 7/3/07, Alex LeDonne [email protected] wrote:

   raise 'ifconfig failed' unless lines
    raise "#{cmd} failed" unless lines

Never mind this one - my bad not testing. ):

-A

On Jul 3, 2007, at 1:08 PM, Jeff B. wrote:

On windows I get ifconfig not found exception

testara.rb:9:in popen': No such file or directory - ifconfig (Errno::ENOENT) from testara.rb :9:inmac_address’
from testara.rb:8:in `mac_address’
from testara.rb:23

argh. cut and paste error - one more time…

 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
cmds = ‘/sbin/ifconfig’, ‘/bin/ifconfig’, ‘ifconfig’,
‘ipconfig /all’

   lines = nil
   cmds.each do |cmd|
     stdout = IO.popen('ifconfig'){|fd| fd.readlines} rescue next
     next unless stdout and stdout.size > 0
     lines = stdout and break
   end
   raise "#{ cmd } failed" unless lines

   candidates = lines.select{|line| line =~ re}
   raise 'no mac address candidates' unless candidates.first

   maddr = candidates.first[re]
   raise 'no mac address found' unless maddr
   @mac_address = maddr.strip
 end

(man i have to get parallels installed…)

-a

On 7/3/07, Alex LeDonne [email protected] wrote:


Nope on Linux

huh. does for me!?

It seems to assume ifconfig is in the $PATH, which on many distros is
not the normal state of affairs for a non-root user (ifconfig tends to
live in /sbin, I think).
You are right and it might be a point to note for the code in general.

However I did not do something stupid for once ;), this is the output
which just does not match the original regexp ( I run as root and
stepped the original code through irb )
eth0 Link encap:Ethernet HWaddr 00:11:xx:xx:xx:xx

What distro are you running I tested on a Slax and a Debian Sarge?
Could you test my reg or give the line to match so that Ara can adapt
please.

     IO.popen('ifconfig'){|fd| fd.readlines}

can someone give this version a whirl on windows?
Home now I just run it, seems to work fine.

Cheers
Robert

def mac_address
return @mac_address if defined? @mac_address
re =
%r/^:-{5}[0-9A-Za-z][0-9A-Za-z][^:-]/o
lines = nil
cmds = ‘/sbin/ifconfig’, ‘/bin/ifconfig’, ‘ifconfig’,
‘ipconfig /all’

  cmds.each do |cmd|
    begin
      stdout = IO.popen(cmd){|fd| fd.readlines}
    rescue
      stdout = nil
    end
    next unless stdout and stdout.size > 0
    lines = stdout and break
  end
  raise "failed" unless lines

  candidates = lines.select{|line| line =~ re}
  raise 'no mac address candidates' unless candidates.first

  maddr = candidates.first[re]
  raise 'no mac address found' unless maddr
  @mac_address = maddr.strip
end

puts mac_address

Note: I needed to wrap with begin rescue to get it to work on windows

  • works on gentoo linux
  • works on windows