forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMAC-Address.cpp
More file actions
55 lines (39 loc) · 1003 Bytes
/
Copy pathMAC-Address.cpp
File metadata and controls
55 lines (39 loc) · 1003 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Run in UNIX systems to get MAC Address of your system....
//Harsha Vardhan
#include <iostream>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <net/if.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
using namespace std;
void getMacAddress(char *uc_Mac)
{
int fd;
struct ifreq ifr;
const char* iface = "enp2s0";
char* mac;
fd = socket(AF_INET, SOCK_DGRAM, 0);
ifr.ifr_addr.sa_family = AF_INET;
strncpy((char *)ifr.ifr_name , (const char *)iface , IFNAMSIZ-1);
ioctl(fd, SIOCGIFHWADDR, &ifr);
close(fd);
mac = (char *)ifr.ifr_hwaddr.sa_data;
//display mac address
sprintf((char *)uc_Mac,(const char *)"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n" , mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
int main()
{
char mac[32]={0};
getMacAddress(mac);
cout<<endl<<"Mac Address : "<<mac<<endl;
return 0;
}