sketchucation logo sketchucation
    • 登入
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🔌 Smart Spline | Fluid way to handle splines for furniture design and complex structures. Download

    Get MAC Address

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

      @garry k said:

      then for windows I can redirect the output of ipconfig

      null = test(?e, '/dev/null') ? '/dev/null' : 'NUL'

      Why? The %x or backquoted string method returns it's result.

      so:
      ip = %x[ipconfig /all]
      Then parse the string ip,.. iterate it using each_line or
      split("\n") it into an array of lines, then use find, etc.

      I'm not here much anymore.

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

        @dan

        does reply_from_ifconig.split("\n").grep("Physical Address") or similar work on a PC?

        john
        EDIT for something as un-changing as ifconfig on a mac you could use

        (%x(ifconfig -a)).split("\n")[10].split[1]
        

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

        1 條回覆 最後回覆 回覆 引用 0
        • P 離線
          pgarmyn
          最後由 編輯

          @dan rathbun said:

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

          Scanning for “Physical Address” only gives a result with an EN/US OS
          This works also on the old continent :

          def MyModule.mac_address
          	platform = RUBY_PLATFORM.downcase
          	iptxt = `#{(platform =~ /win32/) ? 'ipconfig /all' ; 'ifconfig'}`
          	## delete DHCPv6 ;
          	iptxt.gsub!(/..\-..\-..\-..\-..\-..\-..\-..\-..\-..\-..\-..\-..\-../,"") 
          	# delete Tunnel ;
          	iptxt.gsub!("00-00-00-00-","")
          	## create array with all the physical adresses ;
          	t_array=iptxt.scan(/..\-..\-..\-..\-..\-../)
          	#puts t_array.inspect 
          	## returns first physical adresses if one found
          	return t_array[0] if t_array.length>0 
          	return nil
          end
          
          1 條回覆 最後回覆 回覆 引用 0
          • D 離線
            driven
            最後由 編輯

            There's also this one

            mac_MAC = %x(ioreg -rd1 -c IOPlatformExpertDevice |  awk "/IOPlatformUUID/ { print $3; }")
            
            

            returns
            "IOPlatformUUID" = "00000000-0000-1000-8000-00**********" [my address obscured with ***]

            john

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

            1 條回覆 最後回覆 回覆 引用 0
            • P 離線
              pgarmyn
              最後由 編輯

              @dan rathbun said:

              DUh... I just stumbled upon GetMAC.exe on my Win 7 machine....

              Yes, i have it also on Win8.1 👍
              For the MAC-world (Apple-users) is there an equivalent ❓

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

                @pgarmyn said:

                For the MAC-world (Apple-users) is there an equivalent :?:

                I just posted it above?

                The command ioreg located in /usr/sbin/ioreg is used to display the I/O Kit registry.

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

                1 條回覆 最後回覆 回覆 引用 0
                • P 離線
                  pgarmyn
                  最後由 編輯

                  👍 👍 👍 @driven

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

                    @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

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

                    1 條回覆 最後回覆 回覆 引用 0
                    • Dan RathbunD 離線
                      Dan Rathbun
                      最後由 編輯

                      There IS a gem that can be used for reference (perhaps it can be made SketchUp friendly.)

                      Link Preview Image
                      macaddr/lib/macaddr.rb at master · ahoward/macaddr

                      cross platform mac address determination for ruby. Contribute to ahoward/macaddr development by creating an account on GitHub.

                      favicon

                      GitHub (github.com)

                      I'm not here much anymore.

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

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

                        if ( @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
                        end

                        return ""
                        end`

                        1 條回覆 最後回覆 回覆 引用 0
                        • P 離線
                          pgarmyn
                          最後由 編輯

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

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

                                            Advertisement