好的爱情是你通过一个人看到整个世界,坏的爱情是你为了一个人舍弃世界。

Linux查看网卡mac

技术文档 6386浏览 0评论

#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;
}

转载请注明:自由的风 » Linux查看网卡mac

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址