Chinaunix首页 | 论坛 | 博客
  • 博客访问: 122489
  • 博文数量: 41
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 306
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-01 10:41
文章分类

全部博文(41)

文章存档

2011年(1)

2009年(40)

我的朋友

分类:

2009-04-10 19:42:55

#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <string.h>

int main(int argc, char *argv[]){
        struct ifreq ifreq;
        int sock;

        if(argc!=2)
        {
                printf("Usage : ethname\n");
                return 1;
        }
        if((sock=socket(AF_INET,SOCK_STREAM,0))<0)
        {
                perror("socket");
                return 2;
        }
        strcpy(ifreq.ifr_name,argv[1]);
        if(ioctl(sock,SIOCGIFHWADDR,&ifreq)<0)
        {
                perror("ioctl");
                return 3;
        }
        printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
                        (unsigned char)ifreq.ifr_hwaddr.sa_data[0],
                        (unsigned char)ifreq.ifr_hwaddr.sa_data[1],
                        (unsigned char)ifreq.ifr_hwaddr.sa_data[2],
                        (unsigned char)ifreq.ifr_hwaddr.sa_data[3],
                        (unsigned char)ifreq.ifr_hwaddr.sa_data[4],
                        (unsigned char)ifreq.ifr_hwaddr.sa_data[5]);
        unsigned char temp = (unsigned char)ifreq.ifr_hwaddr.sa_data[0];
        int a = temp;
        printf("%02x",a);
        return 0;
}

阅读(976) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~