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|
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
···
--
Jeff Barczewski, MasterView core team
Inspired Horizons Ruby on Rails Training and Consultancy http://inspiredhorizons.com/
lines = nil
cmds.each do |cmd|
stdout = IO.popen("#{ cmd } 2> #{ null }"){|fd|
fd.readlines} rescue next
#
# ok on windoze?
next unless stdout and stdout.size > 0
lines = stdout and break
end
raise "all of #{ cmds.join ' ' } 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
??
--
Jeff Barczewski, MasterView core team
Inspired Horizons Ruby on Rails Training and Consultancy http://inspiredhorizons.com/
lines = nil
cmds.each do |cmd|
stdout = IO.popen("#{ cmd } 2> #{ null }"){|fd|
fd.readlines} rescue next
#
# ok on windoze?
next unless stdout and stdout.size > 0
lines = stdout and break
end
raise "all of #{ cmds.join ' ' } 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
lines = nil
cmds.each do |cmd|
stdout = IO.popen("#{ cmd } 2> #{ null }"){|fd| fd.readlines} rescue next
#
# ok on windoze?
next unless stdout and stdout.size > 0
lines = stdout and break
end
raise "all of #{ cmds.join ' ' } 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
lines = nil
cmds.each do |cmd|
stdout = IO.popen("#{ cmd } 2> #{ null }"){|fd|
fd.readlines} rescue next
#
# ok on windoze?
next unless stdout and stdout.size > 0
lines = stdout and break
end
raise "all of #{ cmds.join ' ' } 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
??
yes, that's a winner.
works on win xp
works on gentoo linux
Fwiw, works on FreeBSD, too.
···
On Wed, Jul 04, 2007 at 04:36:47AM +0900, Jeff Barczewski wrote:
On 7/3/07, ara.t.howard <ara.t.howard@gmail.com> wrote:
Well, it works on Mac OS X 10.4.10 except that it tells me the mac of the en0 (which isn't being used, nothing's plugged in!) rather than the en1 of the wireless adapter (which is active).
Hmm it would help to know what this is for, as Rob pointed out
inactive interfaces might cause problems.
Given the verbose nature of ipconfig /all you will have problems with
hostnames like this
AB-AD-BA...
at least, please, get rid of A-Z in your Regexpression.
Just some thoughts
R
···
On 7/3/07, ara.t.howard <ara.t.howard@gmail.com> wrote:
On Jul 3, 2007, at 2:13 PM, Ezra Zygmuntowicz wrote:
> This one works for me on OSX, Gentoo and windows.
the current invocation is ok for my purposes: generating a key for encryption as
"--#{ hostname }--#{ mac_address }--"
but note i added a handle to the complete list to people could monkey with it if needed.
cheers.
-a
···
On Jul 3, 2007, at 3:02 PM, Robert Dober wrote:
Hmm it would help to know what this is for, as Rob pointed out
inactive interfaces might cause problems.
Given the verbose nature of ipconfig /all you will have problems with
hostnames like this
AB-AD-BA...
at least, please, get rid of A-Z in your Regexpression.
Just some thoughts
--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama