• Login
sketchucation logo sketchucation
  • Login
๐Ÿค‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Get MAC Address

Scheduled Pinned Locked Moved Developers' Forum
55 Posts 8 Posters 4.9k Views 8 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    Garry K
    last edited by 28 Feb 2014, 17:57

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

    1 Reply Last reply Reply Quote 0
    • D Offline
      Dan Rathbun
      last edited by 28 Feb 2014, 18:06

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

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • D Offline
        Dan Rathbun
        last edited by 28 Feb 2014, 18:25

        @chris fullmer said:

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

        NO neither.

        I see now, I either have used a pinned shortcut copy (of the desktop icon,) on the taskbar, or a copy on the StartMenu, or from a popup menu of the Desktop taskbar.
        These are all single-click ways of launching SketchUp. I usually start SU this way because the Desktop is covered up.

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • J Offline
          jaimeda
          last edited by 1 Mar 2014, 10:24

          I think this works in 2014 and earlier

          <span class="syntaxdefault">IO</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">popen</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"ipconfig/all >temp.txt"</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">fd</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> fd</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">readlines</span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">lines </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> open</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"temp.txt"</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">readlines<br />puts</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">lines</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span>
          
          1 Reply Last reply Reply Quote 0
          • G Offline
            Garry K
            last edited by 2 Mar 2014, 16:43

            Does this code work with Mac OS.
            With windows the tempfile goes to desktop and gets discarded when finished.
            Where does the file go for Mac OS and do we have rights?

            
                  # do this for testing on a mac
                  @@windows = false 
            
                  # do this for testing on windows
                  # @@windows = false 
            
                  mac_addr = []
                  cmd = @@windows ? 'ipconfig /all' ; 'ifconfig'
            
                  # choose lines that have only six pairs of 2 char hex values 
                  # separated by either a ; or -
                  regex = Regexp.compile('(..[;-]){5}..')
            
                  tempfile = "temp.txt"
                  `#{cmd} > #{tempfile}`
                  File.open(tempfile, 'r') do |file|
                    lines = file.grep(regex)
                    lines.each { |line| mac_addr << (line.strip[-17, 17]).upcase().gsub(/-/, ';') }
                  end
                  File.unlink(tempfile)
            
                  # if windows and we have more than 1 mac address 
                  # then 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]
            
            
            1 Reply Last reply Reply Quote 0
            • D Offline
              Dan Rathbun
              last edited by 2 Mar 2014, 18:33

              The temp file only goes to the desktop by chance that your current working directory is pointing at the desktop directory.

              WIN = RUBY_PLATFORM !~ /darwin/i OSX =( not WIN ) desktop_dir = WIN ? "#{ENV['USERPROFILE']}/Desktop" : "~/Desktop"
              Assuming that the name of the directory is not language localized, but it likely IS.

              You should not ever assume some other script has not changed the working directory.

              All older SketchUp versions attempted to set it to the user's home directory.
              SketchUp 2014 (on my machine,) has left me in "#{ENV['SystemRoot']}/System32" (why I do not know, but have asked.)

              To see what the current working directory is, use:
              Dir::getwd
              or, it's alias:
              Dir::pwd

              To temporarily change the working directory (and have it change back to whatever it was,) use the blockform of the Dir::chdir class method.

              Dir;;chdir( RUBY_PLATFORM !~ /darwin/i ? "#{ENV['USERPROFILE']}/Desktop" ; "~/Desktop" ) {
                    # do this for testing on a mac
                    @@windows = false 
              
                    # do this for testing on windows
                    # @@windows = false 
              
                    mac_addr = []
                    cmd = @@windows ? 'ipconfig /all' ; 'ifconfig'
              
                    # choose lines that have only six pairs of 2 char hex values 
                    # separated by either a ; or -
                    regex = Regexp.compile('(..[;-]){5}..')
              
                    tempfile = "temp.txt"
                    `#{cmd} > #{tempfile}`
                    File.open(tempfile, 'r') do |file|
                      lines = file.grep(regex)
                      lines.each { |line| mac_addr << (line.strip[-17, 17]).upcase().gsub(/-/, ';') }
                    end
                    File.unlink(tempfile)
              
                    # if windows and we have more than 1 mac address 
                    # then 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]
              
              } # chdir block
              
              

              To test for access rights:
              File::directory?( dirname ) && File::writable?( dirname )

              See the File class for other class query methods to determine access info: executable?(), executable_real?(), readable?(), readable_real?(), writable_real?, etc.

              ๐Ÿ’ญ

              Be aware that path strings with unicode characters will not work well under Ruby 1.8.x on PC. (Mac OSX does not have these problems.)

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • G Offline
                Garry K
                last edited by 3 Mar 2014, 18:44

                Thanks Dan. I should have thought about the whole issue of current working directory. This has been true since the beginning of DOS.

                I'm going to put a config folder in my own plugin folder. I can put the temp.txt file there. I can also use the folder to put user custom settings so that updates don't overwrite user settings.

                Thanks

                1 Reply Last reply Reply Quote 0
                • tt_suT Offline
                  tt_su
                  last edited by 4 Mar 2014, 10:07

                  @garry k said:

                  I'm going to put a config folder in my own plugin folder. I can put the temp.txt file there. I can also use the folder to put user custom settings so that updates don't overwrite user settings.

                  For SketchUp versions older than SU2014 that might cause problems if the user doesn't have full permissions to the Plugins folder since Windows locks down Program Files quite tight. Ruby will in many cases not have write access.

                  1 Reply Last reply Reply Quote 0
                  • G Offline
                    Garry K
                    last edited by 4 Mar 2014, 12:40

                    @tt_su said:

                    For SketchUp versions older than SU2014 that might cause problems if the user doesn't have full permissions to the Plugins folder since Windows locks down Program Files quite tight. Ruby will in many cases not have write access.

                    OK - then what do you suggest. I will need to find a location to temporarily write a file. This will be for Windows and Mac.

                    I suppose I could use one strategy for SU 2014 and another for older versions.

                    1 Reply Last reply Reply Quote 0
                    • tt_suT Offline
                      tt_su
                      last edited by 4 Mar 2014, 13:09

                      For temporary files in SketchUp < 2014 I would write to the temp folder based on that the ENV variable gives you:

                      temp_path = File.expand_path(ENV['TMPDIR'] || ENV['TMP'] || ENV['TEMP'])

                      For settings and persistent preferences, if you want to write that to file it will get tricky in older SketchUp. I stick with using Sketchup.write_default/read_default.

                      This is one of those areas where you probably would end up with code branching for the different ruby interpreters.

                      1 Reply Last reply Reply Quote 0
                      • D Offline
                        Dan Rathbun
                        last edited by 4 Mar 2014, 14:19

                        @tt_su said:

                        For temporary files in SketchUp < 2014 I would write to the temp folder based on that the ENV variable gives you:

                        But... the TMP/TEMP directory is in the user path, and IF the username has unicode characters, then older versions on PC have problems.

                        We would need to employ tricks like in TIG's PCFIleTools.
                        http://sketchucation.com/forums/viewtopic.php?f=180%26amp;t=43007%26amp;p=385472

                        I'm not here much anymore.

                        1 Reply Last reply Reply Quote 0
                        • tt_suT Offline
                          tt_su
                          last edited by 4 Mar 2014, 14:23

                          @dan rathbun said:

                          But... the TMP/TEMP directory is in the user path, and IF the username has unicode characters, then older versions on PC have problems.

                          On the machines I tested this on the ENV variable would return DOS 8.3 short filepaths for the temp paths - and that works for Ruby 1.8 since they call the ASCII versions of the Windows file functions that can handle that.

                          That said, there's always a change that ENV variables might be altered by someone, but it has worked fine for several years.

                          In theory you could use Win32 API calls under Ruby 1.8 to get short 8.3 filepaths and pass that to the Ruby File methods.

                          1 Reply Last reply Reply Quote 0
                          • G Offline
                            Garry K
                            last edited by 4 Mar 2014, 23:15

                            @tt_su said:

                            This is one of those areas where you probably would end up with code branching for the different ruby interpreters.

                            Then branching it is.

                            
                                  mac_addr = []
                                  cmd = @@windows ? 'ipconfig /all' ; 'ifconfig'
                            
                                  # choose lines that have only six pairs of 2 char hex values separated by either a ; or -
                                  regex = Regexp.compile('(..[;-]){5}..')
                            
                                  if (RUBY_VERSION[0..2] == '1.8')
                                    lines = %x[#{cmd}].split("\n").grep(regex)
                                    lines.each { |line| mac_addr << (line.strip[-17, 17]).upcase().gsub(/-/, ';') }
                                  else
                                    temp_path = File.expand_path(ENV['TMPDIR'] || ENV['TMP'] || ENV['TEMP'])
                                    tempfile = File.join(temp_path, 'temp.txt')
                            
                                    `#{cmd} > #{tempfile}`
                                    File.open(tempfile, 'r') do |file|
                                      lines = file.grep(regex)
                                      lines.each { |line| mac_addr << (line.strip[-17, 17]).upcase().gsub(/-/, ';') }
                                    end
                                    File.unlink(tempfile)
                                  end
                            
                            
                            1 Reply Last reply Reply Quote 0
                            • D Offline
                              Dan Rathbun
                              last edited by 5 Mar 2014, 04:22

                              similar:
                              ipa = %x[ipconfig /all].split("\n").grep /\A\s*(Physical Address)/

                              The regular expression means:
                              \A start of line
                              \s* one or more space characters
                              and the parens group the string

                              returns:

                              [
                                "   Physical Address. . . . . . . . . ; XX-XX-XX-ED-C2-XX",
                                "   Physical Address. . . . . . . . . ; 00-XX-XX-ED-C2-XX",
                                "   Physical Address. . . . . . . . . ; XX-XX-XX-E5-20-XX",
                                "   Physical Address. . . . . . . . . ; 00-00-00-00-00-00-00-E0",
                                "   Physical Address. . . . . . . . . ; 00-00-00-00-00-00-00-E0",
                                "   Physical Address. . . . . . . . . ; 00-00-00-00-00-00-00-E0"
                              ]
                              
                              

                              The first 2 are the Wireless:

                              1. Microsoft Virtual Miniport Adapter
                              2. the actual 802.11b/g/n Adaptor

                              The third is the Ethernet port.

                              The last 2 are virtual tunneling ports.

                              ~

                              I'm not here much anymore.

                              1 Reply Last reply Reply Quote 0
                              • D Offline
                                Dan Rathbun
                                last edited by 5 Mar 2014, 04:24

                                DUh... I just stumbled upon GetMAC.exe on my Win 7 machine. (It is in the "%WINDIW%/System32" directory.)

                                Ironic given the title of the topic.

                                Win 7 comes with it installed.

                                It is installed on XP Pro, but other editions need to install the Resource Kit or Support Tools. (They are separate installers on the install CD.)

                                Documentation page: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/getmac.mspx?mfr=true

                                Output looks like: %(#008080)[C:\Users\Dan>getmac]

                                
                                Physical Address    Transport Name
                                =================== ==========================================================
                                XX-XX-XX-2B-E5-XX   \Device\Tcpip_{<a GUID is output here>}
                                XX-XX-XX-C2-1B-96   Media disconnected
                                XX-XX-XX-C2-1B-96   Media disconnected
                                
                                

                                ๐Ÿ’ญ

                                I'm not here much anymore.

                                1 Reply Last reply Reply Quote 0
                                • G Offline
                                  Garry K
                                  last edited by 1 Apr 2014, 16:16

                                  A fellow in Russian has been testing this and ends up with this error when running SU 2014.

                                  Error: #<ArgumentError: invalid byte sequence in UTF-8>
                                  The line of code that is fails on is

                                  lines = file.grep(regex)

                                  So the next thing we did was change the prior line to but had the same error.

                                  File.open(tempfile, 'r', :encoding => 'iso-8859-1') do |file|

                                  What is interesting and confusing is that when we ran this code from the web-console it worked

                                  
                                  mac_addr = []
                                  cmd = 'ipconfig /all'
                                  regex = Regexp.compile('(..[;-]){5}..')
                                  
                                  if (RUBY_VERSION[0..2] == '1.8')
                                    lines = %x[#{cmd}].split("\n").grep(regex)
                                    lines.each { |line| mac_addr << (line.strip[-17, 17]).upcase().gsub(/-/, ';') }
                                  else
                                    temp_path = File.expand_path(ENV['TMPDIR'] || ENV['TMP'] || ENV['TEMP'])
                                    tempfile = File.join(temp_path, 'temp.txt')
                                  
                                    `#{cmd} > #{tempfile}`
                                    File.open(tempfile, 'r', ;encoding => 'iso-8859-1') do |file|
                                       lines = file.grep(regex)
                                       lines.each { |line| mac_addr << (line.strip[-17, 17]).upcase().gsub(/-/, ';') }
                                    end
                                  
                                    File.unlink(tempfile)
                                  end
                                  
                                  mac_addr[1]
                                  
                                  
                                  1 Reply Last reply Reply Quote 0
                                  • D Offline
                                    Dan Rathbun
                                    last edited by 1 Apr 2014, 19:49

                                    Encoding::default_internal has not been set correctly in the first release of SU2014.

                                    SO try:
                                    File.open(tempfile, 'r', :encoding => 'iso-8859-1:utf-8') do |file|

                                    I'm not here much anymore.

                                    1 Reply Last reply Reply Quote 0
                                    • G Offline
                                      Garry K
                                      last edited by 1 Apr 2014, 20:01

                                      My mistake this does work.
                                      We got our versions crossed up.

                                      File.open(tempfile, 'r', :encoding => 'iso-8859-1') do |file|

                                      Thanks anyway Dan

                                      1 Reply Last reply Reply Quote 0
                                      • D Offline
                                        Dan Rathbun
                                        last edited by 1 Apr 2014, 20:10

                                        OH he is in Russia, so he's likely running a different codepage.

                                        Get his default system Encoding:
                                        Encoding::find("filesystem") >> an encoding
                                        (On my machine it returns the #<Encoding:Windows-1252> object reference.)
                                        or
                                        Encoding::locale_charmap >> returns a name for the encoding.
                                        (On my machine it returns the string "CP1252".)

                                        ๐Ÿ’ญ

                                        I'm not here much anymore.

                                        1 Reply Last reply Reply Quote 0
                                        • G Offline
                                          Garry K
                                          last edited by 2 Apr 2014, 15:54

                                          For me it just returns an error.
                                          (eval):5:in `run': uninitialized constant JF::WebConsole::Encoding

                                          1 Reply Last reply Reply Quote 0
                                          • 1
                                          • 2
                                          • 3
                                          • 3 / 3
                                          • First post
                                            Last post
                                          Buy SketchPlus
                                          Buy SUbD
                                          Buy WrapR
                                          Buy eBook
                                          Buy Modelur
                                          Buy Vertex Tools
                                          Buy SketchCuisine
                                          Buy FormFonts

                                          Advertisement