macOS Retrieve MAC address: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: macOS Retrieve MAC address
| Old | New | Line | |
|---|---|---|---|
| 1 | + | *(macOS only)* | |
| 2 | + | ||
| 3 | + | Simple Free Pascal command line program to retrieve the MAC address for the specified network interface or, if no network interface is specified, retrieve the MAC addresses for all the network interfaces. | |
| 4 | + | ||
| 5 | + | ``` pascal | |
| 6 | + | Program mac; | |
| 7 | + | ||
| 8 | + | {$mode objfpc}{$H+} | |
| 9 | + | ||
| 10 | + | Uses | |
| 11 | + | SysUtils, | |
| 12 | + | StrUtils, | |
| 13 | + | Process, | |
| 14 | + | Classes; | |
| 15 | + | ||
| 16 | + | function GetmacOSMacAddress : string; | |
| 17 | + | var | |
| 18 | + | theProcess : TProcess; | |
| 19 | + | theOutput : TStringList; | |
| 20 | + | i, j, theLine, thePos : integer; | |
| 21 | + | ||
| 22 | + | begin | |
| 23 | + | theProcess := TProcess.Create(nil); | |
| 24 | + | theProcess.Executable := 'ifconfig'; | |
| 25 | + | ||
| 26 | + | // If interface specified, then retrieve its MAC address | |
| 27 | + | // Otherwise, retrieve MAC addresses for all interfaces | |
| 28 | + | if(ParamStr(1) <> '') then | |
| 29 | + | theProcess.Parameters.Add(ParamStr(1)); | |
| 30 | + | ||
| 31 | + | theProcess.Options := theProcess.Options + [poWaitOnExit, poUsePipes]; | |
| 32 | + | theProcess.Execute; | |
| 33 | + | theOutput := TStringList.Create; | |
| 34 | + | theOutput.LoadFromStream(theProcess.Output); | |
| 35 | + | ||
| 36 | + | theLine := -1; | |
| 37 | + | for i := 0 to theOutput.Count - 1 do | |
| 38 | + | begin | |
| 39 | + | j := pos('ether', theOutput.Strings[i]); | |
| 40 | + | if (j > 0) then | |
| 41 | + | begin | |
| 42 | + | theLine := i; | |
| 43 | + | thePos := j + length('ether') + 1; | |
| 44 | + | ||
| 45 | + | if(theLine > -1) then | |
| 46 | + | begin | |
| 47 | + | Result := UpperCase(ExtractSubStr(theOutput.Strings[theLine], thePos, [' '])); | |
| 48 | + | WriteLn(Result); | |
| 49 | + | end; | |
| 50 | + | end; | |
| 51 | + | end; | |
| 52 | + | ||
| 53 | + | // Always free the memory used | |
| 54 | + | theOutput.Free; | |
| 55 | + | theProcess.Free; | |
| 56 | + | end; | |
| 57 | + | ||
| 58 | + | begin | |
| 59 | + | GetmacOSMacAddress; | |
| 60 | + | end. | |
| 61 | + | ``` | |
| 62 | + | ||
| 63 | + | ## See also | |
| 64 | + | ||
| 65 | + | - [[Accessing macOS System Information]] | |
| 66 | + | ||
| 67 | + | ## External links | |
| 68 | + | ||
| 69 | + | - [Apple: Uniquely Identifying a Macintosh Computer](https://developer.apple.com/library/archive/technotes/tn1103/_index.html#//apple_ref/doc/uid/DTS10002943) | |
| 70 | + | ||
| 71 | + | --- | |
| 72 | + | *From the Free Pascal wiki, page "macOS Retrieve MAC address", as it stood on 2022-01-05 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 73 | + | Category: Code Snippets | |
| 74 | + | Category: FPC | |
| 75 | + | Category: Networking | |
| 76 | + | Category: macOS |
