sketchucation logo sketchucation
    • 登入
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Get MAC Address

    已排程 已置頂 已鎖定 已移動 Developers' Forum
    55 貼文 8 Posters 4.9k 瀏覽 8 Watching
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • G 離線
      Garry K
      最後由 編輯

      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
      
      1 條回覆 最後回覆 回覆 引用 0
      • G 離線
        Garry K
        最後由 編輯

        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`

        1 條回覆 最後回覆 回覆 引用 0
        • D 離線
          driven
          最後由 編輯

          @gary
          couple of changes needed to run on my mac,
          got rid of class variables
          added windows =
          added simplified regex = with .. for grep

          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

          learn from the mistakes of others, you may not live long enough to make them all yourself...

          1 條回覆 最後回覆 回覆 引用 0
          • G 離線
            Garry K
            最後由 編輯

            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?

            1 條回覆 最後回覆 回覆 引用 0
            • D 離線
              driven
              最後由 編輯

              @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?
              the one with the red circle...

              learn from the mistakes of others, you may not live long enough to make them all yourself...

              1 條回覆 最後回覆 回覆 引用 0
              • G 離線
                Garry K
                最後由 編輯

                Thanks again. I checked the code for windows and it works fine.
                I've added an upcase on the following line

                lines.each{|line| mac_addr << (line.strip[-17,17]).upcase().gsub( /-/, ':' ) }

                1 條回覆 最後回覆 回覆 引用 0
                • jaimedaJ 離線
                  jaimeda
                  最後由 編輯

                  Hello

                  This works fine in Sketchup 2013 but not in 2014.

                  Any tips?

                  1 條回覆 最後回覆 回覆 引用 0
                  • tt_suT 離線
                    tt_su
                    最後由 編輯

                    @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&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxstring">"C;/Users/Thomas/Desktop/temp.txt"<br /></span><span class="syntaxkeyword">`</span><span class="syntaxstring">ipconfig&nbsp;/all&nbsp;>&nbsp;#{tempfile}</span><span class="syntaxkeyword">`<br /></span><span class="syntaxdefault">puts&nbsp;File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">read</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">tempfile</span><span class="syntaxkeyword">)<br />&nbsp;</span><span class="syntaxdefault"></span>
                    
                    1 條回覆 最後回覆 回覆 引用 0
                    • jaimedaJ 離線
                      jaimeda
                      最後由 編輯

                      Thanks Thomas.

                      It works perfectly.

                      1 條回覆 最後回覆 回覆 引用 0
                      • G 離線
                        Garry K
                        最後由 編輯

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

                        1 條回覆 最後回覆 回覆 引用 0
                        • tt_suT 離線
                          tt_su
                          最後由 編輯

                          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&nbsp;</span><span class="syntaxstring">'tempfile'<br /></span><span class="syntaxdefault">file&nbsp;</span><span class="syntaxkeyword">=&nbsp;</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&nbsp;/all&nbsp;>&nbsp;#{file.path}</span><span class="syntaxkeyword">`<br /></span><span class="syntaxdefault">puts&nbsp;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>
                          
                          1 條回覆 最後回覆 回覆 引用 0
                          • Dan RathbunD 離線
                            Dan Rathbun
                            最後由 編輯

                            Weirdly the backquotes, & %x strings which use the ()` method, work on my machine.

                            And I verified that I do not have the backquote_patch.rb (that I proposed,) loaded.

                            I'm not here much anymore.

                            1 條回覆 最後回覆 回覆 引用 0
                            • Chris FullmerC 離線
                              Chris Fullmer
                              最後由 編輯

                              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?

                              Lately you've been tan, suspicious for the winter.
                              All my Plugins I've written

                              1 條回覆 最後回覆 回覆 引用 0
                              • G 離線
                                Garry K
                                最後由 編輯

                                @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 條回覆 最後回覆 回覆 引用 0
                                • Dan RathbunD 離線
                                  Dan Rathbun
                                  最後由 編輯

                                  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 條回覆 最後回覆 回覆 引用 0
                                  • Dan RathbunD 離線
                                    Dan Rathbun
                                    最後由 編輯

                                    @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 條回覆 最後回覆 回覆 引用 0
                                    • jaimedaJ 離線
                                      jaimeda
                                      最後由 編輯

                                      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 條回覆 最後回覆 回覆 引用 0
                                      • G 離線
                                        Garry K
                                        最後由 編輯

                                        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 條回覆 最後回覆 回覆 引用 0
                                        • Dan RathbunD 離線
                                          Dan Rathbun
                                          最後由 編輯

                                          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 條回覆 最後回覆 回覆 引用 0
                                          • G 離線
                                            Garry K
                                            最後由 編輯

                                            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 條回覆 最後回覆 回覆 引用 0
                                            • 1
                                            • 2
                                            • 3
                                            • 2 / 3
                                            • 第一個貼文
                                              最後的貼文
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement