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
      最後由 編輯

      Is there an OS independent way to get the MAC Address of the client within a sketchup script and without having to install additional software on the client's machine.

      I'd like to tie in some hardware id of some sort to a web site registration for that computer and user.

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

        Here is what I can do so far.

        and if windows I can parse the output

        but with osx I don't know what I should expect

        ` platform = RUBY_PLATFORM.downcase

        if ( platform.include?( "win" ))
        windows = true
        cmd = 'ipconfig /all'
        else
        windows = false
        cmd = 'ifconfig'
        end

        then for windows I can redirect the output of ipconfig

        null = test(?e, '/dev/null') ? '/dev/null' : 'NUL'
        lines = IO.popen("#{ cmd } 2> #{ null }"){|fd| fd.readlines}
        puts lines`

        some of the output for windows
        So the Ethernet adapter is usually soldered on the mother board these days and should be reasonably safe.
        parse out Physical Address is the MAC Address for the Ethernet

        ` Ethernet adapter Local Area Connection:

        Media State . . . . . . . . . . . : Media disconnected
        Connection-specific DNS Suffix . :
        Description . . . . . . . . . . . : Atheros L1 Gigabit Ethernet 10/100/1000Base-T Controller
        Physical Address. . . . . . . . . : 00-1D-60-0D-80-87
        DHCP Enabled. . . . . . . . . . . : No
        Autoconfiguration Enabled . . . . : Yes`

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

          always something new to learn !!!

          this works in windows. It would be nice if it would run quitetly.

          puts ipconfig /all

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

            I'm reading that back ticks ` won't work in OSX and that using back ticks in windows is not as secure as the following

            puts %x[ipconfig /all]

            1 條回覆 最後回覆 回覆 引用 0
            • S 離線
              slbaumgartner
              最後由 編輯

              @garry k said:

              I'm reading that back ticks ` won't work in OSX

              Where are you reading that? They work fine for me (of course, SU may "beachball" while Ruby blocks waiting for output from a slow command).

              Steve

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

                This link makes references to it

                Link Preview Image
                Getting output of system() calls in Ruby

                If I call a command using Kernel#system in Ruby, how do I get its output? system("ls")

                favicon

                Stack Overflow (stackoverflow.com)

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

                  @garry k said:

                  I'm reading that back ticks ` won't work in OSX

                  where are you reading that? they are interchangeable on a mac. Both return in Ruby Console.

                  > puts `ifconfig -a | grep "ether"`
                  	ether 00;** ;** ;** ;** ;36 
                  	ether 00;** ;** ;** ;** ;ad 
                  nil
                  > puts %x(ifconfig -a | grep "ether")
                  	ether 00;** ;** ;** ;** ;36 
                  	ether 00;** ;** ;** ;** ;ad 
                  nil
                  
                  

                  but ifconfig will give you more that one ether, so...

                   puts (%x(ifconfig -a | grep "ether")).split[1]
                  "00;** ;** ;** ;** ;36"
                  nil
                  
                  

                  or mac only [I think]

                  
                  > > puts (%x(netstat -in | grep "en0")).split[3].inspect
                  "00;** ;** ;** ;** ;36"
                  nil
                  

                  you'll get what you want...

                  john

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

                  1 條回覆 最後回覆 回覆 引用 0
                  • S 離線
                    slbaumgartner
                    最後由 編輯

                    @garry k said:

                    This link makes references to it

                    Link Preview Image
                    Getting output of system() calls in Ruby

                    If I call a command using Kernel#system in Ruby, how do I get its output? system("ls")

                    favicon

                    Stack Overflow (stackoverflow.com)

                    That link discusses the various ways to capture the output of a system command and security issues that arise from issuing a system command from Ruby. Nowhere does it say that the backtick syntax does not work on Mac OS X!

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

                      Garry.. this is safer and faster to use boolean Constants throughout your module(s) [ie, Strings are slow.]

                      At the top of your module:
                      MAC = OSX = RUBY_PLATFORM !~ /mswin|mingw/ PC = WIN = (not MAC)

                      Then say down further:

                      if WIN
                        # do this
                      else
                        # it's a Mac, do that
                      end
                      

                      another example, showing a webdialog:
                      WIN ? @dlg.show() : @dlg.show_modal()

                      ~

                      I'm not here much anymore.

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

                                            Advertisement