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.
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.