I am working on Ubuntu. How can I get MAC address of my machine or an interface say eth0 using C program.
In the Command Prompt window, type ipconfig /all and press Enter. Under the Ethernet Adapter Local Area Connection section, look for the “Physical Address”. This is your MAC Address.
The easiest way to find the MAC Address of a printer is to do the following: Navigate through the settings on the printer's control panel to find an option to print a configuration page. You can sometimes find the option in "Report Settings," "Network Setup," "Print Setup," "Self Test" or a similar menu.
To see all the MAC addresses and their associated IP addresses, type “arp -a”. This command will list all the available MAC addresses in the system.
Much nicer than all this socket or shell madness is simply using sysfs for this:
the file /sys/class/net/eth0/address
carries your mac adress as simple string you can read with fopen()
/fscanf()
/fclose()
. Nothing easier than that.
And if you want to support other network interfaces than eth0 (and you probably want), then simply use opendir()
/readdir()
/closedir()
on /sys/class/net/
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With