Find MAC address of Linux Device in C++

In this post, we will learn how to find the MAC address of a Linux Device by using a C++ program. With the help of this tutorial, you will be able to easily find out the MAC address of your Linux device by just running a simple C++ program.

What is the MAC address?

The MAC address or Media Access Control Address is a 12 digit hexadecimal number. It acts like a unique ID used to communicate within a network.

We will be using system() call through a C++ program that directly communicates with the OS (Operating System) and allows us to use the Linux commands. Please keep in mind that you should not use a lot of system() calls in a program as they are expensive to call (resource-wise).

We will discuss two ways to get the MAC address.

First Approach to find The MAC address :

In all Linux based systems eth0 is the default network interface. If you have a different interface name change the following path accordingly: /sys/class/net/eth0/address

Now we will use the cat command to display the text in this file. (/sys/class/net/eth0/address contains the MAC address).

  //1st way to get the MAC address
string str1 = "cat /sys/class/net/eth0/address";

const char *command1 = str1.c_str();     //c_str() converts the string into a C-Style string

system(command1);

Second Approach using Regular expressions and ifconfig:

Pre-requisites: Regular Expressions and grep command in Linux.

The ifconfig command (interface configuration) allows us to view the configuration of all the network interfaces on your system. We will then use the grep command which is used to search for a specified pattern.

We want the MAC address which is a 12 digit hexadecimal number.

The regular expression we need to use is: ‘([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}’

So the command is grep -o -E ‘([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}’

Note : The 6 bytes are separated by colons and are hexadecimal.

 

//2nd way to get MAC address
    string str2 = "ifconfig eth0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' ";

    const char *command2 = str2.c_str(); //c_str() converts the string into a C-Style string

 

Parameters used with grep command are :

-o, –only-matching show only nonempty parts of lines that match

-E, –extended-regexp PATTERNS are extended regular expressions

Code for Finding MAC address of Linux Device in C++

#include <iostream>
#include <cstring>

using namespace std;

int main ()
{

    //1st way to get the MAC address
    string str1 = "cat /sys/class/net/eth0/address";

    const char *command1 = str1.c_str();

    //2nd way to get MAC address
    string str2 = "ifconfig eth0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' "; 

    const char *command2 = str2.c_str();

      system(command1);

      system(command2);

    return 0;
}

 

And below is our output

find mac address of linux using C++

Also Read: Difference between inline and macro in C++

Leave a Reply

Your email address will not be published. Required fields are marked *