[fpc-pascal] cannot read /sys/class/net/eth1/address even as root user on linux

Mark Morgan Lloyd markMLl.fpc-pascal at telemetry.co.uk
Thu Dec 5 13:10:42 CET 2013

Dennis Poon wrote:
> I tried to read the mac address in the captioned file path.
> In terminal, I can simply 'cat' and display the mac address.
> In FPC, when I tried to use TFileStream to read it,
>   FS := TFileStream.Create('/sys/class/net/eth1/address', 
> fmOpenRead+fmShareDenyWrite);
> it raised "stream read error"
> If I tried to use TextFile and readln, it even halted the program.
> My question is:
>   Why 'cat' command can easily display this special file 'address' but 
> TFileStream cannot.
> I was told to use this method to get the MAC address of a network card, 
> what other methods are there?

Nothing portable. You can't even use /sys safely without first checking 
it's there: the filesystem support might not be compiled into the 
kernel, or the filesystem might not be mounted. One possibility would be 
looking at /etc/udev/rules.d/z25_persistent-net.rules but again there 
are serious portability issues here.

You do actually have an eth1 device on your system, don't you? Most if 
not all of the ones around here start off with eth0.

Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]

More information about the fpc-pascal mailing list