mac addr determination

J

Jos Backus

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'

null = test(?e, '/dev/null') ? '/dev/null' : 'NUL'

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.
 
E

Ezra Zygmuntowicz

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


will this work on windoze?

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'

null = test(?e, '/dev/null') ? '/dev/null' : 'NUL'

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

This one works for me on OSX, Gentoo and windows.

Cheers-
-- Ezra Zygmuntowicz
-- Lead Rails Evangelist
-- (e-mail address removed)
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)
 
R

Rob Biedenharn

yes, that's a winner.

works on win xp
works on gentoo linux

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'

null = test(?e, '/dev/null') ? '/dev/null' : 'NUL'

lines = nil
cmds.each do |cmd|
stdout = IO.popen("#{ cmd } 2> #{ null }"){|fd|
fd.readlines} rescue next
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

-a

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).

Is that what you're expecting?

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
R

Robert Dober

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
Do you really want to keep A-Za-z ??? What about a-f.../oi ?
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} lines.find or lines.detect
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

Robert
 
R

Robert Dober

okay, here it is

http://drawohara.tumblr.com/post/4862735

if someone breaks it on a reasonable system send a patch and i'll
update.

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
 
A

ara.t.howard

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

http://drawohara.tumblr.com/post/4862735

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top