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

···

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

Candidates comes back empty. :-/

···

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

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

works.

J.

···

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

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!

<snip>

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, ara.t.howard <ara.t.howard@gmail.com> wrote:
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

Works on windows xp!

···

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

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!

Did you join back the regexp line?

J.

···

On 7/3/07, Gregory Brown <gregory.t.brown@gmail.com> wrote:

On 7/3/07, ara.t.howard <ara.t.howard@gmail.com> wrote:
>
> can someone run this on windows and let me know if it works?

Candidates comes back empty. :-/

<snip>

thanks!

-a
--

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

···

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

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

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

> <snip>
>>
>> thanks!
>>
>> -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).

-A

···

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

On Jul 3, 2007, at 9:52 AM, Robert Dober wrote:
> 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<(?: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

$ 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

···

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<(?: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?

> 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

···

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

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

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

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, Alex LeDonne <aledonne.listmail@gmail.com> wrote:

On 7/3/07, ara.t.howard <ara.t.howard@gmail.com> wrote:
>
> On Jul 3, 2007, at 9:52 AM, Robert Dober wrote:
>
> > On 7/3/07, ara.t.howard <ara.t.howard@gmail.com> wrote:
> > <snip>
> >>
> >> thanks!
> >>
> >> -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).

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

>
> > <snip>
> >>
> >> thanks!
> >>
> >> -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).

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

···

On 7/3/07, Alex LeDonne <aledonne.listmail@gmail.com> wrote:

On 7/3/07, ara.t.howard <ara.t.howard@gmail.com> wrote:
> On Jul 3, 2007, at 9:52 AM, Robert Dober wrote:
> > On 7/3/07, ara.t.howard <ara.t.howard@gmail.com> wrote:
>
> -a

--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

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 Jul 3, 2007, at 12:28 PM, Jeff Barczewski 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.

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

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:13:in `mac_address': ifconfig failed (RuntimeError)
    from testara.rb:23

···

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

On Jul 3, 2007, at 12:28 PM, Jeff Barczewski 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

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

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

···

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

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

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

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}

···

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

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

$ 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 <ara.t.howard@gmail.com> wrote:

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.

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

Robert

···

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

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

--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

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

-A

···

On 7/3/07, Alex LeDonne <aledonne.listmail@gmail.com> wrote:

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

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 Jul 3, 2007, at 1:08 PM, Jeff Barczewski 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:in `mac_address'
   from testara.rb:8:in `mac_address'
   from testara.rb:23

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