Sign in Join
Wiki copytemporary

macOS Retrieve MAC address

(macOS only)

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.

pascal
Program mac;

{$mode objfpc}{$H+}

Uses 
  SysUtils,
  StrUtils,
  Process,
  Classes;

function GetmacOSMacAddress : string;
var
   theProcess        : TProcess;
   theOutput         : TStringList;
   i, j, theLine, thePos : integer;

begin
   theProcess := TProcess.Create(nil);
   theProcess.Executable := 'ifconfig';

   // If interface specified, then retrieve its MAC address
   // Otherwise, retrieve MAC addresses for all interfaces
   if(ParamStr(1) <> '') then
      theProcess.Parameters.Add(ParamStr(1));

   theProcess.Options := theProcess.Options + [poWaitOnExit, poUsePipes];
   theProcess.Execute;
   theOutput := TStringList.Create;
   theOutput.LoadFromStream(theProcess.Output);

   theLine := -1;
   for i := 0 to theOutput.Count - 1 do
     begin
       j := pos('ether', theOutput.Strings[i]);
       if (j > 0) then
         begin
           theLine := i;
           thePos := j + length('ether') + 1;

           if(theLine > -1) then
              begin
                Result := UpperCase(ExtractSubStr(theOutput.Strings[theLine], thePos, [' ']));
                WriteLn(Result);
              end;
         end;
     end;

   // Always free the memory used
   theOutput.Free;
   theProcess.Free;
end;

begin
   GetmacOSMacAddress;
end.

See also

External links


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.

Revision 1, last changed by WikiArchive .

Edited on wiki.freepascal.org by 1 person, 2020–2022

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
TrevNew macOS page
TrevExternal links: New section
TrevSee also: New section

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.