The code below does all this and also gets the interface name and does some network communication (send SYN to IP address obtained from a file which is a list of IP addresses - one per line). Following the suggestion to use a netlink socket to get the default route I implemented something that gets the default route ip address and then gets the MAC address through an ARP request. We want the MAC address which is a 12 digit hexadecimal number.I am posting how I finally solved this. We will then use the grep command which is used to search for a specified pattern. The ifconfig command ( interface configuration) allows us to view the configuration of all the network interfaces on your system. Pre-requisites: Regular Expressions and grep command in Linux. System(command1) Second Approach using Regular expressions and ifconfig: String str1 = "cat /sys/class/net/eth0/address" Ĭonst char *command1 = str1.c_str() //c_str() converts the string into a C-Style string (/sys/class/net/eth0/address contains the MAC address). Now we will use the cat command to display the text in this file. If you have a different interface name change the following path accordingly: /sys/class/net/eth0/address In all Linux based systems eth0 is the default network interface. We will discuss two ways to get the MAC address. 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 be using system() call through a C++ program that directly communicates with the OS (Operating System) and allows us to use the Linux commands. It acts like a unique ID used to communicate within a network. The MAC address or Media Access Control Address is a 12 digit hexadecimal number. 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. In this post, we will learn how to find the MAC address of a Linux Device by using a C++ program.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |