日期:2014-05-16 浏览次数:20751 次
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
int getmac(char *ethname, char *macbuf)
{
int sock;
struct ifreq iface;
char tmp_mac[10];
sock = socket(PF_INET, SOCK_DGRAM, 0);
strcpy(iface.ifr_name, ethname);
if(ioctl(sock, SIOCGIFHWADDR, &iface) != 0) {
close(sock);
return 1;
}
memcpy(tmp_mac, iface.ifr_hwaddr.sa_data, 6);
sprintf(macbuf, "%02X:%02X:%02X:%02X:%02X:%02X",
tmp_mac[0] & 0xFF, tmp_mac[1] & 0xFF, tmp_mac[2] &0xFF,