mac addr determination

A

ara.t.howard

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
 
K

Karl von Laudermann

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!

Works for me. Though I have two network cards, and it returns the MAC
address of the first one only, which may or may not be the one you
want.
 
J

Jano Svitok

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!

works.

J.
 
R

Robert Dober

On 7/3/07 said:
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
 
A

ara.t.howard

Nope on Linux

huh. does for me!?
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
--

can someone give this version a whirl on windows?

-a
 
A

Alex LeDonne

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

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

can someone give this version a whirl on windows?

-a
 
T

Tim Pease

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.

Blessings,
TwP
 
A

ara.t.howard

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
 
A

ara.t.howard

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
 
A

Alex LeDonne

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}
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
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
 
T

Tim Pease

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}

# fix small typo
stdout = IO.popen(cmd){|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
 
T

Tim Pease

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.

$ 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
 
A

ara.t.howard

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


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
 
R

Robert Dober

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

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

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

Cheers
Robert
 
A

ara.t.howard

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

??



-a
 
T

Todd Benson

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 works fine on unmodified FreeBSD 6.2
 
A

ara.t.howard

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top