Via Cà Matta 2 - Peschiera Borromeo (MI)
+39 02 00704272
info@synaptica.info

Delphi Linux getting MacAddress

Digital Innovation Partner

Delphi Linux getting MacAddress

In linux every adapter configuration has a file system path, for example the adapter eth0 has all configuration, in some text files located in “/sys/class/net/eth0/”.
The file whose name is “address” contains a single line with the mac adrress value.

function GetLinuxMacAddress(device_name : String): string;
const
  linux_path     = '/sys/class/net/%s/address';

var
  f       : textfile;
  device,
  path,
  addr    : string;

begin
  Result := '';

  path := Format(linux_path,[device_name]);
  if (not FileExists(path)) then
  begin
     Result := '';
  end
  else
  begin
    AssignFile(f, path);
    reset(f);
    readln(f, addr);
    closefile(f);
    Result := addr;
  end;
end;