#include <stdio.h>
#include <string.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <errno.h>
char *get_local_mac(char *if_name){
struct ifreq m_ifreq;
int sock = 0;
char mac[32] = " ";
sock = socket(AF_INET,SOCK_STREAM,0);
strcpy(m_ifreq.ifr_name,if_name);
ioctl(sock,SIOCGIFHWADDR,&m_ifreq);
int i = 0;
for(i = 0; i < 6; i++){
sprintf(mac+3*i, "%02X:", (unsigned char) m_ifreq.ifr_hwaddr.sa_data[i]);
}
mac[strlen(mac) - 1] = 0;
printf("MAC: %s\n", mac);
return NULL;
}
int main(){
get_local_mac("eth0");
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-14 06:02:34