Get MAC Address
-
-
@pgarmyn said:
:thumb: :thumb: :thumb: @driven
Well, maybe not three thumbs... it's unclear to me if that's a standard or an xtool instal, so a safer bet would be
getMACADDRESS = %x(/usr/sbin/networksetup -getMACADDRESS en0 | /usr/bin/awk '{print $3}' | /usr/bin/sed s/;//g)
which returns
001ec*******
the mac MAC address using standard instal items...
john
-
There IS a gem that can be used for reference (perhaps it can be made SketchUp friendly.)
https://github.com/ahoward/macaddr/blob/master/lib/macaddr.rb
-
I'm back at this MAC address - specifically for first Ethernet device.
This code seems to work fine for Windows 7 - but will it work for OSX ??
Has anyone tried this in a French computer or some other language?
` def get_mac_address()
return @mac_address if defined?( @mac_address )@win = ( RUBY_PLATFORM =~ /darwin/i ) == nil
tmp = ""
str = ""
first = trueif ( @win )
# for windows put results in array - splitting on newline
ipa = %x[ipconfig /all].split("\n")# check begining of string for 'Ethernet' and set flag once found # then check for 'Physical Address' and look at everything after the ':' # set mac_address and return ipa.each {|line| tmp = line.strip str = tmp[0..7] if ( first ) first = false if ( "Ethernet" == str ) elsif ( "Physical" == str ) pos = tmp.rindex( ":" ) @mac_address = tmp[pos+2, tmp.length ] return @mac_address end }
else
tmp = %x[ifconfig eth0].strip
pos = tmp.rindex( " " )
@mac_address = tmp[pos+1, tmp.length ]
return @mac_address
endreturn ""
end` -
@Garry
As i said before, this only works for US or EN OS.
The output of ipconfig depends on the OS language.
In French you will have to scan for "adresse physique" and for the other languages.... -
This works in Windows 7
def get_mac_address() return @@mac_address if defined?( @@mac_address ) if ( ( RUBY_PLATFORM =~ /darwin/i ) == nil ) cmd = 'ipconfig /all' else cmd = 'ifconfig' end macs = [] lines = %x[#{cmd}].split("\n").grep( /^.*\s([0-9a-f|A-F]{2}[:-]){5}([0-9a-f|A-F]{2})$/ ) lines.each{|line| macs << line.strip[-17,17] } @@mac_address = macs.first.gsub( '-', ':' ) return @@mac_address end
-
Thanks pgarmyn - I was just confirming the language thing
I did try running the code that you posted earlier - but it gave me an error so I decided this was a good opportunity to learn more.I have cleaned this up so if it is benefit to others
` def get_mac_address()
return @@mac_address if defined?( @@mac_address )mac_addr = [] cmd = @@windows ? 'ipconfig /all' : 'ifconfig' # choose lines that have only six pairs of 2 char hex values separated by either a : or - # pattern must start with a space and be at the end of the line # there can be white space at the end of the line lines = %x[#{cmd}].split("\n").grep( /\s([0-9A-F]{2}[-:]){5}([0-9A-F]{2})\s*$/ ) lines.each{|line| mac_addr << (line.strip[-17,17]).gsub( /-/, ':' ) } # if windows and we have more than 1 mac address use the second one otherwise use the first # if not windows then use the first address index = @@windows && mac_addr.length > 1 ? 1 : 0 @@mac_address = mac_addr[index] return @@mac_address
end`
-
@gary
couple of changes needed to run on my mac,
got rid of class variables
addedwindows =
added simplifiedregex =
with..
forgrep
def get_mac_address() windows = ( ( RUBY_PLATFORM =~ /darwin/i ) == nil ) return mac_address if defined?( mac_address ) mac_addr = [] cmd = windows ? 'ipconfig /all' ; 'ifconfig' # choose lines that have only six pairs of 2 char hex values separated by either a ; or - # pattern must start with a space and be at the end of the line # there can be white space at the end of the line regex = Regexp.compile("(..[;-]){5}..") lines = %x[#{cmd}].split("\n").grep( regex ) lines.each{|line| mac_addr << (line.strip[-17,17]).gsub( /-/, ';' ) } # if windows and we have more than 1 mac address use the second one otherwise use the first # if not windows then use the first address index = windows && mac_addr.length > 1 ? 1 ; 0 mac_address = mac_addr[index] return mac_address end
> get_mac_address 00:1e:c4:16:08:46
-
Thank-you very much, I appreciate this, since I don't have access to a mac.
I will test on Windows with your simplified regex.You show the mac_address as lower case - can you confirm that is indeed the output.
Also - off topic - but how do you get the source code to go into the embedded window? -
@garry k said:
You show the mac_address as lower case - can you confirm that is indeed the output.
yes it's lower-case, I realised that obscuring it in my other posts was counter productive, so I just adjusted only the number values to show the return this time...
@unknownuser said:Also - off topic - but how do you get the source code to go into the embedded window?
-
Thanks again. I checked the code for windows and it works fine.
I've added an upcase on the following linelines.each{|line| mac_addr << (line.strip[-17,17]).upcase().gsub( /-/, ':' ) }
-
Hello
This works fine in Sketchup 2013 but not in 2014.
Any tips?
-
@jaimeda said:
This works fine in Sketchup 2013 but not in 2014.
Due to the upgrade of the Ruby core this broke. We haven't been able to figure out why. But the workaround is to pipe the result from the command to a temporary file and read it after executing the command.
<span class="syntaxdefault"><br />tempfile </span><span class="syntaxkeyword">= </span><span class="syntaxstring">"C;/Users/Thomas/Desktop/temp.txt"<br /></span><span class="syntaxkeyword">`</span><span class="syntaxstring">ipconfig /all > #{tempfile}</span><span class="syntaxkeyword">`<br /></span><span class="syntaxdefault">puts File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">read</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">tempfile</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault"></span>
-
Thanks Thomas.
It works perfectly.
-
@tt_su said:
@jaimeda said:
This works fine in Sketchup 2013 but not in 2014.
Due to the upgrade of the Ruby core this broke. We haven't been able to figure out why. But the workaround is to pipe the result from the command to a temporary file and read it after executing the command.
<span class="syntaxdefault"><br />tempfile </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">"C;/Users/Thomas/Desktop/temp.txt"<br /></span><span class="syntaxkeyword">`</span><span class="syntaxstring">ipconfig /all > #{tempfile}</span><span class="syntaxkeyword">`<br /></span><span class="syntaxdefault">puts File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">read</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">tempfile</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span>
In my C++ programming for windows I would create a temp file this way
tempfp = tmpfile();
Is there anything in Sketchup that would do the same for both Windows and Mac?
-
Well,... since we now have the Ruby Standard Library available: http://ruby-doc.org/stdlib-2.0.0/libdoc/tempfile/rdoc/Tempfile.html
So an updated example would be:
<span class="syntaxdefault"><br /></span><span class="syntaxkeyword">require </span><span class="syntaxstring">'tempfile'<br /></span><span class="syntaxdefault">file </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Tempfile</span><span class="syntaxkeyword">.new(</span><span class="syntaxstring">'my_prefix'</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">file</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">close<br /></span><span class="syntaxkeyword">`</span><span class="syntaxstring">ipconfig /all > #{file.path}</span><span class="syntaxkeyword">`<br /></span><span class="syntaxdefault">puts File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">read</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">file</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">path</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">file</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">unlink<br /></span>
-
Weirdly the backquotes, &
%x
strings which use theAnd I verified that I do not have the backquote_patch.rb (that I proposed,) loaded.
-
We have seen that if a user drags a skp model onto their SU icon, then they work. but if they double click on a skp model file, or double click on the SU icon, then it does not work. Are you seeing it work in both scenarios?
-
@tt_su said:
Well,... since we now have the Ruby Standard Library available: http://ruby-doc.org/stdlib-2.0.0/libdoc/tempfile/rdoc/Tempfile.html
So an updated example would be:
<span class="syntaxdefault"><br />require </span><span class="syntaxstring">'tempfile'<br /></span><span class="syntaxdefault">file </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Tempfile</span><span class="syntaxkeyword">.new(</span><span class="syntaxstring">'my_prefix'</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">file</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">close<br /></span><span class="syntaxkeyword">`</span><span class="syntaxstring">ipconfig /all > #{file.path}</span><span class="syntaxkeyword">`<br /></span><span class="syntaxdefault">puts File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">read</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">file</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">path</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">file</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">unlink<br /></span>
This is great for 2014 and forward - but what if you want to support earlier versions.
I guess we have to either use a one size fits all strategy or check the version an branch appropriately.
-
There are whole topic threads on using the TEMP directory.
No problem on Mac OSX...
The issue on PC is unicode characters in usernames, which cause Ruby 1.8.x to choke.
So some coders create a TEMP subdir of THEIR specific plugin's folder.
(I think this is better than creating any temp folders in the root dir of the system drive.)
Advertisement