If you want a layer of abstraction away from the /sys/class/net
interface you could use the nmcli
command. For example, the GENERAL
field of the dev[ice]
object contains the interface type and hardware address (MAC):
$ nmcli -f GENERAL.DEVICE,GENERAL.TYPE,GENERAL.HWADDR dev showGENERAL.DEVICE: enp0s3GENERAL.TYPE: ethernetGENERAL.HWADDR: 08:00:27:A7:C4:90GENERAL.DEVICE: enp0s9GENERAL.TYPE: ethernetGENERAL.HWADDR: 08:00:27:8E:46:ABGENERAL.DEVICE: loGENERAL.TYPE: loopbackGENERAL.HWADDR: 00:00:00:00:00:00
You can get the output in a more parsable form using the -g | --get-values
option:
$ nmcli -g GENERAL.TYPE,GENERAL.HWADDR -e no dev showethernet08:00:27:A7:C4:90ethernet08:00:27:8E:46:ABloopback00:00:00:00:00:00
So for example in a script you could do
$ nmcli -g GENERAL.TYPE,GENERAL.HWADDR -e no dev show | awk -v RS= '$1 ~ "ethernet" {print $2}'08:00:27:A7:C4:9008:00:27:8E:46:AB