-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
68 lines (58 loc) · 1.64 KB
/
main2.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <string>
#include <dirent.h>
bool IsPhysicalAdapter(const std::string & adapter)
{
std::string path = "/sys/class/net/" + adapter + "/device/";
DIR* dir = opendir(path.c_str());
bool ret = dir != NULL;
if(dir) closedir(dir);
return ret;
}
int main()
{
int fd;
struct ifreq ifr;
unsigned char *mac;
DIR *dir;
dirent *entry;
dir = opendir("/sys/class/net/");
if(dir == NULL){
std::cerr << "Error opening /sys/class/net/" << std::endl;
return -1;
}
while((entry = readdir(dir)) != NULL){
if(entry->d_name[0] != '.'){
if(IsPhysicalAdapter(entry->d_name)){
std::cout<< "Physical Adapter: " << entry->d_name << std::endl;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd < 0)
{
perror("socket");
exit(1);
}
strcpy(ifr.ifr_name, entry->d_name);
if(ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
{
perror("socket");
exit(1);
}
mac = (unsigned char *)ifr.ifr_hwaddr.sa_data;
printf("MAC Address: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
close(fd);
}
}
}
closedir(dir);
return 0;
}