-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmac_address.cpp
47 lines (39 loc) · 958 Bytes
/
mac_address.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
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
// using namespace std;
std::string get_mac(){
const int size = 256;
char ip_address[size];
int hw_type;
int flags;
char mac_address[size];
char mask[size];
char device[size];
FILE* fp = fopen("/proc/net/arp", "r");
if(fp == NULL)
{
perror("Error opening /proc/net/arp");
}
char line[size];
fgets(line, size, fp); // Skip the first line, which consists of column headers.
while(fgets(line, size, fp))
{
sscanf(line, "%s 0x%x 0x%x %s %s %s\n",
ip_address,
&hw_type,
&flags,
mac_address,
mask,
device);
// printf("IP = %s, MAC = %s", ip_address, mac_address);
}
fclose(fp);
return mac_address;
}
int main(int argc, char** argv)
{
std::string mac = get_mac();
std::cout << mac;
}