Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get MAC address of your machine using a C program?

I am working on Ubuntu. How can I get MAC address of my machine or an interface say eth0 using C program.

like image 622
Bruce Avatar asked Nov 22 '09 19:11

Bruce


People also ask

How do I find my MAC address in cmd?

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.

How do I print a 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.

How can I get a list of all MAC addresses?

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.


1 Answers

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

like image 185
user175104 Avatar answered Oct 21 '22 16:10

user175104