Mac addr determination

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

···

On Jul 3, 2007, at 1:13 PM, Tim Pease wrote:

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

                                    #
                                    # 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
--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

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/

yes, that's a winner.

works on win xp
works on gentoo linux

:slight_smile:

···

On 7/3/07, ara.t.howard <ara.t.howard@gmail.com> wrote:

On Jul 3, 2007, at 1:13 PM, Tim Pease wrote:

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

??

--
Jeff Barczewski, MasterView core team
Inspired Horizons Ruby on Rails Training and Consultancy
http://inspiredhorizons.com/

This works fine on unmodified FreeBSD 6.2

···

On 7/3/07, ara.t.howard <ara.t.howard@gmail.com> wrote:

     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
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)

···

On Jul 3, 2007, at 12:31 PM, ara.t.howard wrote:

On Jul 3, 2007, at 1:13 PM, Tim Pease wrote:

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

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

···

On Jul 3, 2007, at 1:36 PM, Jeff Barczewski wrote:

yes, that's a winner.

works on win xp
works on gentoo linux

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

[snip]

     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

:slight_smile:

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:

--
Jos Backus
jos at catnook.com

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.

thanks everyone for the help!

-a

···

On Jul 3, 2007, at 2:13 PM, Ezra Zygmuntowicz wrote:

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

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

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
Rob@AgileConsultingLLC.com

···

On Jul 3, 2007, at 3:46 PM, ara.t.howard wrote:

On Jul 3, 2007, at 1:36 PM, Jeff Barczewski wrote:

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
--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

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.

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.

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

···

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