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

    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.
    • tt_suT Offline
      tt_su
      last edited by

      @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

        @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
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          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
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            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

              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
              • Dan RathbunD Offline
                Dan Rathbun
                last edited by

                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

                  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
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by

                    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

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

                      1 Reply Last reply Reply Quote 0
                      • G Offline
                        Garry K
                        last edited by

                        Sorry - I see this only works in SU 2014

                        1 Reply Last reply Reply Quote 0
                        • G Offline
                          Garry K
                          last edited by

                          I've run into issues with taking the fist or second Mac Address out of ifconfig or ipconfig /all.

                          I've changed the code to look at the entire file and then to step through it line by line and treat it as a bit of a state machine.

                          Thanks to pgarmyn and Driven I think I've got some code that will work. They both helped with files as have a handful of other people.

                          With windows I've tested files in English, French, Russian and some with VMWare set up.
                          With OSX I've just tested in English.


                          Mac Address

                          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