Difference between revisions of "macOS Retrieve MAC address"

From Free Pascal wiki
Jump to navigationJump to search
(New macOS page)
 
(→‎See also: New section)
 
(One intermediate revision by the same user not shown)
Line 60: Line 60:
 
end.
 
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
== See also ==
 +
 +
* [[Accessing macOS System Information]]
 +
 +
== External links ==
 +
 +
* [https://developer.apple.com/library/archive/technotes/tn1103/_index.html#//apple_ref/doc/uid/DTS10002943 Apple: Uniquely Identifying a Macintosh Computer]
  
 
[[Category:FPC]]
 
[[Category:FPC]]

Latest revision as of 03:16, 5 January 2022

macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide

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.

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