Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1136326
  • 博文数量: 646
  • 博客积分: 288
  • 博客等级: 二等列兵
  • 技术积分: 5375
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-08 14:33
个人简介

为了技术,我不会停下学习的脚步,我相信我还能走二十年。

文章分类

全部博文(646)

文章存档

2014年(8)

2013年(134)

2012年(504)

分类:

2012-11-08 12:41:59

原文地址:linux编程获取MAC 作者:一鸿秋水

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <arpa/inet.h>

#define inaddrr( x ) ( *( struct in_addr * )&ifr->x[ sizeof( sa.sin_port ) ] )
#define IFRSIZE ( ( int )( size * sizeof( struct ifreq ) ) )

int main ( void )
{
    unsigned char * u;
    int sockfd, size = 1;
    struct ifreq * ifr;
    struct ifconf ifc;
    struct sockaddr_in sa;

    if ( 0 > ( sockfd = socket( AF_INET, SOCK_DGRAM, IPPROTO_IP ) ) )
    {
        fprintf( stderr, "Cannot open socket.\n" );
        exit( EXIT_FAILURE );
    }
    ifc.ifc_req = NULL;
    do
    {
        ++size;
        /* realloc buffer size until no overflow occurs */
        if ( NULL == ( ifc.ifc_req = realloc( ifc.ifc_req, IFRSIZE ) ) )
        {
            fprintf( stderr, "Out of memory.\n" );
            exit( EXIT_FAILURE );
        }
        ifc.ifc_len = IFRSIZE;
        if ( ioctl( sockfd, SIOCGIFCONF, &ifc ) )
        {
            perror( "ioctl SIOCFIFCONF" );
            exit( EXIT_FAILURE );
        }
    } while ( IFRSIZE <= ifc.ifc_len );
    ifr = ifc.ifc_req;
    for ( ; ( char * )ifr < ( char * )ifc.ifc_req + ifc.ifc_len; ++ifr )
    {
        if ( ifr->ifr_addr.sa_data == ( ifr + 1 )->ifr_addr.sa_data )
        {
            continue; /* duplicate, skip it */
        }
        if ( ioctl( sockfd, SIOCGIFFLAGS, ifr ) )
        {
            continue; /* failed to get flags, skip it */
        }
        printf( "Interface: %s\n", ifr->ifr_name );
        if ( 0 == ioctl( sockfd, SIOCGIFHWADDR, ifr ) )
        {
            switch ( ifr->ifr_hwaddr.sa_family )
            {
            case ARPHRD_NETROM:
            case ARPHRD_ETHER:
            case ARPHRD_PPP:
            case ARPHRD_EETHER:
            case ARPHRD_IEEE802:
                break;
            default:
                printf( "\n" );
                continue;
            }
            u = ( unsigned char * )&ifr->ifr_addr.sa_data;
            if ( u[0] + u[1] + u[2] + u[3] + u[4] + u[5] )
            {
                printf( "HW Address: %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
                        u[0], u[1], u[2], u[3], u[4], u[5] );
            }
        }
        printf( "\n" );
    } /* end of for */
    close( sockfd );
    return( EXIT_SUCCESS );
}

 

 

输入网路接口名获取MAC地址

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

int main(int agrc,char *agrv[])
{
    struct ifreq ifr;
    char *ptr;
    int skfd;
    if((skfd=socket(AF_INET,SOCK_DGRAM,0))<0){
    perror("open socket error");
    return -1;
    }
    strcpy(ifr.ifr_name,agrv[1]);
    if(ioctl(skfd,SIOCGIFHWADDR,&ifr)<0){
    perror("ioctl error");
    return -1;
    }
    ptr=(char *)&ifr.ifr_addr.sa_data;
    printf("%02X:%02X:%02X:%02X:%02X:%02X\n",(ptr[0]&0377),(ptr[1]&0377),(ptr[2]&0377),
(ptr[3]&0377),(ptr[4]&0377),(ptr[5]&0377));
}

 

[root@woodpecker test]# ./a.out eth0
00:13:20:22:06:D7

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