Chinaunix首页 | 论坛 | 博客
  • 博客访问: 277964
  • 博文数量: 46
  • 博客积分: 2021
  • 博客等级: 大尉
  • 技术积分: 406
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-03 13:00
文章分类

全部博文(46)

文章存档

2011年(1)

2010年(9)

2009年(2)

2007年(13)

2006年(21)

我的朋友

分类: LINUX

2006-10-18 12:59:08

获取主机MAC地址的代码:
 
#include
#include
#include
#include
#include
#include
#include

int main()
{
    struct ifreq ifr;
    int sock;
    unsigned char *ptr;
    int status;
    strcpy (ifr.ifr_name , "eth0");
    sock = socket (PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    if (sock == -1)
    {
        perror ("socket error ");
        return -1;
    }
    status = ioctl (sock, SIOCGIFHWADDR, &ifr);
    if (status < 0)
    {
        perror ("ioctl error ");
        close (sock);
        return -2;
    }
    ptr = ifr.ifr_hwaddr.sa_data;
    printf("%02x.%02x.%02x.%02x.%02x.%02x\n",
            ptr[0], ptr[1],ptr[2],ptr[3],ptr[4],ptr[5]);
}
阅读(1432) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~