[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

mac addr determination

ara.t.howard

7/3/2007 2:25:00 PM


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




26 Answers

Karl von Laudermann

7/3/2007 2:33:00 PM

0

On Jul 3, 10:25 am, "ara.t.howard" <ara.t.how...@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!

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.

Gregory Brown

7/3/2007 2:33:00 PM

0

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. :-/

Jano Svitok

7/3/2007 2:36:00 PM

0

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!

works.

J.

Jano Svitok

7/3/2007 2:37:00 PM

0

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. :-/

Did you join back the regexp line?

J.

Robert Dober

7/3/2007 3:52:00 PM

0

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

ara.t.howard

7/3/2007 5:41:00 PM

0


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

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




Alex LeDonne

7/3/2007 5:47:00 PM

0

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

-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

Tim Pease

7/3/2007 5:56:00 PM

0

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

$ 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

ara.t.howard

7/3/2007 6:23:00 PM

0


On Jul 3, 2007, at 11:56 AM, Tim Pease 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
>> >
>>
>> 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
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




ara.t.howard

7/3/2007 6:48:00 PM

0


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

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